使用python替换图像中的白色行

萨蒂亚迪亚

我正在尝试修剪图像的整个行中除白色以外没有任何内容的部分。

我尝试使用matplot lib

  1. 将图像转换成矩阵,然后看一下(r,g,b)=(0,0,0)或(1,1,1),如果每个(r,g,b)都属于上述类型,则删除图像中的整行行

    矩阵看起来像[[[r,g,b],[r,g,b] ....]],....,[[r,g,b],[r,g,b]。 ..]]]

我达到了我的要求,但是我运行了大约500张图像,大约需要30分钟。我能以更好的方式做到吗?

实际形象

所需的图像应该像

所需的输出

编辑1:尝试使用魔杖包装中的修剪方法

with wand_img(filename=path) as i:
# i.trim(color=Color('white'))
# i.trim(color=Color('white'))
i.trim()
i.trim()
i.save(filename='output.png')

但不适用于以下类型的图像

fmw42

在Imagemagick 6.9.10.25 Q16 Mac OSX Sierra中使用两个修剪对我来说很好用。您的图像右侧有一个黑条。第一次修剪将删除它。第二次修剪将去除剩余的多余白色。您可能需要为修剪添加一些绒毛(公差)量。但是我不需要它。

输入:

在此处输入图片说明

convert img.png -trim +write tmp1.png -trim result.png

第一次修剪的结果(tmp1.png):

在此处输入图片说明

第二次修剪后的最终结果:

在此处输入图片说明

加成:

查看有关Python Wand的文档:

trim(*args, **kwargs)
Remove solid border from image. Uses top left pixel as a guide by default, or you can also specify the color to remove.

Parameters: 
color (Color) – the border color to remove. if it’s omitted top left pixel is used by default
fuzz (numbers.Integral) – Defines how much tolerance is acceptable to consider two colors as the same.


您将需要为第一个修剪指定color = black,因为此版本的修剪使用左上角进行修剪。命令行Imagemagick到处都是。如果失败,则添加一些模糊值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章