I have a large set of jpg images for which I want to create thumbnails. The images all have different sizes and resolutions, but I would like all thumbnails to have a standard size, e.g. 120x80px. However, I do not want to stretch the images. So I would like to do something of the following:
我有一大堆jpg图像,我想为其创建缩略图。图像都具有不同的大小和分辨率,但我希望所有缩略图都具有标准尺寸,例如120x80px。但是,我不想拉伸图像。所以我想做以下事情:
- Crop image to a 1.5 : 1 aspect ratio. Center the cropping area (i.e. cut off an equal amount left and right, or above and below
- Resize the image to 120 x 80 px.
将图像裁剪为1.5:1的宽高比。使裁剪区域居中(即左右切割等量,或上下切割
将图像大小调整为120 x 80像素。
Is there a linux command to do so? I looked into imagemick convert, but I can't figure out how to do the centered cropping. It seems that you have to manually specify the cropping area for each image?
是否有linux命令可以这样做?我查看了imagemick转换,但我无法弄清楚如何进行居中裁剪。您似乎必须手动指定每个图像的裁剪区域?
3 个解决方案
#1
6
This works for images larger than 120x80. Not tested on smaller ones, but you should be able to tune it.
这适用于大于120x80的图像。没有在较小的上测试,但你应该能够调整它。
#! /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
#2
1
Here's a Python script crop-resize.py
that crops, centers, and resizes input images:
这是一个Python脚本crop-resize.py,用于裁剪,居中和调整输入图像的大小:
usage: crop-resize.py [-h] [-s N N] [-q] [--outputdir DIR]
files [files ...]
Resize the image to given size. Don't strech images, crop and center
instead.
positional arguments:
files image filenames to process
optional arguments:
-h, --help show this help message and exit
-s N N, --size N N new image size (default: [120, 80])
-q, --quiet
--outputdir DIR directory where to save resized images (default: .)
The core function is:
核心功能是:
def crop_resize(image, size, ratio):
# crop to ratio, center
w, h = image.size
if w > ratio * h: # width is larger then necessary
x, y = (w - ratio * h) // 2, 0
else: # ratio*height >= width (height is larger)
x, y = 0, (h - w / ratio) // 2
image = image.crop((x, y, w - x, h - y))
# resize
if image.size > size: # don't stretch smaller images
image.thumbnail(size, Image.ANTIALIAS)
return image
It is very similar to the bash script by @choroba.
它与@choroba的bash脚本非常相似。
#3
0
Ok I managed create something that works at least for square thumbnails. However, I'm not quite sure how to change this to a 1:5 by 1 thumbnail.
好吧,我管理创建的东西至少适用于方形缩略图。但是,我不太确定如何将其更改为1:5 x缩略图。
make_thumbnail() {
pic=$1
thumb=$(dirname "$1")/thumbs/square-$(basename "$1")
convert "$pic" -set option:distort:viewport \
"%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]"$
-filter point -distort SRT 0 +repage -thumbnail 80 "$thumb"
}
mkdir thumbs
for pic in *.jpg
do
make_thumbnail "$pic"
done
#1
6
This works for images larger than 120x80. Not tested on smaller ones, but you should be able to tune it.
这适用于大于120x80的图像。没有在较小的上测试,但你应该能够调整它。
#! /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
#2
1
Here's a Python script crop-resize.py
that crops, centers, and resizes input images:
这是一个Python脚本crop-resize.py,用于裁剪,居中和调整输入图像的大小:
usage: crop-resize.py [-h] [-s N N] [-q] [--outputdir DIR]
files [files ...]
Resize the image to given size. Don't strech images, crop and center
instead.
positional arguments:
files image filenames to process
optional arguments:
-h, --help show this help message and exit
-s N N, --size N N new image size (default: [120, 80])
-q, --quiet
--outputdir DIR directory where to save resized images (default: .)
The core function is:
核心功能是:
def crop_resize(image, size, ratio):
# crop to ratio, center
w, h = image.size
if w > ratio * h: # width is larger then necessary
x, y = (w - ratio * h) // 2, 0
else: # ratio*height >= width (height is larger)
x, y = 0, (h - w / ratio) // 2
image = image.crop((x, y, w - x, h - y))
# resize
if image.size > size: # don't stretch smaller images
image.thumbnail(size, Image.ANTIALIAS)
return image
It is very similar to the bash script by @choroba.
它与@choroba的bash脚本非常相似。
#3
0
Ok I managed create something that works at least for square thumbnails. However, I'm not quite sure how to change this to a 1:5 by 1 thumbnail.
好吧,我管理创建的东西至少适用于方形缩略图。但是,我不太确定如何将其更改为1:5 x缩略图。
make_thumbnail() {
pic=$1
thumb=$(dirname "$1")/thumbs/square-$(basename "$1")
convert "$pic" -set option:distort:viewport \
"%[fx:min(w,h)]x%[fx:min(w,h)]+%[fx:max((w-h)/2,0)]+%[fx:max((h-w)/2,0)]"$
-filter point -distort SRT 0 +repage -thumbnail 80 "$thumb"
}
mkdir thumbs
for pic in *.jpg
do
make_thumbnail "$pic"
done