【问题标题】:PIL crop and paste problem: Cropping doesn't create a cropped imagePIL 裁剪和粘贴问题:裁剪不会创建裁剪的图像
【发布时间】:2011-04-19 19:14:42
【问题描述】:

我正在尝试裁剪图像,然后将裁剪后的图像粘贴到另一个图像的中心。理想情况下,我希望裁剪后的图像小于其粘贴的图像,以便粘贴的图像周围有边框,但我不知道这是否可能。

这是我尝试过的(以及产生的错误消息):

>>> import Image
>>> grey = Image.new('RGB', (200, 200), "grey")
>>> House = Image.open("House01.jpg")
>>> print grey.size, grey.mode, grey.format
>>>(200, 200) RGB None
>>> print House.size, House.mode, House.format
>>>(300, 300) RGB JPEG
>>> box = (25, 25, 25, 25)
>>> House.crop(box)
>>>Image._ImageCrop image mode=RGB size=0x0 at 0x11AD210>
>>> region = House.crop(box)
>>> region.show()
>>>Traceback (most recent call last):
 >>> File "<pyshell#28>", line 1, in <module>
    region.show()
>>>  File "C:\Python26\lib\site-packages\PIL\Image.py", line 1483, in show
    _show(self, title=title, command=command)
>>>  File "C:\Python26\lib\site-packages\PIL\Image.py", line 2123, in _show
    apply(_showxv, (image,), options)
>>>  File "C:\Python26\lib\site-packages\PIL\Image.py", line 2127, in _showxv
    apply(ImageShow.show, (image, title), options)
>>>  File "C:\Python26\lib\site-packages\PIL\ImageShow.py", line 41, in show
    if viewer.show(image, title=title, **options):
>>>  File "C:\Python26\lib\site-packages\PIL\ImageShow.py", line 66, in show
    self.show_image(image, **options)
>>>  File "C:\Python26\lib\site-packages\PIL\ImageShow.py", line 85, in show_image
    return self.show_file(self.save_image(image), **options)
>>>  File "C:\Python26\lib\site-packages\PIL\ImageShow.py", line 81, in save_image
    return image._dump(format=self.get_format(image))
>>>  File "C:\Python26\lib\site-packages\PIL\Image.py", line 493, in _dump
    self.save(file, format)
>>>  File "C:\Python26\lib\site-packages\PIL\Image.py", line 1439, in save
    save_handler(self, fp, filename)
>>>  File "C:\Python26\lib\site-packages\PIL\BmpImagePlugin.py", line 242, in _save
    ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, stride, -1))])
>>>  File "C:\Python26\lib\site-packages\PIL\ImageFile.py", line 498, in _save
    e.setimage(im.im, b)
>>>SystemError: tile cannot extend outside image

我可以看到“区域”大小已设为 (0,0),但我不明白为什么。

任何帮助都将非常感谢

【问题讨论】:

    标签: python python-imaging-library paste crop


    【解决方案1】:

    crop 方法的PIL documentation 声明:

    从 当前图像。盒子是一个 4 元组 定义左、上、右和 较低的像素坐标。

    这是一个惰性操作。更改为 源图像可能是也可能不是 反映在裁剪的图像中。要得到 一个单独的副本,调用load方法 在裁剪后的副本上。

    因此,您应该尝试 region = House.crop(box).load() 以确保您得到的是实际裁剪后的副本。

    更新:
    实际上,上面的方法似乎只在您使用 PIL 1.1.6 及更高版本时才有效。在此之前的版本中,我猜 load() 不会返回任何内容,因此您无法链接操作。在这种情况下,请使用:

    region = House.crop(box)
    region.load()
    

    【讨论】:

    • 感谢您的帮助。我已经输入了您的建议,然后是 region.show(),现在收到错误消息: Traceback (most recent call last): File "", line 1, in region.show() AttributeError: 'NoneType' 对象没有属性 'show'
    【解决方案2】:

    我遇到了一个我似乎无法解决的类似错误,但我随后意识到它与传递给 Image.crop() 的参数有关。您可以看到图像的大小为 (0,0),因此没有可显示的内容。您正在设置从点 (25,25) 到 (25,25) 的界限。

    如果您需要 25x25 的裁剪图像(从左上角开始): ``` >

    >> import Image
    >>> grey = Image.new('RGB', (200, 200), "grey")
    >>> House = Image.open("House01.jpg")
    >>> print grey.size, grey.mode, grey.format
    >>>(200, 200) RGB None
    >>> print House.size, House.mode, House.format
    >>>(300, 300) RGB JPEG
    >>> box = (0, 0, 25, 25)
    >>> House.crop(box)
    >>>Image._ImageCrop image mode=RGB size=0x0 at 0x11AD210>
    >>> region = House.crop(box)
    >>> region.show()
    

    ``` 如果您想从中心或其他点开始,我会使用此link 作为参考:

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 2010-10-24
      • 2022-01-23
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 2019-10-31
      相关资源
      最近更新 更多