2010-10-01 210 views
2

我試圖裁剪圖像,然後將裁剪後的圖像粘貼到另一圖像的中心。理想情況下,我希望裁剪後的圖像比粘貼的圖像小,以便粘貼的圖像周圍有邊框,但我不知道這是否可能。PIL裁剪和粘貼問題:裁剪不會創建裁剪圖像

這裏就是我(連同所產生的錯誤信息)的嘗試:

>>> 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),但我不明白爲什麼。

任何幫助就這將是十分感謝

回答

6

PIL documentation對作物的方法規定:

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.

所以,你應該嘗試 region = House.crop(box).load() ,以確保你得到一個實際的裁剪副本。

UPDATE:
事實上,似乎上面只有當你正在使用PIL 1.1.6及以後的作品。在此之前的版本中,我猜load()不會返回任何內容,因此您無法鏈接操作。在這種情況下,使用方法:

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

感謝您的幫助。我已經進入你的建議,其次是region.show()現在得到的錯誤信息: 回溯(最近通話最後一個): 文件 「」,1號線,在 region.show() AttributeError的: 'NoneType'對象沒有屬性'show' – Jordan 2010-10-01 11:09:20

0

我有一個類似的錯誤,我似乎無法來解決,但後來我意識到,你這樣做,它與()傳遞給Image.crop的參數做。你可以看到你的圖像的大小是(0,0),所以沒有什麼可顯示的。你正在設定從(25,25)到(25,25)的範圍。如果你想從中心或另一點,我會用開始 ``` >

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

``` :

如果你需要一個25×25裁剪圖像(從左上角開始)這個link作爲參考: