Skip to main content
 首页 » 编程设计

使用 PIL 保存 CMYK tif 图像时 Python.exe 崩溃

2025年05月04日9jackei

我在 PIL 中仅遇到 CMYK tif 图像的问题。问题是一切似乎都很顺利,我可以加载文件,保存它,但是当我裁剪它并尝试保存它时,python.exe 只是挂起。这是我的 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") 

此时 python.exe 崩溃了。这不是一个孤立的问题,它一直在此时发生。

如有任何帮助,我们将不胜感激!

PD:我在 Windows 7 x64 操作系统中使用 python 2.7.3 和 PIL 1.1.7。

PD2:Python 崩溃转储:

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 

请您参考如下方法:

crop函数实际上是惰性的,这意味着在您尝试访问像素之前不会发生裁剪,在您的情况下发生在 save 期间。 .

您可以通过调用 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") 

我最初建议这是一种帮助调试问题的方法,因为可能会发生三种情况:

  1. 如果load崩溃,问题在于强制评估裁剪。
  2. 如果load成功,但是 save崩溃,问题在于保存(某些)TIFF 图像。
  3. 如果他们都成功了……首先想到的可能性是您没有足够的内存来同时保存裁剪和未裁剪的版本,或者在某处存在错误save触发惰性函数的评估。

当然,在选项 3 中,如果您只是想通过一次性任务解决这个问题,您可能不会关心进一步调试。但是它很有可能会再次弹出不同的图像,所以如果你正在尝试构建一个更广泛使用的程序,最好继续调试问题(从从故障转储中获取堆栈跟踪开始)。

参见 the docs有关上述所有功能的更多详细信息。