Pillow's Image.open() MemoryError while opening big tiff file

1.8k views Asked by At

I am writing a python script that will be running on the server and will resize images (namely Indesign links). It all works fine, except when I am trying to open bigger tiff files (800 MB).

I am using Pillow's Image.open() in order to resize it, but I am getting MemoryError error. I tried with other libraries like ImageMagick and tifffile with the same results. I could split it in smaller chunks, resize those and then combine them, but I have no idea how to do that without opening file first.

I searched extensively, but cannot find solution that would seem appropriate to my case. I was also observing memory consumption and it doesn't seem to be irregularly high. It's safe to say I am completely lost.

The whole trace goes like this:

Traceback (most recent call last):
  File "app.py", line 87, in <module>
    convert()
  File "X:\Development\Python\zipper\converter.py", line 112, in convert
    if saveAsJPEG(file, name + ".jpg"): converted_images = updateCounter(all_images, converted_images)
  File "X:\Development\Python\zipper\converter.py", line 37, in saveAsJPEG
    print Image.open(a).size
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 2266, in open
    im = factory(fp, filename)
  File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 97, in __init__
    self._open()
  File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 637, in _open
    self._seek(0)
  File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 672, in _seek
    self.tag.load(self.fp)
  File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 458, in load
    data = ImageFile._safe_read(fp, size)
  File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 521, in _safe_read
    block = fp.read(min(size, SAFEBLOCK))
MemoryError

Thank you all for any help!

--- EDIT

This is the code that throws error

def saveAsJPEG(file, newfile):
    print "\nopening:", file
    try:
        with Image.open(file) as im:
            if not im.size[0] < size[0] or im.size[1] < size[1]:
                new_im = im.resize(size, Image.ANTIALIAS)
                new_im.save(newfile, 'JPEG', quality=100, dpi=(72, 72))

                # Force a memory dump.  Otherwise memory will get cluttered up -> I am not sure if this is necessary as it doesn't seem to do anything. 
                del im
                del new_im
                collect()
                return True
    except:
        print "image is too big -> try something else. But what?"
        # this line below throws error (MemoryError). It was the same without try-except part before.
        Image.open(file)

The function is used in for-loop where "images" is array (list) of full file paths (X:\path\to\my\image.tiff)

for file in images:
    #gets extension
    name = basename(splitext(file)[0])
    ext = splitext(file)[1]

    # check for extension type and run appropriate function
    if ext == '.jpg' or ext == '.jpeg' or ext == '.tif' or ext == '.tiff':
        if saveAsJPEG(file, name + ".jpg"): converted_images = updateCounter(all_images, converted_images)

    del file, name, ext
0

There are 0 answers