Python PIL:如何保存裁剪后的图像?
我有一个脚本可以创建图像并裁剪它。问题是,在我调用 Crop() 方法后,它不会保存在磁盘上
crop = image.crop(x_offset, Y_offset, width, height).load()
return crop.save(image_path, format)
I have a script which creates an image and the it crops it. The problem is that after I call the crop() method it does not save on the disk
crop = image.crop(x_offset, Y_offset, width, height).load()
return crop.save(image_path, format)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将参数传递给元组中的
.crop()
。并且不要使用.load()
这就是您所需要的。虽然,我不确定你为什么要返回保存操作的结果;它返回
None
。You need to pass the arguments to
.crop()
in a tuple. And don't use.load()
That's all you need. Although, I'm not sure why you're returning the result of the save operation; it returns
None
.主要的问题是尝试将
load()
返回的对象用作图像对象。来自 PIL 文档:试试这个:
The main trouble is trying to use the object returned by
load()
as an image object. From the PIL documentation:Try this:
这是 PIL 1.1.7 新版本的完整工作答案。裁剪坐标现在为左上角和右下角(不是 x、y、宽度、高度)。
python 的版本号是:2.7.15
,PIL 的版本号是:1.1.7
Here is a fully working aswer with a NEW version of PIL 1.1.7. The crop coordinates are now upper left corner and lower right corner (NOT the x, y, width, height).
The version number for python is: 2.7.15
and for PIL: 1.1.7