Batch Crop and Resize Images to Create Thumbnails

Batch crop and resize images to create thumbnails

This works for images larger than 120x80. Not tested on smaller ones, but you should be able to tune it.

#! /bin/bash
for img in p*.jpg ; do
identify=$(identify "$img")
[[ $identify =~ ([0-9]+)x([0-9]+) ]] || \
{ echo Cannot get size >&2 ; continue ; }
width=${BASH_REMATCH[1]}
height=${BASH_REMATCH[2]}
let good_width=height+height/2

if (( width < good_width )) ; then # crop horizontally
let new_height=width*2/3
new_width=$width
let top='(height-new_height)/2'
left=0

elif (( width != good_width )) ; then # crop vertically
let new_width=height*3/2
new_height=$height
let left='(width-new_width)/2'
top=0
fi

convert "$img" -crop "$new_width"x$new_height+$left+$top -resize 120x80 thumb-"$img"
done

Batch crop and resize images to create thumbnails

This works for images larger than 120x80. Not tested on smaller ones, but you should be able to tune it.

#! /bin/bash
for img in p*.jpg ; do
identify=$(identify "$img")
[[ $identify =~ ([0-9]+)x([0-9]+) ]] || \
{ echo Cannot get size >&2 ; continue ; }
width=${BASH_REMATCH[1]}
height=${BASH_REMATCH[2]}
let good_width=height+height/2

if (( width < good_width )) ; then # crop horizontally
let new_height=width*2/3
new_width=$width
let top='(height-new_height)/2'
left=0

elif (( width != good_width )) ; then # crop vertically
let new_width=height*3/2
new_height=$height
let left='(width-new_width)/2'
top=0
fi

convert "$img" -crop "$new_width"x$new_height+$left+$top -resize 120x80 thumb-"$img"
done

Padded fit with easy thumbnails

Thanks for Timmy O'Mahony's suggestion of creating a processor to do the padding. Here's the code for those of you that come up against a similar issue. To get this to work, you'll need something like this in settings:

THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'common.thumbnail_processors.pad_image',
'easy_thumbnails.processors.autocrop',
'easy_thumbnails.processors.scale_and_crop',
'easy_thumbnails.processors.filters')

And then you can add this to common/thumbnail_processors.py (or wherever)

import Image

def pad_image(image, **kwargs):
""" Pad an image to make it the same aspect ratio of the desired thumbnail.
"""

img_size = image.size
des_size = kwargs['size']
fit = [float(img_size[i])/des_size[i] for i in range(0,2)]

if fit[0] > fit[1]:
new_image = image.resize((image.size[0],
int(round(des_size[1]*fit[0]))))
top = int((new_image.size[1] - image.size[1])/2)
left = 0
elif fit[0] < fit[1]:
new_image = image.resize((int(round(des_size[0]*fit[1])),
image.size[1]))
top = 0
left = int((new_image.size[0] - image.size[0])/2)
else:
return image

# For transparent
#mask=Image.new('L', new_image.size, color=0)
#new_image.putalpha(mask)

# For white
new_image.paste((255, 255, 255, 255))

new_image.paste(image, (left, top))
return new_image

How do I batch-resize images in ImageMagick while maintaining aspect ratio and a max width and height?

ImageMagick command line mogrify is likely what you want and can do what you need. It processes a folder full of images. See https://imagemagick.org/Usage/basics/#mogrify and https://imagemagick.org/script/command-line-options.php#resize. The command would like be something like the following:

Create a new directory to hold your output, say, thumbnails.
Change directory to your folder with the images

mogrify -path path_to/thumbnails -resize 488x220 *


That command will process every file in your folder. If you have other formats, such as txt or other images that you do not want to process, then modify the command to restrict to just png and jpg as follows:

mogrify -path path_to/thumbnails -resize 488x220 *.png *.jpg


add *.PNG *.JPG *.jpeg *.JPEG to the end of the line, if you have different caps and spellings for suffixes in your directory.

There are different APIs for other programming languages. See https://imagemagick.org/script/sitemap.php#program-interfaces



Related Topics



Leave a reply



Submit