Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to convert screenshots to JPG format System
I was asked for a string of commands to take a desktop picture (in PDF format) and convert it to a JPG, so here's the script:
#!/bin/sh
/usr/sbin/screencapture theimage.pdf;
/usr/bin/sips -s format jpeg theimage.pdf -o theimage.jpg;
/bin/rm theimage.pdf;
The first line (after the #!/bin/sh declaration) takes a screen shot named theimage.pdf; the second line converts it to a JPG and saves it as theimage.jpg; and the third line removes the original PDF (theimage.pdf).

Make the script executable by typing:
chmod +x /path/to/saved/script
and run it with:
/path/to/script
[robg adds: If you want a generic script to handle converting any PDF to a JPEG, probably the best bet is to use the built-in Folder Action Script that converts images to JPGs -- with a couple minor edits, as described in this hint. Just take a screenshot and drag and drop it into the folder to which you've attached the script, and you're done ... quite useful!]
    •    
  • Currently 2.00 / 5
  You rated: 2 / 5 (6 votes cast)
 
[18,287 views]  

A script to convert screenshots to JPG format | 11 comments | Create New Account
Click here to return to the 'A script to convert screenshots to JPG format' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to convert screenshots to JPG format
Authored by: prodok on Apr 07, '04 12:52:16PM

Nothing against the script, but I would be very cautious to convert screenshots to JPEG format. Screenshots are normally relatively low on the number of colors, and contain bigger areas of the same color, as well as high local contrasts. The JPEG format on the other hand has been developed for photographic images with many different colors and small areas of the same color and low local contrasts.

An image containing high contrasts and even colored areas will produce very ugly artefacts when converted to JPEG. And this may not really be what was wanted. It is normally more recommended to use a lossless file format, and then reduce the number of colors so that indexed colors can be used. A good example for such a format would be GIF, but TIFFs can be used in a same way.



[ Reply to This | # ]
A script to convert screenshots to JPG format
Authored by: carsten on Sep 28, '04 09:01:21PM

PNG is a lossless format which realizes the best compression compared to GIF and TIFF and it supports more than 256 colors.

Thanks to this hint we now have a way to quickly convert screen snapshots from PDF to PNG.

/usr/bin/sips -s format png Picture\ 1.pdf -o Picture\ 1.png;

(Gives, "Warning: Output file format does not support saving some properties" but the resulting PNG looks fine to me.)

Before, I would have to open each one with Preview, export as PNG, and change the popup from JPEG to PNG every single time because the app isn't smart enough to remember the last selected export format. Worse still is that Preview isn't even AppleScript-able. BAH.

Thanks for this hint, I didn't even know that /usr/bin/sips existed!

#!/bin/sh
# This file: ~/bin/sipscon or pdf2png

# Set your preferred output format here.
# png tiff jpeg gif
format=png

# Don't touch anything below.
NOTENOUGHARGS=0
E_BADARGS=65
if [ $# = "$NOTENOUGHARGS" ]
then
  me=`basename $0`
  echo "Converts images to ${format} format via /usr/bin/sips."
  echo "Usage: $me [input files]"
  echo "       $me *.pdf"
  echo "       $me Picture\ 1.pdf"
  echo "       $me \"Picture 1.tiff\" Picture\ 2.gif"
  exit $E_BADARGS
fi

# Delimit with quotes to allow for filenames with spaces!
for filename in "$@"
do
  echo "Converting ${filename} to ${format} format: \
    ${filename%.*}.${format}"
  /usr/bin/sips -s format ${format} "${filename}" \
    -o "${filename%.*}.${format}"
  echo
done
echo "Done."

# Reference:
# http://www.tldp.org/LDP/abs/html/refcards.html#AEN15979


[ Reply to This | # ]
A script to convert screenshots to JPG format
Authored by: carsten on Sep 28, '04 09:06:29PM

For some reason, the \ character did not show up in the HTML of the above script.

Line 18 should read:
echo " $me \"Picture 1.tiff\" Picture\ 2.gif"

Line 25 should read:
echo "Converting ${filename} to ${format} format: \

Line 27 should read:
/usr/bin/sips -s format ${format} "${filename}" \

Sorry for the confusion.



[ Reply to This | # ]
A script to convert screenshots to JPG format
Authored by: gdanko on Oct 18, '04 06:41:54PM

[baracus:~] gdanko% screencapture ./screen.pdf
[baracus:~] gdanko% sips -s format png screen.pdf -o screen.png
/Volumes/Vandelay/Users/gdanko/screen.pdf
Error: An error -50 occured
/Volumes/Vandelay/Users/gdanko/screen.png



[ Reply to This | # ]
A script to convert screenshots to JPG format
Authored by: derrickbass on Apr 07, '04 01:43:44PM

I would take out the rm command. As it is written, if something goes wrong with the conversion, the original file will be deleted anyway. Alternative, you can check the return value of the sips command and only execute the rm command if it succeeds.



[ Reply to This | # ]
A script to convert screenshots to JPG format
Authored by: Rainy Day on Oct 25, '05 12:11:07PM
So the second and third lines should be combined something like this then:
/usr/bin/sips -s format jpeg theimage.pdf -o theimage.jpg && /bin/rm theimage.pdf;
The && between commands executes the second command only if the first completes without error.

[ Reply to This | # ]
Can't a .plist be modified to change this?
Authored by: GORDYmac on Apr 07, '04 02:56:54PM

In earlier OSX versions, you could change a .plist file to change the saved format of screen captures. Has that gone away?



[ Reply to This | # ]
Can't a .plist be modified to change this?
Authored by: carsten on Sep 28, '04 07:51:22PM

Yes, in 10.3 command-shift-3 and command-shift-4 screen captures are now always in PDF format. You used to be able to modify this behaviour with TinkerTool. I don't understand the logic behind Apple's decision to do this.



[ Reply to This | # ]
A script to convert screenshots to JPG format
Authored by: jamesg on Apr 07, '04 06:07:02PM

I agree with the earlier comment - it's not very smart to convert a screenshot to JPG. It's a lossy compression scheme.

For most people who take screenshots often and use them in manuals and the like, it's better in the long run to (gasp) keep a copy as a PDF or capture it in TIFF or PICT in the first place. In this one scenario, I wish Apple didn't force the PDF format on us.



[ Reply to This | # ]
A script to convert screenshots to JPG format
Authored by: GaelicWizard on Apr 07, '04 10:37:02PM

The Grab.app program only "grabs" screen shots, and only in TIFF format. It does not even support PDF at all, so you're not being forced into using PDF. ;-) In fact, the only way to get a shot of just one window (AFAIK) is to use Grab. The shift-command-3/4 buttons are really only useful (IMHO) for quick, over AIM, "Check it out!" shots.

---
Pell



[ Reply to This | # ]
A script to convert screenshots to JPG format
Authored by: dpetrie on Apr 08, '04 08:37:20AM

Actually you can get a single window capture by using shift-command-4 then press the spacebar after you see the capture crosshair. You are now in capture window mode. Spacebar toggles between window and area mode.



[ Reply to This | # ]