2013-03-18 139 views
0

如何使用PIL裁剪圖像的邊框?使用PIL裁剪圖像的邊框

從圖像這樣
Start

我想對這個
Result

感謝。

+0

你可能需要查找「圖像裁剪」爲PIL,看看這個答案:http://stackoverflow.com/a/9983361/532471 – Geekfish 2013-03-18 10:59:12

+0

憑什麼你打算裁剪圖像? ? – pradyunsg 2013-03-18 11:44:47

+0

Geekfish,不,我不知道尖號的邊界。我只想從圖像中剪切數字。 – Nolik 2013-03-18 11:50:38

回答

1
img = Image.open('your_wonderful_image.png') 
nonwhite_positions = [(x,y) for x in range(img.size[0]) for y in range(img.size[1]) if img.getdata()[x+y*img.size[0]] != (255,255,255)] 
rect = (min([x for x,y in nonwhite_positions]), min([y for x,y in nonwhite_positions]), max([x for x,y in nonwhite_positions]), max([y for x,y in nonwhite_positions])) 
img.crop(rect).save('out.png')