Tuesday, September 30, 2008

Clipboard to PNG

So, I lost my cool shortcut to take screen shots with ease on Windows, turning them into both clear and tiny images. In PNG format of course. How did I lose it? Hard disk crash. And the backup I thought I had on my home PC was on the previous hard drive, which is still OK, but in a pile somewhere, and not worth looking for. Cue the backup DVD search.

Bingo, found it on the first one - labeled "\cygwin \download \home \sfu \tmp" from 29-Dec-2007, just before the home hard drive renovation.

There it was, under /usr/local/bin, below D:\CYGWIN. And /usr/local/etc had

-rwx------+ 1 jspath ???????? 659 Aug 21 2003 colormap.ppm

Which is a Portable Pixmap file containing the magic pixels known somehere long ago as the set optimized for Netscape.

$ file /usr/local/etc/colormap.ppm
/usr/local/etc/colormap.ppm: Netpbm PPM "rawbits" image data

Anyway, that fits into the cliptopng script I wrote to go from "[prt sc]" to writing a PNG file in one swoop. Beats launching Word or MSPaint and trying to shrink the image file.

= = = =
$ cat /usr/local/bin/cliptopng

# cliptopng - write a PNG file from the Windows clipboard.

ofile=$1

/cygdrive/c/bin/winclip/winclip -p | \
/usr/local/bin/netpbm/ppmquant.exe -mapfile /usr/local/etc/colormap.ppm |\
/usr/local/bin/netpbm/ppmtogif.exe >${ofile}.gif
/usr/local/bin/gif2png.exe -O -i -p ${ofile}.gif
rm ${ofile}.gif

= = = =

There's probably a better way to do this, but it works for me.

The first step requires winclip.exe. Fortunately I had that restored under c:\bin.

The other files are from the NetPBM library, which you can get for Windows via http://www.cygwin.com/.

The gif2png.exe may be another non-NetPBM utility, necessary because I couldn't find a direct PNG writer. It may exist now, but this runs in a second or so and I'm unmotivated to change it.

Here's the Winclip license feedback:

= = = =
$ c:/bin/winclip/winclip -?
c:\bin\winclip\winclip.exe: unknown option -?
winclip - copy/Paste the Windows Clipboard. $Revision: 1.13 $
(C) Copyright 1994-2002 Diomidis D. Spinelllis. All rights reserved.

Permission to use, copy, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

usage: winclip [-w|u] -c|-p [filename]

= = = =

To restore, I just dragged the whole local directory from DVD backup to hard drive. A lot easier than clicking on SETUP.EXE, etc.

$ cliptopng stuff
ppmquant: rescaling input image to match colormap maxval
ppmquant: 216 colors found in colormap
ppmquant: mapping image to new colors...
ppmtogif: computing colormap...
ppmtogif: 148 colors found
ppmtogif: maxval is not 255 - automatically rescaling colors
7/7 99%
jspath@jspath-xplt ~

$ file stuff.png
stuff.png: PNG image data, 1024 x 738, 8-bit colormap, interlaced

$ ls -l stuff.png
-rwx------+ 1 jspath mkgroup-l-d 64789 Sep 30 15:50 stuff.png

No comments: