Batch converting jpegs into thumbnail images
Batch converting jpegs into thumbnail images
am 23.09.2007 17:32:15 von tuxedo
This is not primarely a unix shell question but more of an Image Magick
question. However, perhaps someone has the relevant information here.
I use a simple shell procedure to batch convert large jpeg images of
varying dimensions, into thumbnails of maximum width 75 pixels.
From within the directory containing the originals, I simply run this:
for i in *.jpg
do convert -quality 70 -geometry x75 $i ../thumbs/`basename $i .jpg`.jpg;
done
For those who don't already know, convert makes copies of the images in 70%
compression quality, specifying the height in pixels by -geometry x75, and
placing the resulting thumbnails in the thumbs directory.
In addition to the above, I would like the procedure to do the following:
1. For resulting thumbnails that happens to be wider than 100 pixels, to
crop or shave the left and right edges equally (or to the nearest pixel) so
the thumbs with otherwise wider dimensions also become 100 pixels wide.
2. If the resulting thumb would be less than 100 pixels wide, to add a
solid padding of any color (eg. #cccccc) equally on left or right, so they
also become 100 pixels wide, including the padding naturally.
This is like simply placing the ?? x 75 pixels images produced by the
convert loop in the center of a 100x75 pixels canvas, while discarding or
cropping excess areas on the left and right in otherwise wider images.
Does someone have a procedure which can easily achieve this or know how it
may best be done using Image Magick with some suitable loop?
Re: Batch converting jpegs into thumbnail images
am 23.09.2007 18:09:04 von Cyrus Kriticos
Tuxedo wrote:
> This is not primarely a unix shell question but more of an Image Magick
> question.
>
> 1. For resulting thumbnails that happens to be wider than 100 pixels, to
> crop or shave the left and right edges equally (or to the nearest pixel) so
> the thumbs with otherwise wider dimensions also become 100 pixels wide.
>
> 2. If the resulting thumb would be less than 100 pixels wide, to add a
> solid padding of any color (eg. #cccccc) equally on left or right, so they
> also become 100 pixels wide, including the padding naturally.
>
> This is like simply placing the ?? x 75 pixels images produced by the
> convert loop in the center of a 100x75 pixels canvas, while discarding or
> cropping excess areas on the left and right in otherwise wider images.
width=$(identify YOUR_IMAGE.JPG)
width=${width%x*}
width=${width##* }
echo $width
http://redux.imagemagick.org/script/command-line-tools.php
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Batch converting jpegs into thumbnail images
am 23.09.2007 18:44:14 von tuxedo
Cyrus Kriticos wrote:
> Tuxedo wrote:
> > This is not primarely a unix shell question but more of an Image Magick
> > question.
> >
> > 1. For resulting thumbnails that happens to be wider than 100 pixels, to
> > crop or shave the left and right edges equally (or to the nearest pixel)
> > so the thumbs with otherwise wider dimensions also become 100 pixels
> > wide.
> >
> > 2. If the resulting thumb would be less than 100 pixels wide, to add a
> > solid padding of any color (eg. #cccccc) equally on left or right, so
> > they also become 100 pixels wide, including the padding naturally.
> >
> > This is like simply placing the ?? x 75 pixels images produced by the
> > convert loop in the center of a 100x75 pixels canvas, while discarding
> > or cropping excess areas on the left and right in otherwise wider
> > images.
>
> width=$(identify YOUR_IMAGE.JPG)
> width=${width%x*}
> width=${width##* }
> echo $width
>
> http://redux.imagemagick.org/script/command-line-tools.php
>
Thanks for the tip. I understand it returns the width of the given image.
You mean the best solution to this would be to identify the width of
each image and then calculate/add padding on left and right or crop to size?
Re: Batch converting jpegs into thumbnail images
am 23.09.2007 18:51:41 von Cyrus Kriticos
Tuxedo wrote:
> Thanks for the tip. I understand it returns the width of the given image.
> You mean the best solution to this would be to identify the width of
> each image and then calculate/add padding on left and right or crop to size?
yes
e.g.
http://redux.imagemagick.org/script/command-line-options.php #crop
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Batch converting jpegs into thumbnail images
am 23.09.2007 19:17:56 von tuxedo
Cyrus Kriticos wrote:
[...]
> yes
>
> e.g.
> http://redux.imagemagick.org/script/command-line-options.php #crop
>
Thanks for the info, at least I know the way it should be done.
Could you or someone here perhaps print out a short example of how such
shell script may be done, i.e. size identification, calculation to add or
remove x-number of pixels, when working from within a directory of images
in a unix bash or similar environment, and finally, placing the processed
thumbnail images in another directory, all within one loop?
Any pointers would be greatly appreciated for this otherwise highly complex
task for a non-seasoned shell scripter....
Re: Batch converting jpegs into thumbnail images
am 23.09.2007 20:31:24 von Cyrus Kriticos
Tuxedo wrote:
>
> Thanks for the info, at least I know the way it should be done.
>
> Could you or someone here perhaps print out a short example of how such
> shell script may be done, i.e. size identification, calculation to add or
> remove x-number of pixels, when working from within a directory of images
> in a unix bash or similar environment, and finally, placing the processed
> thumbnail images in another directory, all within one loop?
--- cut here ---
#!/bin/bash
# dirs without trailing /
SOURCE_DIR=/tmp/images
CONVERTED_DIR=/tmp/converted
CROPED_DIR="$CONVERTED_DIR"
MAX_WIDTH=100
HEIGHT=75
cd "$SOURCE_DIR" && for I in *.jpg; do
echo -n "shrink $I..."
convert -quality 70 -geometry x${HEIGHT} $I ${CONVERTED_DIR}/${I}
echo ok
WIDTH=$(identify "${CONVERTED_DIR}/${I}")
WIDTH="${WIDTH%x*}"
WIDTH="${WIDTH##* }"
if [ "$WIDTH" -gt "$MAX_WIDTH" ]; then
echo -n "crop ${I} from width $WIDTH to ${MAX_WIDTH}..."
X_OFFSET=$((($WIDTH-MAX_WIDTH)/2))
convert -crop ${MAX_WIDTH}x${HEIGHT}+${X_OFFSET}+0 \
"${CONVERTED_DIR}/${I}" "${CROPED_DIR}/${I}"
echo ok
fi
done
--- cut here ---
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Batch converting jpegs into thumbnail images
am 23.09.2007 22:34:45 von tuxedo
Cyrus Kriticos wrote:
[...]
> --- cut here ---
> #!/bin/bash
>
> # dirs without trailing /
> SOURCE_DIR=/tmp/images
> CONVERTED_DIR=/tmp/converted
> CROPED_DIR="$CONVERTED_DIR"
>
> MAX_WIDTH=100
> HEIGHT=75
>
> cd "$SOURCE_DIR" && for I in *.jpg; do
> echo -n "shrink $I..."
> convert -quality 70 -geometry x${HEIGHT} $I ${CONVERTED_DIR}/${I}
> echo ok
>
> WIDTH=$(identify "${CONVERTED_DIR}/${I}")
> WIDTH="${WIDTH%x*}"
> WIDTH="${WIDTH##* }"
>
> if [ "$WIDTH" -gt "$MAX_WIDTH" ]; then
> echo -n "crop ${I} from width $WIDTH to ${MAX_WIDTH}..."
> X_OFFSET=$((($WIDTH-MAX_WIDTH)/2))
> convert -crop ${MAX_WIDTH}x${HEIGHT}+${X_OFFSET}+0 \
> "${CONVERTED_DIR}/${I}" "${CROPED_DIR}/${I}"
> echo ok
> fi
> done
> --- cut here ---
>
I could never have achieved this myself in such short time! Your script
crops the images exactly as needed. However, it does not add padding to
images which are less than 100 pixels in width (I presume you left this as
an exercise for someone else). Anyway, the added padding is probably
something that is more suitable solved by CSS methods on an HTML level,
which is the end medium on which the thumbs will finally be displayed.
That your script echoes the progress which images are cropped and by how
many pixels, adds the entertainment factor in watching this script work.
Thank you!
Re: Batch converting jpegs into thumbnail images
am 23.09.2007 23:58:22 von cfajohnson
On 2007-09-23, Cyrus Kriticos wrote:
....
> width=$(identify YOUR_IMAGE.JPG)
> width=${width%x*}
> width=${width##* }
width=$(identify -format %w YOUR_IMAGE.JPG)
Or, if you need both height and width:
eval "$( identify -format "width=%w height=%h" )"
See the identify man page for the other format specifications.
> echo $width
>
> http://redux.imagemagick.org/script/command-line-tools.php
>
--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Re: Batch converting jpegs into thumbnail images
am 24.09.2007 14:21:53 von Michal Nazarewicz
Tuxedo writes:
> I use a simple shell procedure to batch convert large jpeg images of
> varying dimensions, into thumbnails of maximum width 75 pixels.
>
> From within the directory containing the originals, I simply run this:
>
> for i in *.jpg
> do convert -quality 70 -geometry x75 $i ../thumbs/`basename $i .jpg`.jpg;
> done
>
> In addition to the above, I would like the procedure to do the following:
>
> 1. For resulting thumbnails that happens to be wider than 100 pixels, to
> crop or shave the left and right edges equally (or to the nearest pixel) so
> the thumbs with otherwise wider dimensions also become 100 pixels wide.
>
> 2. If the resulting thumb would be less than 100 pixels wide, to add a
> solid padding of any color (eg. #cccccc) equally on left or right, so they
> also become 100 pixels wide, including the padding naturally.
Not tested but maybe:
convert -quality 70 -geometry x75 "$i" \
-background '#CCC' -page 100x75+0+0 " ../thumbs/$i"
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +------ooO--(_)--Ooo--
Re: Batch converting jpegs into thumbnail images
am 24.09.2007 17:12:18 von tuxedo
Michal Nazarewicz wrote:
[...]
> convert -quality 70 -geometry x75 "$i" \
> -background '#CCC' -page 100x75+0+0 " ../thumbs/$i"
>
Thanks! I'll test this as well as the helpful tip posted by Chris F.A.
Johnson. The width expansion can be useful for something. However, the
solution posted by Cyrus Kriticos actually fullfills the purpose right now.
Re: Batch converting jpegs into thumbnail images
am 24.09.2007 20:58:17 von Cyrus Kriticos
Tuxedo wrote:
> Cyrus Kriticos wrote:
>
> [...]
>
> Your script
> crops the images exactly as needed. However, it does not add padding to
> images which are less than 100 pixels in width (I presume you left this as
> an exercise for someone else).
Bugfix and added padding to samller images:
--- cut here ---
#!/bin/bash
SOURCE_DIR=/tmp/images
CONVERTED_DIR=/tmp/converted
MAX_WIDTH=100
HEIGHT=75
cd "$SOURCE_DIR" && for I in *.jpg; do
echo -n "$I..."
convert -quality 70 -geometry x${HEIGHT} $I ${CONVERTED_DIR}/${I}
echo -n shrinked
WIDTH=$(identify "${CONVERTED_DIR}/${I}")
WIDTH="${WIDTH%x*}"
WIDTH="${WIDTH##* }"
if [ "$WIDTH" -gt "$MAX_WIDTH" ]; then
X_OFFSET=$((($WIDTH-$MAX_WIDTH)/2))
convert -quality 70 -crop ${MAX_WIDTH}x${HEIGHT}+${X_OFFSET}+0
"${CONVERTED_DIR}/${I}" "${CONVERTED_DIR}/${I}"
echo " & cropped from width $WIDTH to ${MAX_WIDTH}"
else
X_OFFSET=$((($MAX_WIDTH-$WIDTH)/2))
convert -quality 70 -thumbnail ${MAX_WIDTH}x${HEIGHT} -extent
${MAX_WIDTH}x${HEIGHT} -roll +${X_OFFSET}+0 "$I" "${CONVERTED_DIR}/${I}"
echo " & added border"
fi
done
--- cut here ---
It's not perfect, maybe someone has a one-liner.
tags: convert image images jpeg jpegs gif gifs thumb thumbs resize border
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Batch converting jpegs into thumbnail images
am 24.09.2007 20:59:20 von Cyrus Kriticos
Tuxedo wrote:
> Cyrus Kriticos wrote:
>
> [...]
>
> Your script
> crops the images exactly as needed. However, it does not add padding to
> images which are less than 100 pixels in width (I presume you left this as
> an exercise for someone else).
Bugfix and added padding to smaller images:
--- cut here ---
#!/bin/bash
SOURCE_DIR=/tmp/images
CONVERTED_DIR=/tmp/converted
MAX_WIDTH=100
HEIGHT=75
cd "$SOURCE_DIR" && for I in *.jpg; do
echo -n "$I..."
convert -quality 70 -geometry x${HEIGHT} $I ${CONVERTED_DIR}/${I}
echo -n shrinked
WIDTH=$(identify "${CONVERTED_DIR}/${I}")
WIDTH="${WIDTH%x*}"
WIDTH="${WIDTH##* }"
if [ "$WIDTH" -gt "$MAX_WIDTH" ]; then
X_OFFSET=$((($WIDTH-$MAX_WIDTH)/2))
convert -quality 70 -crop ${MAX_WIDTH}x${HEIGHT}+${X_OFFSET}+0
"${CONVERTED_DIR}/${I}" "${CONVERTED_DIR}/${I}"
echo " & cropped from width $WIDTH to ${MAX_WIDTH}"
else
X_OFFSET=$((($MAX_WIDTH-$WIDTH)/2))
convert -quality 70 -thumbnail ${MAX_WIDTH}x${HEIGHT} -extent
${MAX_WIDTH}x${HEIGHT} -roll +${X_OFFSET}+0 "$I" "${CONVERTED_DIR}/${I}"
echo " & added border"
fi
done
--- cut here ---
It's not perfect, maybe someone has a one-liner.
tags: convert image images jpeg jpegs gif gifs thumb thumbs resize border
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Batch converting jpegs into thumbnail images
am 24.09.2007 21:02:42 von Cyrus Kriticos
Tuxedo wrote:
> Cyrus Kriticos wrote:
>
> [...]
>
> Your script
> crops the images exactly as needed. However, it does not add padding to
> images which are less than 100 pixels in width (I presume you left this as
> an exercise for someone else).
Bugfix and added padding to smaller images:
--- cut here ---
#!/bin/bash
SOURCE_DIR=/tmp/images
CONVERTED_DIR=/tmp/converted
MAX_WIDTH=100
HEIGHT=75
cd "$SOURCE_DIR" && for I in *.jpg; do
echo -n "$I..."
convert -quality 70 -geometry x${HEIGHT} $I ${CONVERTED_DIR}/${I}
echo -n shrinked
WIDTH=$(identify "${CONVERTED_DIR}/${I}")
WIDTH="${WIDTH%x*}"
WIDTH="${WIDTH##* }"
if [ "$WIDTH" -gt "$MAX_WIDTH" ]; then
X_OFFSET=$((($WIDTH-$MAX_WIDTH)/2))
convert -quality 70 -crop ${MAX_WIDTH}x${HEIGHT}+${X_OFFSET}+0
"${CONVERTED_DIR}/${I}" "${CONVERTED_DIR}/${I}"
echo " & cropped from width $WIDTH to ${MAX_WIDTH}"
else
X_OFFSET=$((($MAX_WIDTH-$WIDTH)/2))
convert -quality 70 -thumbnail ${MAX_WIDTH}x${HEIGHT} -extent
${MAX_WIDTH}x${HEIGHT} -roll +${X_OFFSET}+0 "$I" "${CONVERTED_DIR}/${I}"
echo " & added border"
fi
done
--- cut here ---
It's not perfect, maybe someone has a one-liner.
tags: convert image images jpeg jpegs gif gifs thumb thumbs resize border
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Batch converting jpegs into thumbnail images
am 24.09.2007 21:06:02 von Cyrus Kriticos
Tuxedo wrote:
> Cyrus Kriticos wrote:
>
> [...]
>
> Your script
> crops the images exactly as needed. However, it does not add padding to
> images which are less than 100 pixels in width (I presume you left this as
> an exercise for someone else).
Bugfix and added padding to smaller images:
--- cut here ---
#!/bin/bash
SOURCE_DIR=/tmp/images
CONVERTED_DIR=/tmp/converted
MAX_WIDTH=100
HEIGHT=75
cd "$SOURCE_DIR" && for I in *.jpg; do
echo -n "$I..."
convert -quality 70 -geometry x${HEIGHT} $I ${CONVERTED_DIR}/${I}
echo -n shrinked
WIDTH=$(identify "${CONVERTED_DIR}/${I}")
WIDTH="${WIDTH%x*}"
WIDTH="${WIDTH##* }"
if [ "$WIDTH" -gt "$MAX_WIDTH" ]; then
X_OFFSET=$((($WIDTH-$MAX_WIDTH)/2))
convert -quality 70 -crop ${MAX_WIDTH}x${HEIGHT}+${X_OFFSET}+0 \
"${CONVERTED_DIR}/${I}" "${CONVERTED_DIR}/${I}"
echo " & cropped from width $WIDTH to ${MAX_WIDTH}"
else
X_OFFSET=$((($MAX_WIDTH-$WIDTH)/2))
convert -quality 70 -thumbnail ${MAX_WIDTH}x${HEIGHT} -extent \
${MAX_WIDTH}x${HEIGHT} -roll +${X_OFFSET}+0 "$I" \
"${CONVERTED_DIR}/${I}"
echo " & added border"
fi
done
--- cut here ---
It's not perfect, maybe someone has a one-liner.
tags: convert image images jpeg jpegs gif gifs thumb thumbs resize border
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Batch converting jpegs into thumbnail images
am 24.09.2007 21:10:24 von Cyrus Kriticos
Tuxedo wrote:
> Cyrus Kriticos wrote:
>
> [...]
>
> Your script
> crops the images exactly as needed. However, it does not add padding to
> images which are less than 100 pixels in width (I presume you left this as
> an exercise for someone else).
Bugfix and added padding to smaller images:
--- cut here ---
#!/bin/bash
SOURCE_DIR=/tmp/images
CONVERTED_DIR=/tmp/converted
MAX_WIDTH=100
HEIGHT=75
cd "$SOURCE_DIR" && for I in *.jpg; do
echo -n "$I..."
convert -quality 70 -geometry x${HEIGHT} $I ${CONVERTED_DIR}/${I}
echo -n shrinked
WIDTH=$(identify "${CONVERTED_DIR}/${I}")
WIDTH="${WIDTH%x*}"
WIDTH="${WIDTH##* }"
if [ "$WIDTH" -gt "$MAX_WIDTH" ]; then
X_OFFSET=$((($WIDTH-$MAX_WIDTH)/2))
convert -quality 70 -crop ${MAX_WIDTH}x${HEIGHT}+${X_OFFSET}+0 \
"${CONVERTED_DIR}/${I}" "${CONVERTED_DIR}/${I}"
echo " & cropped from width $WIDTH to ${MAX_WIDTH}"
else
X_OFFSET=$((($MAX_WIDTH-$WIDTH)/2))
convert -quality 70 -thumbnail ${MAX_WIDTH}x${HEIGHT} -extent \
${MAX_WIDTH}x${HEIGHT} -roll +${X_OFFSET}+0 "$I" \
"${CONVERTED_DIR}/${I}"
echo " & added border"
fi
done
--- cut here ---
It's not perfect, maybe someone has a one-liner.
tags: convert image images jpeg gif thumbnail resize border
--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Re: Batch converting jpegs into thumbnail images
am 25.09.2007 14:18:41 von tuxedo
Cyrus Kriticos wrote:
[...]
> It's not perfect, maybe someone has a one-liner.
Thanks for posting this latest version, however, it does the job perfectly.
A one-liner would not make any difference IMO, at least not for the purpose
which I will use it.
It does inspire a more elaborate idea, i.e. a Swiss army thumbnail knife.
Such stool could have command line options (or arguments) to crop or pad on
left and right until the image fits into the given canvas, as it currently
does with a fixed height. Or/and crop from top and bottom until an image
fits into the given proportions based on the width of the thumb.
Another option could be to override the default and fit the entire image
into the given space of the thumb, adding padding on top and bottom or left
and right; an extended option to this could be to specify x-number of
pixels padding between the actual photo edges and the edges of the jpeg
image, whether on top/bottom or left/right, whereas the image would
otherwise bleed. This may be compared to placing a smaller sheet of paper
(the image) on top of and in the center of a larger black sheet of paper.
Anyway, these were just some quick ideas, I've not thought of in any detail.
Re: Batch converting jpegs into thumbnail images
am 26.09.2007 21:58:32 von Michal Nazarewicz
> Michal Nazarewicz wrote:
>> convert -quality 70 -geometry x75 "$i" \
>> -background '#CCC' -page 100x75+0+0 " ../thumbs/$i"
Tuxedo writes:
> Thanks! I'll test this as well as the helpful tip posted by Chris F.A.
> Johnson. The width expansion can be useful for something. However, the
> solution posted by Cyrus Kriticos actually fullfills the purpose right
> now.
It's pretty complex though. Generally, you may find the following site
useful: http://www.imagemagick.org/Usage/
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +------ooO--(_)--Ooo--