在mini_magick中检测透明像素

时间:2015-01-27 06:14:16

标签: imagemagick minimagick

我有一个已知尺寸的图像,让我们说8x8。我需要明确知道使用MiniMagick在图像的外边缘像素周围是否有任何非透明像素。我也很想知道如何在ImageMagick中做到这一点,如果可能的话(为了我自己的理解),但我现在只需要在MiniMagick中使用它。

如果有人甚至有半答案,请随意在这个问题上抛出一些东西。我也愿意玩弄概念。谢谢!

1 个答案:

答案 0 :(得分:1)

我会像这样接近它。如果您愿意,可以在内部非边缘像素中填充完全透明的虚空 - 一个洞。然后,图像中剩余的任何不透明度必须是边缘像素中的一些不透明度的结果。因此,如果我们在任何背景上覆盖新创建的图像并且导致背景更改,我们可以推断出新图像中必须存在非透明像素,因此在新图像的边缘因为我们已经确定中间没有非透明像素。这听起来比它更难!

#!/bin/bash
# First get dimensions of inner "hole", it's the width minus 2 and height minus 2
# so for a 16x16 image, it will be 14x14
inner=$(convert input.png -format "%[fx:w-2]x%[fx:h-2]" info:)

# Now punch a fully transparent hole in the input image
convert input.png                                      \ 
        -size $inner xc:none -alpha set -geometry +1+1 \
        -compose copy -composite tmp.png

<强> input.png

enter image description here

<强> tmp.png

enter image description here

所以,现在我们用白色背景上的一个孔覆盖图像,然后将结果与纯白图像进行比较,并计算出有多少像素不同。

convert -size 16x16 xc:white tmp.png -composite \
        xc:white -metric AE -compare -format "%[distortion]" info:
60

有60 =&gt;顶部16个,底部16个,每个垂直边缘14个。如果我们用完全透明的原始图像重复练习,答案将为零。因此,您需要的测试是误差度量(不同像素的数量)是否为非零。

实际上,在shell中,由于图片很小,您可能只是将图片转换为文字,让awk找到边缘像素,如下所示:

# Create opaque image
convert -size 16x16 xc:red PNG32:input.png

# Find pixels in row 0, row 15, col 0, col 15 where transparency (last digit before closing paren) is non-zero
convert input.png txt: | awk '/,0:|,15:|^0,|^15,/ && !/,0)/'
0,0: (255,0,0,1)  #FF0000  red
1,0: (255,0,0,1)  #FF0000  red
2,0: (255,0,0,1)  #FF0000  red
3,0: (255,0,0,1)  #FF0000  red
4,0: (255,0,0,1)  #FF0000  red
5,0: (255,0,0,1)  #FF0000  red
6,0: (255,0,0,1)  #FF0000  red
7,0: (255,0,0,1)  #FF0000  red
8,0: (255,0,0,1)  #FF0000  red
9,0: (255,0,0,1)  #FF0000  red
10,0: (255,0,0,1)  #FF0000  red
11,0: (255,0,0,1)  #FF0000  red
12,0: (255,0,0,1)  #FF0000  red
13,0: (255,0,0,1)  #FF0000  red
14,0: (255,0,0,1)  #FF0000  red
15,0: (255,0,0,1)  #FF0000  red
0,1: (255,0,0,1)  #FF0000  red
15,1: (255,0,0,1)  #FF0000  red
0,2: (255,0,0,1)  #FF0000  red
15,2: (255,0,0,1)  #FF0000  red
0,3: (255,0,0,1)  #FF0000  red
15,3: (255,0,0,1)  #FF0000  red
0,4: (255,0,0,1)  #FF0000  red
15,4: (255,0,0,1)  #FF0000  red
0,5: (255,0,0,1)  #FF0000  red
15,5: (255,0,0,1)  #FF0000  red
0,6: (255,0,0,1)  #FF0000  red
15,6: (255,0,0,1)  #FF0000  red
0,7: (255,0,0,1)  #FF0000  red
15,7: (255,0,0,1)  #FF0000  red
0,8: (255,0,0,1)  #FF0000  red
15,8: (255,0,0,1)  #FF0000  red
0,9: (255,0,0,1)  #FF0000  red
15,9: (255,0,0,1)  #FF0000  red
0,10: (255,0,0,1)  #FF0000  red
15,10: (255,0,0,1)  #FF0000  red
0,11: (255,0,0,1)  #FF0000  red
15,11: (255,0,0,1)  #FF0000  red
0,12: (255,0,0,1)  #FF0000  red
15,12: (255,0,0,1)  #FF0000  red
0,13: (255,0,0,1)  #FF0000  red
15,13: (255,0,0,1)  #FF0000  red
0,14: (255,0,0,1)  #FF0000  red
15,14: (255,0,0,1)  #FF0000  red
0,15: (255,0,0,1)  #FF0000  red
1,15: (255,0,0,1)  #FF0000  red
2,15: (255,0,0,1)  #FF0000  red
3,15: (255,0,0,1)  #FF0000  red
4,15: (255,0,0,1)  #FF0000  red
5,15: (255,0,0,1)  #FF0000  red
6,15: (255,0,0,1)  #FF0000  red
7,15: (255,0,0,1)  #FF0000  red
8,15: (255,0,0,1)  #FF0000  red
9,15: (255,0,0,1)  #FF0000  red
10,15: (255,0,0,1)  #FF0000  red
11,15: (255,0,0,1)  #FF0000  red
12,15: (255,0,0,1)  #FF0000  red
13,15: (255,0,0,1)  #FF0000  red
14,15: (255,0,0,1)  #FF0000  red
15,15: (255,0,0,1)  #FF0000  red

# Now create fully transparent image
convert -size 16x16 xc:none PNG32:input.png

# Find any non-opaque pixels
convert input.png txt: | awk '/,0:|,15:|^0,|^15,/ && !/,0)/'

# None

如果您不喜欢像我一样将15硬编码到awk,您当然可以从{的输出的第一行获取宽度和高度{1}}如下所示:

convert

并沿着这些行创建一个带有最后一行/ col的搜索模式 - 只要问你是否不知道该怎么做:

# ImageMagick pixel enumeration: 16,16,255,srgba