Silly script: Converting a picture to document-friendly size

I needed to convert quite a few pictures taken with my phone which automatically uploads all its pictures to Dropbox folder. I remember from the old days that there were some “Save for web…” options in some of the graphic manipulation tools, but I just couldn’t find anything small and neat while googling.

So, a quick check on the fantastic function convert from ImageMagick and mission accomplished – sort of.

This is my small script that takes a filename (one of the pictures) and converts it to a comparatively low-resolution format that fits well to a 14-cm wide frame in my document. No need for scaling and adjustments (unless you have portrait/landscape rotations to take into account) while pasting the files into your document.

Run it like this:

Datan:/some/cool/path> \
"Dropbox/Camera Uploads/2013-06-26 09.05.54.jpg"

Executing ... --> Number of horizontal pixels: 396.7
... leaving

Datan:/some/cool/path> ls -lastr /tmp/user/
... 253883 Jun 27 11:01 2013-06-26 09.05.54.jpg.tiff

Which in this case, with the default settings, results in a reduction from 1.3 MB to 250kB.

So here it is – and someone probably has a one-liner for this instead… let us know in that case. And no, it does not work for windows and yes, I do not care.


# Takes an arbitrary (?) picture and converts it into a neat
# format to be used in A4-portrait documents, e.g.,
# openoffice/libreoffice.
# ImageMagick is required to have this up and running, as well
# as bc.
# The function could be extended to also take a list of input
# files. And one could of course make it more user friendly
# with flags to control the paper width, resolution, etc.
# 2013-06-26
# A, JJ Wikner,

# ==========================================================
# ==========================================================

funcName=`basename $0`
echo "Executing ${funcName} ... "

if [ $# -lt 1 ]
echo " You should specify a file name '$funcName {arg}'!"
echo "... leaving ${funcName}."
exit 1

# Strip the file into its components for pretty-printing,
# etc. Might not be needed as such.


# Then the parameters.
PAPERWIDTH=14 # Width in cm (gasp, Europeans! ththth)
DENSITY=72 # Chosen resolution (DPI)

# Find the values required for the convert function
PAPERINCH=`echo "${PAPERWIDTH} / 2.541" | bc -l`
WPIX=`echo "${PAPERINCH} * ${DENSITY}" | bc`
echo " ${funcName} --> Number of horizontal pixels: $WPIX"

# Then execute the convert function.
# Notice that the width of the picture is only taken into
# account.
# We also make sure to only reduce the size of pictures
# and not increase them in case they were smaller than
# 14 cm from the beginning. The "\>" flag is handling this.

# The file is sent to the tmp area and inherits most of
# the basename of the input file name.

convert \
"${fileName}" \
-bordercolor white \
-density ${DENSITY} \
-resize ${WPIX}x\> \

echo "... leaving ${funcName}."

# ==========================================================
# ==========================================================


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s