2015-04-08 30 views
1

我一直试图让这image I want to crop图像自动裁剪到尽可能最小的大小,删除其周围的透明位。我不能手动剪裁这个图像,因为更多的东西会被添加到图像image I want to crop as wellPNG不会autocrop使用image.getbbox()

我一直在使用这种代码:

from PIL import Image, ImageChops 

image=Image.open('headbase1.png') 
image.load() 

imageSize = image.size 
imageBox = image.getbbox() 
print(image.getbbox()) 
cropped=image.crop(imageBox) 
cropped.save('headbase_end.png') 

它不会裁掉的透明度周围,和边框就是这个(0,0,45,45),我不认为是对的。

谢谢,投票。

编辑,这个工作:Automatically cropping an image with python/PIL与该图像,但它拒绝为我的形象工作。 。

+0

numpy方法似乎也不工作。 –

回答

3

getbbox不上的PNG具有alpha通道工作:image.mode == 'RGBA'

首先拆下阿尔法信道,然后得到的边界框。 image.convert('RGB').getbbox()