【问题标题】:Image Processing MemoryError图像处理内存错误
【发布时间】:2014-09-30 12:05:45
【问题描述】:

我正在尝试使用 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)

亲切的问候, 价格

【问题讨论】:

  • 您可以使用execfile(r"c:\Scripts\Draw_Rectv2.py") 而不是使用os.system。我不知道它是否会解决它,但我认为值得一试
  • 使用system 让一个Python 脚本执行另一个是很不寻常的。你试过import吗?
  • @Kevin:我使用“系统”是因为我在无法使用导入的受限环境中工作。
  • @Elisha:如何让 execfile 使用参数?没关系,就像我发表的评论一样,受限环境使我无法使用 execfile。

标签: python image memory image-processing python-2.2


【解决方案1】:

奇怪。 PIL docs 没有提到您需要在图像上调用的任何关闭/销毁/处置方法。我也没有在您的代码中看到任何其他内存泄漏。最后一个可能的问题是其中一个图像非常大或包含Zip Bomb

要查明问题是否出在其中一张图像上,请在处理图像时记录图像名称(带有完整路径)。这样,您可以查看代码尝试处理的最后一张图片。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-22
    • 2015-05-19
    • 2016-07-20
    • 2014-04-24
    • 1970-01-01
    • 2011-10-18
    • 2016-08-05
    • 2011-02-08
    相关资源
    最近更新 更多