convert txt: > This is the Unix philosophy: ... Write programs to handle text streams, > because that is a universal interface. > Doug McIlroy But images do not use text format, which happens to be my problem. I need to find the top left red pixel and make a 210x320 image starting at that pixel. So I can divide the task into two parts: - Find the top left red pixel - Crop the image Someone else already did the cropping part, see https://superuser.com/a/1163824, and that leaves me with finding the top left red pixel. I could have used libpng or libjpeg, but that is format-dependent. So the only choice left is to convert it to a text format that standard command-line tools can process. After 5 minutes of web search on "imagemagick convert image to text," I got: $ convert image.png txt: 0,0: (36,147,252) #FF0000 srgb(36,147,252) 1,0: (36,147,252) #FF0000 srgb(36,147,252) 2,0: (36,147,252) #FF0000 srgb(36,147,252) 3,0: (36,147,252) #FF0000 srgb(36,147,252) 4,0: (36,147,252) #FF0000 srgb(36,147,252) 5,0: (36,147,252) #FF0000 srgb(36,147,252) 6,0: (36,147,252) #FF0000 srgb(36,147,252) 7,0: (36,147,252) #FF0000 srgb(36,147,252) 8,0: (36,147,252) #FF0000 srgb(36,147,252) ... This sed command: $ sed -n "/#FF0000/{p;q}" prints the first line that contains #FF0000 (red in RGB) and quit. To make it print the x and y coordinate of the first red pixel, I use: $ sed -n '/#FF0000/{s/^\([0-9]\+\),\([0-9]\+\)\(.*\)/\1 \2/g;p;q}' 0 0 Put it all together, I got: crop="210x320+`convert original.png | sed -n '/#FF0000/{s/^\([0-9]\+\),\([0-9]\+\)\(.*\)/\1+\2/g;p;q}'`" convert original.png -crop "$crop" cropped.png Thanks for reading.