Coder Social home page Coder Social logo

quickcompressimages's Introduction

quickCompressImages

基于ImageMagick,批量(当前文件夹及子文件夹)处理图片压缩方法

快速压缩图片方法(小白篇)

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%

quickcompressimages's People

Contributors

zplufb avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.