I am trying to edit a lot of images with a python script. The amount of images varies a lot, when there are only a small amount of images (10-20) everything works fine and I don't get any errors. When the number of images increases (100-200), something weird happens. When I run the script directly, I don't get any errors what so ever and everything runs fine. When I call the script from another script, a MemoryError occurs.
General process:
- Filename checked
- Matching .txt-file read and variables set
- Draw rectangles on the image
- Save image
Script:
import Image,ImageDraw, os, time, sys
def main() :
print('*****************************************')
print(' Draw_Rectangle v1.2 ')
print('*****************************************')
print(' ')
start= time.time()
print(' ')
print('Starting drawing rectangles...')
path = os.path.join("C:\Program Files\test\images")
fileList = os.listdir(path)
total = len(fileList)
count = 0
for fileName in fileList :
if fileName.endswith("FULL.png") :
ins = open(os.path.join(path,fileName[:-9]+".txt"), "r")
for line in ins :
if line[0] == "x" :
x = line[2:]
if line[0] == "y" :
y = line[2:]
if line[0] == "w" :
w = line[2:]
if line[0] == "h" :
h = line[2:]
break
ins.close()
im = Image.open(os.path.join(path,fileName))
draw=ImageDraw.Draw(im)
draw.rectangle([int(x),int(y),int(x) + int(w),int (y) + int(h)],outline="#0000FF")
draw.rectangle([int(x)-1,int(y)-1,int(x) + int(w)-1,int (y) + int(h)-1],outline="#0000FF")
draw.rectangle([int(x)+1,int(y)+1,int(x) + int(w)+1,int (y) + int(h)+1],outline="#0000FF")
im.save(os.path.join(path,fileName),"PNG")
count+=4
percentage = (count * 100) / total
print 'Progress : [%d%%]\r'%percentage,
duration= time.time()-start
print('Progress : [100%]')
print(' ')
print('Drawing rectangles completed')
print(' ')
print('Duration : ' + str(duration) + ' seconds')
return 0
if __name__ == "__main__":
main()
The line of code to call the script from the other script:
os.system(r'python.exe -u "C:\Scripts\Draw_Rectv2.py"')
So to summarize: I am trying to draw rectangles on the images, when I run the script normally, I don't get any errors. When i call it from the other script, I get
ExceptionInOtherThread(Exception in other Thread - MemoryError)
Kind Regards, Price