Graphic arts clue - followup
Apr. 13th, 2007 04:05 pmThe other day I asked, essentially, how I could turn
| this: | into this: |
![]() |
![]() |
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.


no subject
Date: 2007-04-13 08:42 pm (UTC)no subject
Date: 2007-04-14 12:22 am (UTC)no subject
Date: 2007-04-14 04:06 am (UTC)with an image format name and a colon (i.e. ps:image) or specify the
image type as the filename suffix (i.e. image.ps)." Experimentally, "convert - pnm:-" does what you're looking for...
no subject
Date: 2007-04-14 04:46 am (UTC)no subject
Date: 2007-04-14 05:12 am (UTC)shows that
does the whole thing directly, though it is somewhat cryptic - basically it produces a image with only the red channel being the original blue, then sets the blue channel of that one to the red channel of the other, leaving the result. (I would have gone with the column swapping approach, after finding that - I suspect it's a bug that
doesn't actually work :-)
no subject
Date: 2007-04-14 05:23 pm (UTC)> convert featurebox2_bg.gif \( +clone -channel R -fx B \) +swap -channel B -fx v.R featurebox2_blue.gif
Not too surprisingly, that approach preserves transparency. Given that the next thing on my agenda was learning enough about GIF transparency to re-introduce it to the files that lost it in the conversion to and from PNM, that's very handy to know. Thanks!