How to resample photos

Whenever I get photos from others after a trip, the photos tend to be huge.  Who the heck needs 20 megapixels anyway?  Four is plenty for me so the first thing I want to do to make the photos easier to deal with is resample them.  I use ImageMagick’s convert for this.  convert wants a percentage reduction. Here’s how you can calculate what percentage to use:

pc = ((desiredMP / ((originalpixelcountinx * originalpixelcountiny) / 1000000.)) ** 0.5) * 100

Of course, it’s helpful if all the photos that you have are the same size.  I use jhead to see the original size of the photo, but since we’re already talking ImageMagick here, identify works as well.  So, if your original photo size was 5312×2988 and you want four megapixel output, then you calculate the percentage for convert like this:

>>> ((4 / ((5312 * 2988) / 1000000.)) ** 0.5) * 100
50.20080321285141
>>>

50% for these photos.  Then you can run a little bash script to do the conversion:

for img in `ls *.JPG`
do
  convert -sample 50% $img small/$img
  echo "$img converted"
done

The reduced photos are written into a directory called “small” that must already exist.  Now you can delete those huge photos since we’re not making billboards. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *