微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

批量裁剪和调整图像大小以创build缩略图

我有一个大的JPG图像集,我想创build缩略图。 图像都有不同的大小和分辨率,但我想所有的缩略图都有一个标准的大小,例如120x80px。 但是,我不想拉伸图像。 所以我想做一些以下的事情:

裁剪图像为1.5:1的宽高比。 以种植面积为中心(即切断左右或上下的相等数量

调整图像的大小为120 x 80像素。

有没有一个Linux命令来做到这一点? 我看着imagemick转换,但我不知道如何做中心裁剪。 看来你必须手动指定每个图像的裁剪区域?

如何在我的Linux服务器上创buildPDF文件缩略图/屏幕截图?

获取非活动的窗口缩略图预览

包括DirectShow库到Qt中的video缩略图

C#/ VB.NET中的缩略图

生成一个网页的图像(如JPG)?

这适用于大于120×80的图像。 没有在较小的测试,但你应该能够调整它。

#! /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

这是一个Python脚本crop-resize.py ,用于crop-resize.py ,调整和调整输入图像的大小:

usage: crop-resize.py [-h] [-s NN] [-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 NN,--size NN 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脚本非常相似。

好吧,我管理创建至少适用于方形缩略图的东西。 但是,我不太确定如何将其更改为1:5的缩略图

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((wh)/2,0)]+%[fx:max((hw)/2,0)]"$ -filter point -distort SRT 0 +repage -thumbnail 80 "$thumb" } mkdir thumbs for pic in *.jpg do make_thumbnail "$pic" done

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐