Alex (yakshaver) wrote,

Graphic arts clue - followup

The other day I asked, essentially, how I could turn

 this:   into this: 
Needless to say, I figured it out.

I was just about ready to spring for a copy of Photoshop Elements when I decided to ask on zephyr, where tlyu and jhawk reminded me of the existence of Netpbm, which I hadn't thought about in so long I initially didn't remember having ever known about it in the first place. The important thing here being not Netpbm itself,* but being reminded of the existence of the ASCII versions of the PNM formats. I'd been thinking before I posted here in the first place there really ought to be a graphics format where each pixel is stored in hex RGB triplets. But that failed to make me remember the ASCII PNM format, which stores them in decimal RGB triplets.

Once I had that piece of the puzzle, I knew I could do what I wanted without having to learn photoshop or any other GUI program — indeed, without even having to install anything, since I already had ImageMagick. Which isn't to say that this approach doesn't have its own learning curve — but I started this one already well up Mont Blanc, whereas with photoshop I'd be starting within sight of the the Dead Sea.

convert -compress none featurebox2_bg.gif featurebox2_bg.pnm
cat featurebox2_bg.pnm \
| sed 's/\([0-9][0-9]* \)\([0-9][0-9]* \)\([0-9][0-9]*\)/\3 \2\1/g' \
> featurebox2_blue_bg.pnm
convert featurebox2_blue_bg.pnm featurebox2_blue_bg.gif

For the non-unix geeks among you, what this does is, first, convert the GIF file to an uncompressed PNM file, in which each pixel is represented as a string of three numbers; each number is between 0 and 255, and represents the amount of red, green, and blue, respectively, in that pixel's color. For example, some of the dark red pixels toward the lower right of the original are represented as "157 42 42". Being represented as text lets me use standard unix text manipulation tools to edit it. In this case, I use sed, the stream editor, to swap the red and blue value in each triplet: "157 42 42" becomes "42 42 157"** — what was red is now blue. And finally, I convert back to GIF.)

(And for the unix geeks: I'd like to have been able to do it in a single pipeline, without the intermediary .pnm files, but couldn't figure out how, given that convert uses the extension of the output file argument to determine the output format. Can it in fact be done?)

* Netpbm is in theory capable of doing a tremendous variety of image manipulations; in practice I found that even the "Super Stable" release would not build without errors on either Mac OS X or Ubuntu. It's too bad it doesn't have better (which I suspect would be any) release engineering: unlike ImageMagick, it embraces the the Unix way, and building a single pipeline, without temporary files, would have been trivial.

** If you're trying to actually parse the sed line, it actually turns "157 42 42" into "42 42 157 " (note the trailing space). The extra space is not significant, so it wasn't worth writing a more complex regular expression to get rid of it.

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded