使用Bash将所有图像设置为横向布局

3
在文件浏览器或Shotwell中,一些图像显示为纵向模式,而另一些则显示为横向模式。但是,identify命令无法区分它们:
横向:
IMG_0064.JPG JPEG 3648x2736 3648x2736+0+0 8-bit DirectClass 3.319MB 0.000u 0:00.000

肖像:

IMG_0108.JPG JPEG 3648x2736 3648x2736+0+0 8-bit DirectClass 3.004MB 0.000u 0:00.000

我使用以下脚本来获取我的图片的宽度和高度: 批量裁剪和调整大小以创建缩略图 有没有办法也获得方向?
------------------------------------------------------------------------------------------
我想要批量裁剪和调整大小以创建缩略图(解决方案),如果我在池中得到一些纵向图像,它会将它们旋转。
完整解决方案:
#! /bin/bash
for img in *.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

    orientation=$(identify -format '%[exif:orientation]' $img)
        if (( orientation > 1 )) ; then # crop horizontally
        echo "$img is portrait"
        name="temp"
        convert -rotate 90 "$img" "$name"
        mv "$img" "portrait_$img"
        mv "$name" "$img"
    fi

    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 -auto-orient "$img" -crop "$new_width"x$new_height+$left+$top -resize 120x80 thumb-"$img"
done
2个回答

4

您可以在使用convert转换图像时添加-auto-orient选项来自动旋转图像。

如果您只需要获取方向信息,则必须在identify上使用格式说明符,例如:

identify -format '%[exif:orientation]' image_file.jpg

更多详情请参见ImageMagick文档中关于数字照片方向的部分。 (链接)

4
尝试使用convert工具中的-orient-auto-orient标志。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接