Question

I'm having troubles only with CMYK tif images in PIL. The thing is that everything seems to be going fine, I can load the file, save it, but when I crop it and try to save it python.exe just hangs. Here's a rough transcript of my session:

>>> import os
>>> from PIL import Image
>>> os.listdir(".")
['CMYK_TIFF.tif', 'GRAYSCALE_TIFF.tif', 'RGB_TIFF.tif']
>>> im = Image.open("CMYK_TIFF.tif")
>>> im
<PIL.TiffImagePlugin.TiffImageFile image mode=CMYK size=4320x3240 at 0x2630B88>
>>> points = (12, 3, 44, 88)
>>> im = im.crop(points)
>>> im
<PIL.Image._ImageCrop image mode=CMYK size=32x85 at 02630B48>
>>> im.save("new_image.tif")

At this point python.exe just crashes. This is not an isolated issue, it happens consistenly at this point.

Any help would be greatly appreciated!

PD: I'm using python 2.7.3 and PIL 1.1.7 in a windows 7 x64 OS.

PD2: Python crash dump:

Descripción (description)
Ruta de acceso de la aplicación con errores (filepath to the application with errors):                 C:\Python27\python.exe
Firma del problema (problem signature)
Nombre de evento de problema (name of the event or problem):    APPCRASH
Nombre de la aplicación (application name): python.exe
Versión de la aplicación (aplication version):  0.0.0.0
Marca de tiempo de la aplicación (timestamp):   4f84a524
Nombre del módulo con errores (Name of the module with errors): MSVCR90.dll
Versión del módulo con errores (version of the module with errors): 9.0.30729.6161
Marca de tiempo del módulo con errores (module timestamp):  4dace4e7
Código de excepción (exception code):   c0000005
Desplazamiento de excepción (exception displacement):   000000000001e2e0
Versión del sistema operativo (OS version): 6.1.7601.2.1.0.256.48
Id. de configuración regional (regional configuration id):  11274
Información adicional 1:    3312
Información adicional 2:    3312c03e983672d704c6ef8ee1696a00
Información adicional 3:    b29d
Información adicional 4:    b29dcc8fc6f4d939931d139c4d9e8d31

Información adicional sobre el problema
Id. de depósito:    67567272
Was it helpful?

Solution

The crop function is actually lazy, meaning the cropping doesn't happen until you try to access the pixels, which in your case happens during the save.

You can force it to happen eagerly by calling load:

>>> im = im.crop(points)
>>> im
<PIL.Image._ImageCrop image mode=CMYK size=32x85 at 02630B48>
>>> im.load()
<PixelAccess at 0x108d2ba70>
>>> im.save("new_image.tif")

I initially suggested this as a way to help debug the problem, because there are three things that could happen:

  1. If load crashes, the problem is in forcing evaluation of the crop.
  2. If load succeeds, but save crashes, the problem is in saving (certain) TIFF images.
  3. If they both succeed… the first possibilities that come to mind are that you don't have enough memory to keep the cropped and uncropped versions around simultaneously, or there's a bug somewhere in the way save triggers evaluation of lazy functions.

Of course in option 3, if you're just trying to get past this for a one-shot task, you may not care about debugging further. But there's a good chance it'll pop up again with a different image, so if you're trying to build a program for wider use, it's better to continue debugging the problem (starting with getting the stack trace from a crash dump).

See the docs for more details on all of the functions described above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top