本文介绍了批量裁剪并调整图像大小以创建缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一大堆jpg图像,我想为其创建缩略图。图像都具有不同的大小和分辨率,但我希望所有缩略图都具有标准尺寸,例如120x80px。但是,我不想拉伸图像。所以我想做以下事情:

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:


  1. 将图像裁剪为1.5:1的宽高比。将裁剪区域居中(即左右切割等量,或高于和低于

  2. 将图像大小调整为120 x 80像素。

是否有linux命令这样做?我查看了imagemick转换,但我无法弄清楚如何进行居中裁剪。似乎你必须手动指定每个图像的裁剪区域?

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?

推荐答案

这适用于大于120x80的图像。未在小图像上测试,但你应该能够调整它。

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

这篇关于批量裁剪并调整图像大小以创建缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:30