0

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

  • you can do `execfile(r"c:\Scripts\Draw_Rectv2.py")` instead of using `os.system`. I dont know if it will fix it but i think it worth trying – Elisha Sep 30 '14 at 12:09
  • 1
    using `system` to get one Python script to execute another is quite unusual. Have you tried `import` instead? – Kevin Sep 30 '14 at 12:22
  • @Kevin: I used 'system' because I am working in a restricted environment where import can't be used. – Martijn Claes Oct 06 '14 at 06:42
  • @Elisha: How can I get execfile to work with arguments? Nevermind, like the comment I made to, the restricted environment keeps me from using execfile. – Martijn Claes Oct 06 '14 at 06:44
  • http://stackoverflow.com/q/5788891/766068 – Elisha Oct 06 '14 at 06:49

1 Answers1

0

Odd. The PIL docs don't mention any close/destroy/dispose method you would need to call on an image. I also don't see any other memory leak in your code. The last possible problem is that one of the images is very large or contains a Zip Bomb.

To find out if the problem is with one of the images, log the image names (with full path) as you process them. That way, you can look which was the last image that the code tried to process.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820