Python PIL:如何保存裁剪后的图像?

发布于 2024-11-16 21:03:54 字数 176 浏览 7 评论 0原文

我有一个脚本可以创建图像并裁剪它。问题是,在我调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰ゝ天使的微笑 2024-11-23 21:03:54

您需要将参数传递给元组中的 .crop() 。并且不要使用 .load()

box = (x_offset, Y_offset, width, height)
crop = image.crop(box)
return crop.save(image_path, format)

这就是您所需要的。虽然,我不确定你为什么要返回保存操作的结果;它返回None

You need to pass the arguments to .crop() in a tuple. And don't use .load()

box = (x_offset, Y_offset, width, height)
crop = image.crop(box)
return crop.save(image_path, format)

That's all you need. Although, I'm not sure why you're returning the result of the save operation; it returns None.

七度光 2024-11-23 21:03:54

主要的问题是尝试将 load() 返回的对象用作图像对象。来自 PIL 文档:

在 [PIL] 1.1.6 及更高版本中,load 返回一个像素访问对象,可用于读取和修改像素。访问对象的行为类似于二维数组 [...]

试试这个:

crop = image.crop((x_offset, Y_offset, width, height))   # note the tuple
crop.load()    # OK but not needed!
return crop.save(image_path, format)

The main trouble is trying to use the object returned by load() as an image object. From the PIL documentation:

In [PIL] 1.1.6 and later, load returns a pixel access object that can be used to read and modify pixels. The access object behaves like a 2-dimensional array [...]

Try this:

crop = image.crop((x_offset, Y_offset, width, height))   # note the tuple
crop.load()    # OK but not needed!
return crop.save(image_path, format)
-小熊_ 2024-11-23 21:03:54

这是 PIL 1.1.7 新版本的完整工作答案。裁剪坐标现在为左上角右下角(不是 x、y、宽度、高度)。

python 的版本号是:2.7.15

,PIL 的版本号是:1.1.7

# -*- coding: utf-8 -*-

from PIL import Image
import PIL, sys

print sys.version, PIL.VERSION


for fn in ['im000001.png']:

    center_x    = 200
    center_y    = 500

    half_width       = 500
    half_height      = 100

    imageObject = Image.open(fn)

    #cropped = imageObject.crop((200, 100, 400, 300))


    cropped = imageObject.crop((center_x - half_width,
                                center_y - half_height, 
                                center_x + half_width,
                                center_y + half_height,
                                ))

    cropped.save('crop_' + fn, 'PNG')

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

# -*- coding: utf-8 -*-

from PIL import Image
import PIL, sys

print sys.version, PIL.VERSION


for fn in ['im000001.png']:

    center_x    = 200
    center_y    = 500

    half_width       = 500
    half_height      = 100

    imageObject = Image.open(fn)

    #cropped = imageObject.crop((200, 100, 400, 300))


    cropped = imageObject.crop((center_x - half_width,
                                center_y - half_height, 
                                center_x + half_width,
                                center_y + half_height,
                                ))

    cropped.save('crop_' + fn, 'PNG')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文