5

サムネイルを作成したい大量の jpg 画像があります。画像のサイズと解像度はすべて異なりますが、すべてのサムネイルを標準サイズ (120x80px など) にしたいと考えています。ただし、画像を引き伸ばしたくありません。そこで、次のようなことをしたいと思います。

  1. 画像を 1.5 : 1 の縦横比にトリミングします。トリミング領域を中央に配置します (つまり、左右または上下を同じ量だけ切り取ります)
  2. 画像のサイズを 120 x 80 ピクセルに変更します。

そうするためのLinuxコマンドはありますか?imagemick convert を調べましたが、中央のトリミングを行う方法がわかりません。各画像のトリミング領域を手動で指定する必要があるようですか?

4

3 に答える 3

6

これは、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
于 2012-12-05T23:45:30.277 に答える
1

crop-resize.py入力画像のトリミング、中央揃え、サイズ変更を行う Python スクリプトを次に示します。

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: .)

コア機能は次のとおりです。

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

@choroba さんの bash スクリプトとよく似ています。

于 2012-12-06T01:48:37.803 に答える
0

わかりました、少なくとも正方形のサムネイルで機能するものを作成できました。ただし、これを 1:5 x 1 のサムネイルに変更する方法がよくわかりません。

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
于 2012-12-05T23:35:36.680 に答える