使用 awk 模糊图像

使用 awk 模糊图像

我正在尝试模糊图像。我正在使用转换命令将图像格式从 .bmp 更改为 pbm,然后再次更改为 bmp

我的想法是 for 循环图像的每个像素,计算当前像素和局部像素的平均值并分配平均值

不幸的是,如果我尝试将图像转换为 bmp,则会出现错误:

convert: improper image header `newFile.ppm' @ error/pnm.c/ReadPNMImage/282.
convert: no images defined `newFile.bmp' @ error/convert.c/ConvertImageCommand/3210.

我的脚本如下所示:GNU nano 2.5.3 文件:blur.sh

#!/bin/sh

name=$(echo $1 | cut -d'.' -f1)
echo $name
name="$name.ppm"
echo $name
awk 'BEGIN {FS=" "}
/^.*/{
    for(i=0; i < NF -5; i++) {
        avarage=(($i + $($i+1) + $($i+2))/3)
        printf $avarage" "
        if(i > 100) {printf $i >> "newFile.ppm" }
        else {
            printf $avarage" " >> "newFile.ppm"
            printf $avarage" " >> "newFile.ppm"
            printf $avarage" " >> "newFile.ppm"
        }
    }
}' $name

答案1

awk是一个糟糕的图像处理工具。

该问题源于未将正确的 PPM 图像标头写入输出文件。您还忽略输入文件中的标头。如果 PPM 图像不是类型P3(ASCII 编码的 RGB)、P2(ASCII 编码的灰度图)或P1(ASCII 编码的黑白),您也无法像通常读取文本文件一样读取它(因为它将是二进制文件) )。

该标头的格式描述如下:有关 Netpbm 格式的维基百科文章。文章中给出的例子是

P3
3 2
255
# The part above is the header
# "P3" means this is a RGB color image in ASCII
# "3 2" is the width and height of the image in pixels
# "255" is the maximum value for each color
# The part below is image data: RGB triplets
255   0   0     0 255   0     0   0 255
255 255   0   255 255 255     0   0   0

请注意,在此示例中,每行有 3 个像素,尽管本文后面的示例显示换行符并不重要,并且像素的 RGB 值可以标准化:

同一张图片:

P3
# The same image with width 3 and height 2,
# using 0 or 1 per color (red, green, blue)
3 2 1
1 0 0   0 1 0   0 0 1
1 1 0   1 1 1   0 0 0

同一张图片:

P3 3 2 1  1 0 0   0 1 0   0 0 1  1 1 0   1 1 1   0 0 0

此外,循环会忽略每行的最后三个 RGB 值(如果图像是灰度或黑白,则忽略最后三个像素)。

相关内容