使用imagemagick调整图像大小

使用imagemagick调整图像大小,imagemagick,Imagemagick,我有数百张图片,它们的宽度和高度可能会有所不同。我想根据以下要求调整所有尺寸: 使它们更小,这样最大的一面应该是2560px,或者最短的一面应该是1600px。现在我正在使用以下代码: 用于ls中的文件;转换$file-调整“2560>”的大小-新建$file;完成 用于ls中的文件;转换$file-调整“1600^>”的大小-新建$file;完成 最大侧不得小于2560或最短侧不得小于1600px 这将是可怕的作物额外的空间,所以我的最终图像应该是2560x1600(横向)或1600x2560

我有数百张图片,它们的宽度和高度可能会有所不同。我想根据以下要求调整所有尺寸:

  • 使它们更小,这样最大的一面应该是2560px,或者最短的一面应该是1600px。现在我正在使用以下代码:

    用于
    ls
    中的文件;转换$file-调整“2560>”的大小-新建$file;完成

    用于
    ls
    中的文件;转换$file-调整“1600^>”的大小-新建$file;完成

  • 最大侧不得小于2560或最短侧不得小于1600px

  • 这将是可怕的作物额外的空间,所以我的最终图像应该是2560x1600(横向)或1600x2560(纵向)

  • 例如,如果我的图像是4000x3000,我可能会得到2560x1920或2133x1600。我想保留2560x1920,并从顶部和底部裁剪160像素,以获得2560x1600

    我现在使用的代码是:

    for i in `ls`; do convert $i -resize '2560x1600^' -gravity center -crop '2560x1600+0+0' new-$i; done 
    

    但是如果我的图像是3000x4000(纵向模式),我可能会得到2560x3413,然后它会一直裁剪到我得到2560x1600,我想要1600x2560。

    我建议您使用这样的脚本来获得每个图像的尺寸。然后,您可以根据图像大小实现任何逻辑,也可以避免解析
    ls
    的输出,这通常被认为是一个坏主意

    #!/bin/bash
    
    # Ignore case, and suppress errors if no files 
    shopt -s nullglob
    shopt -s nocaseglob
    
    # Process all image files
    for f in *.gif *.png *.jpg; do
    
        # Get image's width and height, in one go
        read w h < <(identify -format "%w %h" "$f")
    
        if [ $w -eq $h ]; then
            echo $f is square at ${w}x${h}
        elif [ $h -gt $w ]; then
            echo $f is taller than wide at ${w}x${h}
        else
            echo $f is wider than tall at ${w}x${h}
        fi
    
    done
    

    我建议您使用这样的脚本来获取每个图像的尺寸。然后,您可以根据图像大小实现任何逻辑,也可以避免解析
    ls
    的输出,这通常被认为是一个坏主意

    #!/bin/bash
    
    # Ignore case, and suppress errors if no files 
    shopt -s nullglob
    shopt -s nocaseglob
    
    # Process all image files
    for f in *.gif *.png *.jpg; do
    
        # Get image's width and height, in one go
        read w h < <(identify -format "%w %h" "$f")
    
        if [ $w -eq $h ]; then
            echo $f is square at ${w}x${h}
        elif [ $h -gt $w ]; then
            echo $f is taller than wide at ${w}x${h}
        else
            echo $f is wider than tall at ${w}x${h}
        fi
    
    done
    

    我建议您使用这样的脚本来获取每个图像的尺寸。然后,您可以根据图像大小实现任何逻辑,也可以避免解析
    ls
    的输出,这通常被认为是一个坏主意

    #!/bin/bash
    
    # Ignore case, and suppress errors if no files 
    shopt -s nullglob
    shopt -s nocaseglob
    
    # Process all image files
    for f in *.gif *.png *.jpg; do
    
        # Get image's width and height, in one go
        read w h < <(identify -format "%w %h" "$f")
    
        if [ $w -eq $h ]; then
            echo $f is square at ${w}x${h}
        elif [ $h -gt $w ]; then
            echo $f is taller than wide at ${w}x${h}
        else
            echo $f is wider than tall at ${w}x${h}
        fi
    
    done
    

    我建议您使用这样的脚本来获取每个图像的尺寸。然后,您可以根据图像大小实现任何逻辑,也可以避免解析
    ls
    的输出,这通常被认为是一个坏主意

    #!/bin/bash
    
    # Ignore case, and suppress errors if no files 
    shopt -s nullglob
    shopt -s nocaseglob
    
    # Process all image files
    for f in *.gif *.png *.jpg; do
    
        # Get image's width and height, in one go
        read w h < <(identify -format "%w %h" "$f")
    
        if [ $w -eq $h ]; then
            echo $f is square at ${w}x${h}
        elif [ $h -gt $w ]; then
            echo $f is taller than wide at ${w}x${h}
        else
            echo $f is wider than tall at ${w}x${h}
        fi
    
    done
    

    似乎需要一个脚本。 这是我在前面评论中提出的解决方案:

    #!/bin/bash
    # Variables for resize process:
    shortest=1600;
    longest=2560;
    # Ignore case, and suppress errors if no files 
    shopt -s nullglob
    shopt -s nocaseglob
    
    # Process all image files
    for f in *.gif *.png *.jpg; do
    
    # Get image's width and height, in one go
    read w h < <(identify -format "%w %h" "$f")
    
    if [ $w -eq $h ]; then
         convert $f -resize "${shortest}x${shortest}^" -gravity center -crop "${shortest}x${shortest}+0+0" new-$f
    elif [ $h -gt $w ]; then
         convert $f -resize "${shortest}x${longest}^" -gravity center -crop "${shortest}x${longest}+0+0" new-$f
    else
         convert $f -resize "${longest}x${shortest}^" -gravity center -crop "${longest}x${shortest}+0+0" new-$f
    fi
    
    done
    
    #/bin/bash
    #调整大小过程的变量:
    最短=1600;
    最长=2560;
    #忽略大小写,如果没有文件,则抑制错误
    shopt-s nullglob
    shopt-s nocaseglob
    #处理所有图像文件
    对于*.gif*.png*.jpg中的f;做
    #一次性获得图像的宽度和高度
    
    阅读w h<似乎需要一个脚本。 这是我在前面评论中提出的解决方案:

    #!/bin/bash
    # Variables for resize process:
    shortest=1600;
    longest=2560;
    # Ignore case, and suppress errors if no files 
    shopt -s nullglob
    shopt -s nocaseglob
    
    # Process all image files
    for f in *.gif *.png *.jpg; do
    
    # Get image's width and height, in one go
    read w h < <(identify -format "%w %h" "$f")
    
    if [ $w -eq $h ]; then
         convert $f -resize "${shortest}x${shortest}^" -gravity center -crop "${shortest}x${shortest}+0+0" new-$f
    elif [ $h -gt $w ]; then
         convert $f -resize "${shortest}x${longest}^" -gravity center -crop "${shortest}x${longest}+0+0" new-$f
    else
         convert $f -resize "${longest}x${shortest}^" -gravity center -crop "${longest}x${shortest}+0+0" new-$f
    fi
    
    done
    
    #/bin/bash
    #调整大小过程的变量:
    最短=1600;
    最长=2560;
    #忽略大小写,如果没有文件,则抑制错误
    shopt-s nullglob
    shopt-s nocaseglob
    #处理所有图像文件
    对于*.gif*.png*.jpg中的f;做
    #一次性获得图像的宽度和高度
    
    阅读w h<似乎需要一个脚本。 这是我在前面评论中提出的解决方案:

    #!/bin/bash
    # Variables for resize process:
    shortest=1600;
    longest=2560;
    # Ignore case, and suppress errors if no files 
    shopt -s nullglob
    shopt -s nocaseglob
    
    # Process all image files
    for f in *.gif *.png *.jpg; do
    
    # Get image's width and height, in one go
    read w h < <(identify -format "%w %h" "$f")
    
    if [ $w -eq $h ]; then
         convert $f -resize "${shortest}x${shortest}^" -gravity center -crop "${shortest}x${shortest}+0+0" new-$f
    elif [ $h -gt $w ]; then
         convert $f -resize "${shortest}x${longest}^" -gravity center -crop "${shortest}x${longest}+0+0" new-$f
    else
         convert $f -resize "${longest}x${shortest}^" -gravity center -crop "${longest}x${shortest}+0+0" new-$f
    fi
    
    done
    
    #/bin/bash
    #调整大小过程的变量:
    最短=1600;
    最长=2560;
    #忽略大小写,如果没有文件,则抑制错误
    shopt-s nullglob
    shopt-s nocaseglob
    #处理所有图像文件
    对于*.gif*.png*.jpg中的f;做
    #一次性获得图像的宽度和高度
    
    阅读w h<似乎需要一个脚本。 这是我在前面评论中提出的解决方案:

    #!/bin/bash
    # Variables for resize process:
    shortest=1600;
    longest=2560;
    # Ignore case, and suppress errors if no files 
    shopt -s nullglob
    shopt -s nocaseglob
    
    # Process all image files
    for f in *.gif *.png *.jpg; do
    
    # Get image's width and height, in one go
    read w h < <(identify -format "%w %h" "$f")
    
    if [ $w -eq $h ]; then
         convert $f -resize "${shortest}x${shortest}^" -gravity center -crop "${shortest}x${shortest}+0+0" new-$f
    elif [ $h -gt $w ]; then
         convert $f -resize "${shortest}x${longest}^" -gravity center -crop "${shortest}x${longest}+0+0" new-$f
    else
         convert $f -resize "${longest}x${shortest}^" -gravity center -crop "${longest}x${shortest}+0+0" new-$f
    fi
    
    done
    
    #/bin/bash
    #调整大小过程的变量:
    最短=1600;
    最长=2560;
    #忽略大小写,如果没有文件,则抑制错误
    shopt-s nullglob
    shopt-s nocaseglob
    #处理所有图像文件
    对于*.gif*.png*.jpg中的f;做
    #一次性获得图像的宽度和高度
    
    我不太清楚你想做什么。例如,如果您的图像是4000x3000,您可能会得到2560x1920或2133x1600。您想保留2560x1920并从顶部和底部裁剪160像素以获得2560x1600,还是想保留2133x1600并在左侧和右侧添加213和214像素的空白(黑色?),以便以这种方式获得2560x1600?谢谢。你怎么看它“收成太多”?你是不是把太多的图像剪成了原始图像?生成的图像不是2560x1600吗?它是从另一个维度裁剪的还是不成比例的?横向图片可以,但不是纵向图片:如果我的图像是3000x4000(纵向模式),我可能会得到2560x3413,然后它会裁剪到2560x1600,我想要1600x2560。我不太清楚你想要做什么。例如,如果您的图像是4000x3000,您可能会得到2560x1920或2133x1600。您想保留2560x1920并从顶部和底部裁剪160像素以获得2560x1600,还是想保留2133x1600并在左侧和右侧添加213和214像素的空白(黑色?),以便以这种方式获得2560x1600?谢谢。你怎么看它“收成太多”?你是不是把太多的图像剪成了原始图像?生成的图像不是2560x1600吗?它是从另一个维度裁剪的还是不成比例的?横向图片可以,但不是纵向图片:如果我的图像是3000x4000(纵向模式),我可能会得到2560x3413,然后它会裁剪到2560x1600,我想要1600x2560。我不太清楚你想要做什么。例如,如果您的图像是4000x3000,您可能会得到2560x1920或2133x1600。您想保留2560x1920并从顶部和底部裁剪160像素以获得2560x1600,还是想保留2133x1600并在左侧和右侧添加213和214像素的空白(黑色?),以便以这种方式获得2560x1600?谢谢。你怎么看它“收成太多”?你是不是把太多的图像剪成了原始图像?生成的图像不是2560x1600吗?它是从另一个维度裁剪的还是不成比例的?横向图片可以,但不是纵向图片:如果我的图像是3000x4000(纵向模式),我可能会得到2560x3413,然后它会裁剪到2560x1600,我想要1600x2560。我不太清楚你想要做什么。例如,如果您的图像是4000x3000,您可能会得到2560x1920或2133x1600。是否要保留2560x1920并从顶部和底部裁剪160像素以获得2560x