如何使用 imagemagick 将图像转换为像素十六进制颜色的文本文件,然后再转换回图像?

How to use imagemagick to convert an image into text file of pixel hex color then back to an image?

我可以通过

将图像转换为像素坐标和颜色
convert myimage.png txt:

...
7,20: (200,251,251,255)  #C8FBFBFF  srgba(200,251,251,1)
8,20: (200,251,251,255)  #C8FBFBFF  srgba(200,251,251,1)
9,20: (200,251,251,255)  #C8FBFBFF  srgba(200,251,251,1)
10,20: (200,251,251,255)  #C8FBFBFF  srgba(200,251,251,1)
11,20: (200,251,251,255)  #C8FBFBFF  srgba(200,251,251,1)
12,20: (200,251,251,255)  #C8FBFBFF  srgba(200,251,251,1)
13,20: (200,251,251,255)  #C8FBFBFF  srgba(200,251,251,1)
14,20: (0,0,0,255)  #000000FF  black
15,20: (133,81,20,255)  #855114FF  srgba(133,81,20,1)
16,20: (0,0,0,255)  #000000FF  black
...

我怎么能

  1. 只在 1 行中获取像素坐标和十六进制列而无需在 .txt 文件中换行?喜欢

    7,20,#C8FBFBFF 8,20,#C8FBFBFF 9,20,#C8FBFBFF...

  2. 将文本文件转换回图像文件?像

    convert pixel.txt image.png

您可以在 ImageMagick 中使用 Awk 和其他 Unix 命令执行此操作,如下所示:

convert myimage.png txt: | tail -n +2  | awk '{print  }' | tr ":" "," | tr "\n" " "

The tail skips the first line.
The awk prints the first and third string separated by spaces
The first tr changes : to ,
The second tr changes new lines \n to simple spaces

示例:

convert lena.png[10x10+0+0] txt: | tail -n +2  | awk '{print  }' | tr ":" "," | tr "\n" " "

0,0,#E2897C 1,0,#E08982 2,0,#E18779 3,0,#E48679 4,0,#E38A7D 5,0,#E2877A 6,0,#E18672 7,0,#DF8776 8,0,#DD836E 9,0,#DE8976 0,1,#E2897C 1,1,#E08983 2,1,#E18779 3,1,#E48679 4,1,#E38A7E 5,1,#E2877B 6,1,#E18672 7,1,#DF8777 8,1,#DD836F 9,1,#DE8A76 0,2,#E28A7C 1,2,#E0887F 2,2,#E18778 3,2,#E48679 4,2,#E38979 5,2,#E38577 6,2,#E08572 7,2,#E08573 8,2,#DD836D 9,2,#DE8774 0,3,#E4897A 1,3,#E18672 2,3,#E18676 3,3,#E58470 4,3,#E38571 5,3,#E38675 6,3,#E28169 7,3,#E07E69 8,3,#E0816D 9,3,#E0826C 0,4,#E0826D 1,4,#DF846E 2,4,#E08474 3,4,#E28370 4,4,#E28675 5,4,#E18473 6,4,#E0846C 7,4,#E27F6C 8,4,#E37F6C 9,4,#E0836C 0,5,#DF8268 1,5,#DF856D 2,5,#E2836C 3,5,#E27C6B 4,5,#E1836E 5,5,#E2836E 6,5,#E1836E 7,5,#E2826C 8,5,#E38169 9,5,#E1816E 0,6,#E2836F 1,6,#E2836F 2,6,#E3836C 3,6,#E27F6D 4,6,#E1836E 5,6,#E1846B 6,6,#E38168 7,6,#E2826B 8,6,#E3846C 9,6,#E08370 0,7,#E4846D 1,7,#E2846E 2,7,#E2836C 3,7,#E38469 4,7,#E1826C 5,7,#E18267 6,7,#E3836C 7,7,#E28169 8,7,#E2836C 9,7,#E2816F 0,8,#E38272 1,8,#E2836E 2,8,#E1836D 3,8,#E18069 4,8,#E1826A 5,8,#E38368 6,8,#E2836E 7,8,#E1826B 8,8,#E28066 9,8,#E48169 0,9,#E2826F 1,9,#E0826D 2,9,#E0846F 3,9,#E17F6A 4,9,#E28369 5,9,#E38269 6,9,#E38367 7,9,#E3856D 8,9,#E48467 9,9,#E2856B 

加法

如果要返回列表中的图像,请将列表放入字符串变量中。然后将字符串和管道回显到 sed 以将“,#”更改为“#”。然后通过管道返回转换,您可以在其中使用 -sparse-color voronoi 将其绘制在黑色背景图像上。如果你没有每个像素都去填充背景图片,那么它会使用voronoi插值来填充。

# create string from list of coordinates and hex colors
str=`convert lena.png[10x10+0+0] txt: | tail -n +2  | awk '{print  }' | tr ":" "," | tr "\n" " "`

# convert string back to image
echo $str | sed 's/,[#]/ \#/g' | convert -size 10x10 xc:black -sparse-color voronoi @- x.png

https://legacy.imagemagick.org/Usage/canvas/#sparse-color