以编程方式检测图像是否有边框(返回布尔值) [英] Detect if an image has a border, programmatically (return boolean)

查看:233
本文介绍了以编程方式检测图像是否有边框(返回布尔值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我看过这篇文章。 如何以编程方式检测图片边框?
他似乎

First off, I have read this post. How to detect an image border programmatically? He seems to be asking a slightly different question, on finding the X/Y coordinates though.

我只是试图找到一个坚实的给定照片周围存在边框。
我使用ImageMagick探索,但这是最好的选择吗?
我从来没有做过任何与图像相关的编程,所以我希望有一个简单的API,可以解决这个问题。
我也是相当新的如何使用这些库,所以任何建议是赞赏。
我喜欢Python或Java的解决方案,但是任何东西都很好。

I am just trying to find whether or not a solid border exists around a given photo. I've explored using ImageMagick, but is this the best option? I've never done any Image-related programming so I'm hoping there's just a simple api out there that can solve this problem. I'm also fairly new to how to use these libraries, so any advice is appreciated. I'd prefer solutions in Python or Java, anything is fine though.

谢谢!

推荐答案

我回答了一个相关问题这里,即可移除图片周围的任何边框,因此会使用 PIL 。您可以轻松调整代码,使其返回 True False 是否存在边框,如下所示:

I answered a related question here, that removes any border around an image, it uses PIL. You can easily adapt the code so it returns True or False for whether there is a border or not, like this:

from PIL import Image, ImageChops

def is_there_a_border(im):
    bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
    diff = ImageChops.difference(im, bg)
    diff = ImageChops.add(diff, diff, 2.0, -100)
    bbox = diff.getbbox()
    return bbox != (0,0,im.size[0],im.size[1])

但是,即使只有图像的一边,也会返回 True 有边框。但它听起来像你想知道是否有一个边界的图像周围。要将最后一行改为:

However, this will return True even if only one side of an image has a border. But it sounds like you want to know if there is a border all the way around an image. To do that change the last line to:

    return all((bbox[0], bbox[1], (bbox[0] + bbox[2]) <= im.size[0], 
                                  (bbox[1] + bbox[3]) <= im.size[1]))

如果每边都有边框,则只返回true。

This only returns true if there is a border on every side.

例如:

False:

img src =https://i.stack.imgur.com/onOju.jpgalt =enter image description here>

False:

True:

img src =https://i.stack.imgur.com/C126v.jpgalt =输入图片说明here>>

这篇关于以编程方式检测图像是否有边框(返回布尔值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆