zoukankan      html  css  js  c++  java
  • 快速压缩图片方法(小白篇)

    0 适用场景

    批量上传图片到云服务器(例如七牛云),但不需要用这么高清的图片。

    例如发文章在得物,小红书等平台时。

    1 前提

    2 脚本

    2.1 核心代码

    #批量把当前目录的jpg图片 分辨率下降至25%,质量下降一半成像
    #假定文件名:batchCompressImagesIncludeSubFiles5050.sh
    magick *.jpg -resize 50% -quality 50 op_%03d.jpg

    2.2 批量处理

    说明:批量处理指定文件夹及子文件夹所有图片

    2.2.1 带参数

    指定目录及分辨率及质量三个参数

    文件名:batchCompressImagesIncludeSubFilesWithParams.sh

    #!/bin/bash
    read_dir(){
        echo 'start walk through dir of'$1
        suffixJpg='.jpg'
        suffixJpeg='.jpeg'
        suffixPng='.png'
        for file in `ls -a $1`
        do
            if [ -d $1"/"$file ]
            then
                if [[ $file != '.' && $file != '..' ]]
                then
                    read_dir $1"/"$file
                fi
            else
                echo $1"/"$file
                if [[ ${file:0-4:4} == ${suffixJpg} ||  ${file:0-5:5} == ${suffixJpeg} ]]
                then
                    magick  $1"/"$file -resize $2% -quality $3%  $1"/"op_$file
                fi
                if [[ ${file:0-4:4} == ${suffixPng} ]]
                then
                    magick  $1"/"$file -resize $2% -quality $3%  $1"/"op_$file.jpg
                fi
            fi
        done
        echo 'walk complete for dir of'$1
    }
    read_dir $1 $2 $3

    2.2.1 简化版

    指定目录

    文件名:batchCompressImagesIncludeSubFiles5050.sh

    #!/bin/bash
    read_dir(){
        suffixJpg='.jpg'
        suffixJpeg='.jpeg'
        suffixPng='.png'
        for file in `ls -a $1`
        do
            if [ -d $1"/"$file ]
            then
                if [[ $file != '.' && $file != '..' ]]
                then
                    read_dir $1"/"$file
                fi
            else
                echo $1"/"$file
                if [[ ${file:0-4:4} == ${suffixJpg} ||  ${file:0-5:5} == ${suffixJpeg} ]]
                then
                    magick  $1"/"$file -resize 50% -quality 50  $1"/"op_$file
                fi
                if [[ ${file:0-4:4} == ${suffixPng} ]]
                then
                    magick  $1"/"$file -resize 50% -quality 50  $1"/"op_$file.jpg
                fi
            fi
        done
    }
    read_dir $1

    文件名:runCompressSubFiles.sh

    #.代表当前所在目录
    batchCompressImagesIncludeSubFiles5050.sh .

    3 使用及效果

    3.1 使用样例

    1. 打开git bash

    2. 输入runCompressSubFiles onedir

    3. 回车

    3.2 效果

    压缩后的图片和原图,眼睛上看差别几乎没有(由于现在手机拍照像素高,拍出来5M起)。

     

    文件名原图大小压缩后大小压缩率
    four.jpg 9282 KB 307 KB 3.31%
    one.jpg 6433 KB 364 KB 5.66%
    two.png 24846 KB 739 KB 2.97%

     

    four.jpg

     

    op_four.jpg

     

     4 参考

     

  • 相关阅读:
    android 图片全屏
    .9.png
    C++中的endl
    C++输入输出cin与cout
    word-search
    Java中的的画正三角方法
    octave中的一些基本操作
    C#中判断语句 if、if-else if、switch-case
    C#中的异常处理(try-catch的使用)——使程序更加稳定
    编程&blog处女篇-用C#求100以内的质数
  • 原文地址:https://www.cnblogs.com/fanbi/p/15181556.html
Copyright © 2011-2022 走看看