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

Time:09-17

I can convert an image into pixel coordinate and colors by

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
...

how can I

  1. just get the pixel coordinate and hex columns in 1 line without line break in a .txt file? like

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

  2. convert the text file back to an image file? something like

    convert pixel.txt image.png

CodePudding user response:

You can do that in ImageMagick using Awk and other Unix commands as follows:

convert myimage.png txt: | tail -n  2  | awk '{print $1 $3}' | 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

Example:

convert lena.png[10x10 0 0] txt: | tail -n  2  | awk '{print $1 $3}' | 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 

ADDITION

If you want to go back to an image from the list, then put the list into a string variable. Then echo the string and pipe to sed to change ",#" to " #". Then pipe back to convert where you can use -sparse-color voronoi to paint it over a black background image. If you do not have every pixel to fill out the background image, then it will use the voronoi interpolation to fill in.

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

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

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

  • Related