【问题标题】:Crop the border of an image using PIL使用 PIL 裁剪图像的边框
【发布时间】:2013-03-06 15:50:11
【问题描述】:

如何使用 PIL 裁剪图像的边框?

来自这样的图片

我想做这个

谢谢。

【问题讨论】:

  • 您可能需要查找 PIL 的“图像裁剪”,看看这个答案:stackoverflow.com/a/9983361/532471
  • 你打算在什么基础上裁剪图像???
  • Geekfish,不,我不知道指向数字的边界在哪里。我只想从图像中剪切数字。

标签: python python-imaging-library


【解决方案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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-01
    • 2012-04-16
    • 1970-01-01
    • 2012-12-22
    • 2022-01-23
    • 1970-01-01
    • 2011-02-25
    • 2018-12-11
    相关资源
    最近更新 更多