Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

python - PIL crop and paste problem: Cropping doesn't create a cropped image

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:Python26libsite-packagesPILImage.py", line 1483, in show
    _show(self, title=title, command=command)
>>>  File "C:Python26libsite-packagesPILImage.py", line 2123, in _show
    apply(_showxv, (image,), options)
>>>  File "C:Python26libsite-packagesPILImage.py", line 2127, in _showxv
    apply(ImageShow.show, (image, title), options)
>>>  File "C:Python26libsite-packagesPILImageShow.py", line 41, in show
    if viewer.show(image, title=title, **options):
>>>  File "C:Python26libsite-packagesPILImageShow.py", line 66, in show
    self.show_image(image, **options)
>>>  File "C:Python26libsite-packagesPILImageShow.py", line 85, in show_image
    return self.show_file(self.save_image(image), **options)
>>>  File "C:Python26libsite-packagesPILImageShow.py", line 81, in save_image
    return image._dump(format=self.get_format(image))
>>>  File "C:Python26libsite-packagesPILImage.py", line 493, in _dump
    self.save(file, format)
>>>  File "C:Python26libsite-packagesPILImage.py", line 1439, in save
    save_handler(self, fp, filename)
>>>  File "C:Python26libsite-packagesPILBmpImagePlugin.py", line 242, in _save
    ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, stride, -1))])
>>>  File "C:Python26libsite-packagesPILImageFile.py", line 498, in _save
    e.setimage(im.im, b)
>>>SystemError: tile cannot extend outside image

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The PIL documentation for the crop method states:

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

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.

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

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...