我们有以下代码:
img = Image.open(FileSystemStorage().path(relpath))
coords = [
cd['crop_start_x'],
cd['crop_start_y'],
cd['crop_end_x'],
cd['crop_end_y']
]
cropped_img = img.crop(coords)
cropped_path = "%s-cropped%s" % os.path.splitext(relpath)
tasks.delete_temporary_file.delay(fss.path(relpath))
cropped_img.save(fss.path(cropped_path))
当尝试保存裁剪后的图像时,我们收到一个奇怪的消息“不是有效的量化表数量。应该在 1 到 4 之间。”异常(exception),仅在我们的一种环境中。 最奇怪的部分是,代码有时可能会工作即使裁剪或图像没有改变
有人在这方面提供线索吗?
我们使用 Pillow 2.8.1、python 2.7.6 和 Ubuntu 服务器 12.04
最佳答案
基本上,问题源于其中一台应用服务器中 PIL 安装的冲突。由于它们隐藏在负载均衡器后面,因此很难找到,因此有时会弹出错误
当我们在控制台上发出 pip freeze
时,我们发现在其中一台服务器中我们同时安装了 PIL 和 Pillow。
删除它们并重新安装 Pillow 后,我们解决了问题。
只是为了说清楚:
pip uninstall PIL
pip uninstall Pillow
pip install Pillow
然后,只需重新启动网络服务器即可。
正如其他人所说,另一个可能的原因是使用:
import Image
该语句仅适用于 PIL,应避免。 我们应该始终使用:
from PIL import Image
关于python - 保存裁剪图像时出现奇怪的枕头异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30651778/