我正在尝试使用python脚本编辑大量图像。图像数量变化很大,当只有少量图像(10-20)时,一切正常,我没有任何错误。当图像数量增加(100-200)时,会发生奇怪的事情。当我直接运行脚本时,我没有得到任何错误,一切都运行良好。当我从另一个脚本调用脚本时,会发生MemoryError
一般过程:
- 检查文件名
- 匹配.txt文件读取和变量设置
- 在图像上绘制矩形
- 保存图片
脚本:
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()
从其他脚本调用脚本的代码行:
os.system(r'python.exe -u "C:\Scripts\Draw_Rectv2.py"')
总结一下: 我试图在图像上绘制矩形,当我正常运行脚本时,我没有得到任何错误。当我从其他脚本调用它时,我得到了
ExceptionInOtherThread(Exception in other Thread - MemoryError)
亲切的问候, 价