PIL裁剪和粘贴问题:裁剪不会创建裁剪的图像 [英] PIL crop and paste problem: Cropping doesn't create a cropped image

查看:141
本文介绍了PIL裁剪和粘贴问题:裁剪不会创建裁剪的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to crop an image and then paste the cropped image into the centre of another image. Ideally I'd like the cropped image to be smaller than the image its being pasted on so that there is a border around the pasted image but I don't know if that's possible.

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

Here's what I've tried (along with the resulting error message):

>>> 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),但我不明白为什么.

I can see that the 'region' size has been made (0,0) but I can't understand why.

对此的任何帮助将非常感谢

Any help on this would be great thanks

推荐答案

PIL裁剪方法状态的文档:

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

Returns a rectangular region from the current image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

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

This is a lazy operation. Changes to the source image may or may not be reflected in the cropped image. To get a separate copy, call the load method on the cropped copy.

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

So, you should try region = House.crop(box).load() to make sure you get an actual cropped copy.

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

UPDATE:
Actually, it seems the above only works if you're using PIL 1.1.6 and later. In versions before that, I guess load() doesn't return anything so you can't chain the operations. In that case, use:

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

这篇关于PIL裁剪和粘贴问题:裁剪不会创建裁剪的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆