While conversion, invalid literal for float - python

I'm trying to train my own data on the Yolo network, but before that I have to convert the bounding boxes co-ordinates to the form it wants.
The file contents are like this:
0
53 19 163 116
and I'm trying to convert it to the form the network works with the following.
The code is:
import os
from os import walk, getcwd
from PIL import Image
classes = ["stopsign"]
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
"""-------------------------------------------------------------------
"""
""" Configure Paths"""
mypath = "/home/decentmakeover2/BBox-Label-Tool/Labels/002/"
outpath = "/home/decentmakeover2/output/"
cls = "stopsign"
if cls not in classes:
exit(0)
cls_id = classes.index(cls)
wd = getcwd()
list_file = open('%s/%s_list.txt'%(wd, cls), 'w')
""" Get input text file list """
txt_name_list = []
for (dirpath, dirnames, filenames) in walk(mypath):
txt_name_list.extend(filenames)
break
print(txt_name_list)
""" Process """
for txt_name in txt_name_list:
#txt_file = open("Labels/stop_sign/001.txt", "r")
""" Open input text files """
txt_path = mypath + txt_name
print("Input:" + txt_path)
txt_file = open(txt_path, "r")
lines = txt_file.read().split('\r\n') #for ubuntu, use "\r\n"
instead of "\n"
""" Open output text files """
txt_outpath = outpath + txt_name
print("Output:" + txt_outpath)
txt_outfile = open(txt_outpath, "w")
""" Convert the data to YOLO format """
ct = 0
for line in lines:
#print('lenth of line is: ')
#print(len(line))
#print('\n')
if(len(line) >= 2):
ct = ct + 1
print(line + "\n")
elems = line.split(' ')
print(elems)
xmin = elems[0]
xmax = elems[2]
ymin = elems[1]
ymax = elems[3]
#
img_path = str('%s/images/%s/%s.JPEG'%(wd, cls,
os.path.splitext(txt_name)[0]))
#t = magic.from_file(img_path)
#wh= re.search('(\d+) x (\d+)', t).groups()
im=Image.open(img_path)
w= int(im.size[0])
h= int(im.size[1])
#w = int(xmax) - int(xmin)
#h = int(ymax) - int(ymin)
# print(xmin)
print(w, h)
b = (float(xmin), float(xmax), float(ymin), float(ymax))
bb = convert((w,h), b)
print(bb)
txt_outfile.write(str(cls_id) + " " + " ".join([str(a) for
a
in bb]) + '\n')
""" Save those images with bb into list"""
if(ct != 0):
list_file.write('%s/images/%s/%s.JPEG\n'%(wd, cls,
os.path.splitext(txt_name)[0]))
list_file.close()
and i get the error:
first it prints out all the file names and the content of the data then,
['0\n53', '19', '163', '116\n']
(262, 192)
Traceback (most recent call last):
File "text.py", line 84, in <module>
b = (float(xmin), float(xmax), float(ymin), float(ymax))
ValueError: invalid literal for float(): 0
53
I'm not really sure what to do here.
Any suggestions?

As seen in the error message your first term is '0\n53' where as it should be '0' followed by '53'. thus it isn't detected as a float. just splitting with '\n' should work.

Related

FPDF Python Package to Write PDF Files - Inserting extra line breaks. Does this for Multi_cell and cell. Want to remove extra line breaks

I have a python application that traverses directories on the file path passed in as a parameter. As each file is found, the file is opened and data is inserted into multi_cells.
I have checked the TXT values provided to multi_cell and there are no extra line breaks. I have played around with different setting to no avail. When inspecting the PDF output, extra line breaks are inserted. Any help would be appreciated. Look at the end of the script --- the final multicell.
import os,sys
from fpdf import FPDF
for i in range(1, len(sys.argv)):
print('argument:', i, 'value:', sys.argv[i])
mypath = sys.argv[i];
mypath = "D:\\test\\test\\CrossingTrades\\DataDictionary\\testLib\\forms"
class TOC(FPDF):
def __init__(this, orientation='P',unit='mm',format='A4'):
this._toc=[]
this._numbering=0
this._numberingFooter=0
this._numPageNum=1
FPDF.__init__(this,orientation,unit,format)
def header(self):
self.set_font('Courier', 'B', 15);
self.cell(0, 5, "Tradeflow Format and Rule Documentation", 0, 0, 'C')
self.ln(20)
def footer(self):
self.set_y(-10)
self.set_font('Arial', 'I', 8)
# Add a page number
page = 'Page ' + str(self.page_no()) + '/{nb}'
self.cell(0, 10, page, 0, 0, 'C')
def startPageNums(this):
this._numbering=1
this._numberingFooter=1
def stopPageNums(this):
this._numbering=0
def numPageNo(this):
return this._numPageNum
def TOC_Entry(this,txt,level,pageNumber):
this._numPageNum=pageNumber
this._toc+=[{'t':txt,'l':level,'p':this.numPageNo()}]
#print("PAGE NO IS ",this.numPageNo())
def insertTOC(this,location=1,labelSize=20,entrySize=10,tocfont='Times',label='Table of Contents'):
#make toc at end
this.stopPageNums()
#this.AddPage()
this.add_page();
this._numPageNum+=1
tocstart=this.page
this.set_font(tocfont,'B',labelSize)
this.cell(0,5,label,0,1,'C')
this.ln(10)
for t in this._toc:
#Offset
level=t['l']
if(level>0):
this.cell(level*8)
weight=''
if(level==0):
weight='B'
Str=t['t']
this.set_font(tocfont,weight,entrySize)
strsize=this.get_string_width(Str)
this.cell(strsize+2,this.font_size+2,Str)
#Filling dots
this.set_font(tocfont,'',entrySize)
PageCellSize=this.get_string_width(str(t['p']))+2
w=this.w-this.l_margin-this.r_margin-PageCellSize-(level*8)-(strsize+2)
#nb=w/this.get_string_width('.')
dots = "........................................................................................................"
#dots.replace('.', '.'*60, 1)
this.cell(w,this.font_size+2,dots,0,0,'R')
#Page number
this.cell(PageCellSize,this.font_size+2,str(t['p']),0,1,'R')
#grab it and move to selected location
n=this.page
n_toc = n - tocstart + 1
last = []
#store toc pages
for i in range(tocstart,n+1):
last+=[this.pages[i]]
#move pages
for i in range(tocstart-1,location-1,-1):
#~ for(i=tocstart - 1;i>=location-1;i--)
this.pages[i+n_toc]=this.pages[i]
#Put toc pages at insert point
for i in range(0,n_toc):
this.pages[location + i]=last[i]
#def Footer(this):
# if(this._numberingFooter==0):
# return
# #Go to 1.5 cm from bottom
# this.SetY(-15)
# #Select Arial italic 8
# this.SetFont('Arial','I',8)
# this.Cell(0,7,str(this.numPageNo()),0,0,'C');
# if(this._numbering==0):
# this._numberingFooter=0
class DPLFormat:
def __init__(self, name,arguments,contentsList,callerList,callingList):
self.name = name
self.arguments = arguments
self.contentsList = contentsList
self.callerList = callerList
self.callingList = callingList
class DPLRule:
def __init__(self, name,arguments,contentsList,callerList,callingList):
self.name = name
self.arguments = arguments
self.contentsList = contentsList
self.callerList = callerList
self.callingList = callingList
def get_filepaths(directory):
"""
This function will generate the file names in a directory
tree by walking the tree either top-down or bottom-up. For each
directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).
"""
file_paths = [] # List which will store all of the full filepaths.
#print ("Opening:", directory)
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
if ".cfformat" in filename and ".cfformatprop" not in filename:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
file_paths.append(filepath) # Add it to the list.
#file_paths.append(filename) # Add it to the list.
return file_paths # Self-explanatory.
# PDF Handling
# save FPDF() class into a
# variable pdf
pdf = TOC('P', 'mm', 'A4')
pdf.alias_nb_pages()
# Run the above function and store its results in a variable.
full_file_paths = get_filepaths(mypath)
formatList = []
tocList = []
print ("Beginning Processing ",mypath)
for index, item in enumerate(full_file_paths):
formatName = item.replace(".cfformat","")
#print(index,"",formatName)
formatArgs = ""
ruleFlag = 1
contentsList = []
callerList = []
callingList = []
#Find format in files
full_file_paths2 = get_filepaths(mypath)
cnt = 0
for index2, item2 in enumerate(full_file_paths2):
formatName2 = item2.replace(".cfformat","")
#if cnt == 0:
# print("Searching ",os.path.basename(formatName)," IN ",os.path.basename(formatName2))
with open(item2,'r') as fp2:
line2 = fp2.readline()
#print ("Opening ",os.path.basename(formatName2))
while line2:
line2 = fp2.readline()
if cnt == 0 :
if os.path.basename(formatName)in line2 and os.path.basename(formatName) != os.path.basename(formatName2) :
callerList.append(os.path.basename(formatName2))
cnt += 1
#---------------------END SEARCH FOR FORMAT IN OTHER FILES------------------
with open(item,'r') as fp:
line = fp.readline()
if "[" in line and "SQL" or ";" not in line:
formatArgs = line
ruleFlag = 0
cnt = 1
while line:
line = fp.readline()
#print("Line {}: {}".format(cnt, line.strip()))
#if "!" in line:
line = line.replace("–", "-")
#res = bytes(line,'UTF-8')
contentsList.append(line)
if "#" in line:
callingList.append(line)
cnt += 1
if formatArgs != "":
formatList.append( DPLFormat(os.path.basename(formatName),formatArgs,contentsList,callerList,callingList) )
#Now go through format files
pdf.set_font("Courier", size = 8)
formatList.sort(key=lambda x: x.name)
pdf.startPageNums()
for obj in formatList:
#print( obj.name, obj.arguments,sep =' ' )
# Add a page
pdf.add_page()
pdf.TOC_Entry(obj.name,1,pdf.page_no())
# caller list
pdf.set_font('Courier', 'B', size=13);
pdf.cell(200, 10, txt = obj.name ,
ln = 1, align = 'C')
ii = 0
pdf.set_font('Courier', 'I', size=10);
pdf.cell(200, 10, txt = "Called By: " ,
ln = 1, align = 'L')
callerStr=""
#print ("Caller list length is ", len(obj.callerList))
while ii != len (obj.callerList):
callerStr=callerStr+obj.callerList[ii]
ii += 1
pdf.multi_cell(0, 8, callerStr, 1, 'J')
#calling list
pdf.set_font('Courier', 'I', size=10);
ii = 0
pdf.cell(200, 10, txt = "Calling: " ,
ln = 1, align = 'L')
callingStr=""
#print ("Caller list length is ", len(obj.callerList))
while ii != len (obj.callingList):
callingStr=callingStr+obj.callingList[ii]
ii += 1
pdf.set_font('Courier',size=8);
pdf.multi_cell(0, 8, callingStr, 1, 'J')
#contents
pdf.set_font('Courier',size=8);
i = 0
codeStr = ""
while i != len (obj.contentsList):
codeStr=codeStr+obj.contentsList[i]
i += 1
pdf.multi_cell(0, 8, codeStr, 1, 'J')
# save the pdf with name .pdf
print ("\nWriting PDF Documentation")
pdf.insertTOC()
pdf.output("D:\\DPLAutoDoc.pdf")
print ("\nFinished")
The width in multi cell call, the first parameter is cell size or width. In portrait set that to something like 190. The second value actually controls spacing between lines so set to a value like 1 or 2. Set justification to left.

How to read content from a file and then save them in a file?

Not very good at programming. Need to change the script code. It is necessary to read the contents of the file and split it into separate keys, and then save them in a file. Thank you in advance for your help!!!
'text.txt'
File:
0200e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5
0216b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e1278
022bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbc
021060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd72
0218cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514e
025d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4
02264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efed
import binascii
with open('text.txt') as f:
text = f.read()
compressed_key_hex = text.split('\n')
computed_uncompressed_key = []
p_hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F'
p = int(p_hex, 16)
x_hex = compressed_key_hex[2:66]
x = int(x_hex, 16)
prefix = compressed_key_hex[0:2]
y_square = (pow(x, 3, p) + 7) % p
y_square_square_root = pow(y_square, (p+1)//4, p)
if prefix == "02":
y = (-y_square_square_root) % p
else:
y = y_square_square_root
computed_y_hex = hex(y)[2:66]
computed_uncompressed_key = "04" + x_hex + computed_y_hex
with open('result.txt', 'w') as f:
f.write('\n'.join(computed_uncompressed_key))
I get the error:
===================== RESTART: D:\detailALL\03\Bit06.py =====================
Traceback (most recent call last):
File "D:\detailALL\03\Bit06.py", line 12, in <module>
x = int(x_hex, 16)
TypeError: int() can't convert non-string with explicit base
>>>
You are passing a list rather than a str. In the following code x_hex is a list.
x_hex = compressed_key_hex[2:66]
So you need to convert the list to str, you can do that using the following:
x_hex = ''.join(compressed_key_hex[2:66])
I guess the following might be your required solution:
import binascii
with open('text.txt') as f:
text = f.read()
compressed_key_hex = text.split('\n')
print(compressed_key_hex)
computed_uncompressed_key_list = []
p_hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F'
p = int(p_hex, 16)
for val in compressed_key_hex:
x_hex = val[2:66]
x = int(x_hex, 16)
prefix = val[0:2]
y_square = (pow(x, 3, p) + 7) % p
y_square_square_root = pow(y_square, (p+1)//4, p)
if prefix == "02":
y = (-y_square_square_root) % p
else:
y = y_square_square_root
computed_y_hex = hex(y)[2:66]
computed_y_hex = computed_y_hex.zfill(64)
computed_uncompressed_key = "04" + x_hex + computed_y_hex
computed_uncompressed_key_list.append(computed_uncompressed_key)
with open('result.txt', 'w') as f:
f.write('\n'.join(computed_uncompressed_key_list))
text.txt file:
0200e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5
0216b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e1278
022bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbc
021060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd72
0218cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514e
025d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4
02264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efed
result.txt file:
0400e7c810f4553fe1722522f8dcfc8e810757ef427efefef79bdf08ddf3700fd5c9b034d2aa9ee1ef7b2346e8fc9c0245a8746a92bfdbb472fc98397477551ced
0416b3e68fed004b2fea2119cdbb8ab2393dfe8fc99398da18e40b6e949e9e12780126dfa95d2d9ab8fc055ce158f1d2ef51c2a012413b3f88a6365f375cf903f8
042bbf0fcde9bcba6e1038b78bd6906ed00be95d1a6f912a7352f5aca2d7bb6bbcf4a39790075ce43dc08fbf0ecc9cc732415e6b066c3b8b8d960b8548e8a612b7
041060631ef4a610aebc3c9e24f5b0e33dcd0eb422b8223dbd75c1e6edfd21dd723f873c976d071939edf8450124da64c3d9a1b35fb070761b01a5bace7d741588
0418cbb66d6a417890aea6bf5f8a83a4d181a89c5aba8121e20def5b42c311514efb4f8645c503e7a39954e977f7af8e802a5ec44ce3084cb6fb4e133a79733e77
045d8ea956802ed00ebec42b480c0eb77c6ada6ed3fceb40e5fff9aed0fa31c6b4e7c279c9d2c3e731803a4dde91a0d9409e49b1cbec3c7ac536a3783d9518d737
04264a8c56551abeb68d6112863249857a4360c38528d02b9313988ba062e6efeddbd8a97a8762f6a1add1ea6f549b61316fe675fc703d49f597a91ad620f7627a

I am trying to apply a python code to all the files in a directory but it gives me a error

I am trying to apply a python code to all the files in a directory but it gives me a error:
test_image = cv2.imread(sys.argv[1],0)
IndexError: list index out of range
I dont know what to change I tried few things but it does not help so if someone can help with this that would be great. And using stackoverflow for the first time, just to see how it works.
import sys
import cv2
import os
import numpy as np
from utils import pointsInsideCircle, compare, zigzag
from math import pi as PI
filepath = os.path.join("/Users/ssm/Desktop/X/1/Original Images", "*.tif")
W = 8 #block size for comparision
Dsim = 0.1 #threshold for symmetry
Nd = 25 #nearest block
quadrants_points = pointsInsideCircle(W/4) #(i,j) position of blocks which are partially/completely inside circle of radius W/2
zigzag_points = zigzag(W/2)
test_image = cv2.imread(sys.argv[1],0)
height,width = test_image.shape[:2]
#print (height,width)
vectors_list = []
for j in range(0,height-W+1):
for i in range(0,width-W+1):
block = test_image[j:j+W,i:i+W]
dct_block = cv2.dct(np.float32(block))
feature_block = [[],[],[],[]]
for index,coeff_list in enumerate(zigzag_points):
for coeff in coeff_list:
feature_block[index].append(dct_block[coeff[0],coeff[1]])
feature_block_np = np.array(feature_block)
feature_vector = []
for quadrant,points in quadrants_points.iteritems():
summ = 0
for point in points:
summ = summ + feature_block_np[point[0],point[1]]
feature_vector.append(summ/PI)
vectors_list.append(np.array(feature_vector))
vectors_list2 = cv2.sort(np.array(vectors_list),cv2.SORT_EVERY_ROW)
print "vectors calculated"
import json
with open('data.json', 'w') as outfile:
json.dump(vectors_list2.tolist(), outfile)
i=0
blocks = []
for i in range(0,len(vectors_list)):
if i%width == 0:
print i/width
posA = [i/width,i%width]
j = i+1
for j in range(i+1,len(vectors_list)):
posB = [j/width,j%width]
if compare(vectors_list[i],vectors_list[j],posA,posB,Dsim,Nd):
print (posA,posB)
blocks.append([posA,posB])
output_image = cv2.imread(sys.argv[1],1)
for block in blocks:
x1 = block[0][0]
x1_8 = block[0][0]+W
y1 = block[0][1]
y1_8 = block[0][1]+W
output_image[x1:x1_8,y1:y1_8] = [0,0,255]
x2 = block[1][0]
x2_8 = block[1][0]+W
y2 = block[1][1]
y2_8 = block[1][1]+W
output_image[x2:x2_8,y2:y2_8]=[0,255,0]
cv2.imwrite("output.jpg",output_image)
print "feature vectors extracted"
test_image = cv2.imread(sys.argv[1],0)
is checking the list provided by the commandline for a file name. For example if you invoked this script with:
$python myprog.py afilename.xxx
sys.argv would be ['myprog', 'afilename.xxx'], and this imread line would load an image from afilename.xxx.
If you don't provide that filename, sys.argv will only have the script name, and sys.argv[1] will raise this error.

Python, memory error, csv file too large [duplicate]

This question already has answers here:
Reading a huge .csv file
(7 answers)
Closed 8 years ago.
I have a problem with a python module that cannot handle importing a big datafile (the file targets.csv weights nearly 1 Gb)
the error appens when this line is loaded:
targets = [(name, float(X), float(Y), float(Z), float(BG))
for name, X, Y, Z, BG in csv.reader(open('targets.csv'))]
traceback:
Traceback (most recent call last):
File "C:\Users\gary\Documents\EPSON STUDIES\colors_text_D65.py", line 41, in <module>
for name, X, Y, Z, BG in csv.reader(open('targets.csv'))]
MemoryError
I was wondering if there's a way to open the file targets.csv line by line? And also wondering it this would slow down the process?
This module is already pretty slow...
Thanks!
import geometry
import csv
import numpy as np
import random
import cv2
S = 0
img = cv2.imread("MAP.tif", -1)
height, width = img.shape
pixx = height * width
iterr = float(pixx / 1000)
accomplished = 0
temp = 0
ppm = file("epson gamut.ppm", 'w')
ppm.write("P3" + "\n" + str(width) + " " + str(height) + "\n" + "255" + "\n")
# PPM file header
all_colors = [(name, float(X), float(Y), float(Z))
for name, X, Y, Z in csv.reader(open('XYZcolorlist_D65.csv'))]
# background is marked SUPPORT
support_i = [i for i, color in enumerate(all_colors) if color[0] == '255 255 255']
if len(support_i)>0:
support = np.array(all_colors[support_i[0]][1:])
del all_colors[support_i[0]]
else:
support = None
tg, hull_i = geometry.tetgen_of_hull([(X,Y,Z) for name, X, Y, Z in all_colors])
colors = [all_colors[i] for i in hull_i]
print ("thrown out: "
+ ", ".join(set(zip(*all_colors)[0]).difference(zip(*colors)[0])))
targets = [(name, float(X), float(Y), float(Z), float(BG))
for name, X, Y, Z, BG in csv.reader(open('targets.csv'))]
for target in targets:
name, X, Y, Z, BG = target
target_point = support + (np.array([X,Y,Z]) - support)/(1-BG)
tet_i, bcoords = geometry.containing_tet(tg, target_point)
if tet_i == None:
#print str("out")
ppm.write(str("255 255 255") + "\n")
print "out"
temp += 1
if temp >= iterr:
accomplished += temp
print str(100 * accomplished / (float(pixx))) + str(" %")
temp = 0
continue
# not in gamut
else:
A = bcoords[0]
B = bcoords[1]
C = bcoords[2]
D = bcoords[3]
R = random.uniform(0,1)
names = [colors[i][0] for i in tg.tets[tet_i]]
if R <= A:
S = names[0]
elif R <= A+B:
S = names[1]
elif R <= A+B+C:
S = names[2]
else:
S = names[3]
ppm.write(str(S) + "\n")
temp += 1
if temp >= iterr:
accomplished += temp
print str(100 * accomplished / (float(pixx))) + str(" %")
temp = 0
print "done"
ppm.close()
csv.reader() already reads the lines one at a time. However, you're collecting all of the lines into a list first. You should process the lines one at a time. One approach is to switch to a generator, for example:
targets = ((name, float(X), float(Y), float(Z), float(BG))
for name, X, Y, Z, BG in csv.reader(open('targets.csv')))
(Switching from square brackets to parens should change target from a list comprehension to a generator.)

IOError "no such file or folder" even though files are present

I wrote a script in Python 2.6.2 that scans a directory for SVG's and resizes them if they are too large. I wrote this on my home machine (Vista, Python 2.6.2) and processed a few folders with no problems. Today, I tried this on my work computer (XP SP2, Python 2.6.2) and I get IOErrors for every file, even though files are in the directory. I think I've tried everything, and am unsure where to go from here. I am a beginner so this may be something simple. Any help would be appreciated.
import xml.etree.ElementTree as ET
import os
import tkFileDialog
#--------------------------------------
#~~~variables
#--------------------------------------
max_height = 500
max_width = 428
extList = ["svg"]
proc_count = 0
resize_count = 0
#--------------------------------------
#~~~functions
#--------------------------------------
def landscape_or_portrait():
resize_count +=1
if float(main_width_old)/float(main_height_old) >= 1.0:
print "picture is landscape"
resize_width()
else:
print "picture is not landscape"
resize_height()
return
def resize_height():
print "picture too tall"
#calculate viewBox and height
viewBox_height_new = max_height
scaleFactor = (float(main_height_old) - max_height)/max_height
viewBox_width_new = float(main_width_old) * scaleFactor
#calculate main width and height
main_height_new = str(viewBox_height_new) + "px"
main_width_new = str(viewBox_width_new) + "px"
viewBox = "0 0 " + str(viewBox_width_new) + " " + str(viewBox_height_new)
inputFile = file(tfile, 'r')
data = inputFile.read()
inputFile.close()
data = data.replace(str(tmain_height_old), str(main_height_new))
data = data.replace(str(tmain_width_old), str(main_width_new))
#data = data.replace(str(tviewBox), str(viewBox))
outputFile = file(tfile, 'w')
outputFile.write(data)
outputFile.close()
return
def resize_width():
print "picture too wide"
#calculate viewBox width and height
viewBox_width_new = max_width
scaleFactor = (float(main_width_old) - max_width)/max_width
viewBox_height_new = float(main_height_old) * scaleFactor
#calculate main width and height
main_height_new = str(viewBox_height_new) + "px"
main_width_new = str(viewBox_width_new) + "px"
viewBox = "0 0 " + str(viewBox_width_new) + " " + str(viewBox_height_new)
inputFile = file(tfile, 'r')
data = inputFile.read()
inputFile.close()
data = data.replace(str(tmain_height_old), str(main_height_new))
data = data.replace(str(tmain_width_old), str(main_width_new))
#data = data.replace(str(tviewBox), str(viewBox))
outputFile = file(tfile, 'w')
outputFile.write(data)
outputFile.close()
return
#--------------------------------------
#~~~operations
#--------------------------------------
path = tkFileDialog.askdirectory()
for tfile in os.listdir(path):
#print tfile
t2file = tfile
if tfile.find(".") >= 0:
try :
if t2file.split(".")[1] in extList:
print "now processing " + tfile
tree = ET.parse(tfile)
proc_count+=1
# Get the values of the root(svg) attributes
root = tree.getroot()
tmain_height_old = root.get("height")
tmain_width_old = root.get("width")
tviewBox = root.get("viewBox")
#clean up variables, remove px for float conversion
main_height_old = tmain_height_old.replace("px", "", 1)
main_width_old = tmain_width_old.replace("px", "", 1)
#check if they are too large
if float(main_height_old) > max_height or float(main_width_old) > max_width:
landscape_or_portrait()
except Exception,e:
print e
It looks to me like you are missing a os.path.join(path, tfile) to get the full path to the file you want to open. Currently it should only work for files in the current directory.
Perhaps it's a security issue? Perhaps you don't have the rights to create files in the folder

Categories

Resources