Imagemagick 在mini_magick中检测透明像素

Imagemagick 在mini_magick中检测透明像素,imagemagick,minimagick,Imagemagick,Minimagick,我有一个已知尺寸的图像,比如8x8。我需要知道是否有任何非透明像素周围的外边缘像素的图像使用MiniMagick明确。我也想知道如何在ImageMagick中实现这一点,如果可能的话(为了我自己的理解),但我现在只需要在Minimagik中实现这一点 如果任何人都有一半的答案,请随意在这个问题上抛出一些东西。我也愿意玩弄概念。谢谢 我会这样做。用完全透明的虚无填充内部的非边缘像素-一个洞,如果你喜欢的话。然后,图像中剩余的任何不透明度都必须是边缘像素中某些不透明度的结果。因此,如果我们将新创建的

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


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

我会这样做。用完全透明的虚无填充内部的非边缘像素-一个洞,如果你喜欢的话。然后,图像中剩余的任何不透明度都必须是边缘像素中某些不透明度的结果。因此,如果我们将新创建的图像覆盖在任何背景之上,并导致背景改变,则可以推断出新图像中必须有非透明像素,因此在新图像的边缘中,我们已经确定中间没有透明像素。听起来比实际情况更难

#!/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

tmp.png

因此,现在我们在白色背景上覆盖一个洞,然后将结果与纯白色图像进行比较,并计算有多少像素不同

convert -size 16x16 xc:white tmp.png -composite \
        xc:white -metric AE -compare -format "%[distortion]" info:
60
顶部有60=>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
# ImageMagick pixel enumeration: 16,16,255,srgba
如果您不喜欢像我这样将
15
s硬编码为
awk
,当然,您可以从
convert
输出的第一行中选择宽度和高度,如下所示:

# 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
# ImageMagick pixel enumeration: 16,16,255,srgba
并用这些行的最后一行/列创建一个搜索模式-如果您不知道如何执行,只需询问:

awk 'NR==1                  {... pattern="0,|,0:..." }   # First line, get dimensions, make pattern
     $0 ~ pattern && !/,0)/ {print "..."; exit}'         # Find non-transparent edge pixels