ImageMagick image Batch Compression example in Linux

Source: Internet
Author: User
Tags imagemagick
The code is as follows Copy Code
sudo apt-get install ImageMagick


Other Linux has the corresponding software installation commands, I wrote the following two scripts, the pictures are batch format and compression operations:
Image format conversion, here is an example of [BB][MM][PP] (all BMP files) in the format of the file, converted to a small space-occupying JPG file:

The code is as follows Copy Code
#!/bin/sh
For IMG on ' find./-name ' *. [BB] [MM] [PP] "'; Todo
#change Upper filename to lower
_imglower= ' echo $img |tr "[: Upper:]" "[: Lower:]" ';
#get file ' s basename
_basename= ' basename $_imglower. bmp ';
#get file ' s dir
_dirname= ' dirname $img ';
#get desc filename with path
_basefullname=$_dirname "/" $_basename ". jpg";
#do Convert
Convert $img $_basefullname;
#remove BMP File
RM $img;
echo "Deal $_basefullname successfully";
Done

Picture compression script:

The code is as follows Copy Code
For IMG on ' find./-name ' *. [JJ] [PP] [GG] "'; Todo
Convert-resize 85%*85% $img $img-resized;
RM $img;
MV $img-resized $img
Echo $img
Done
For IMG on ' find./-name ' *. [PP] [NN] [GG] "'; Todo
Convert-resize 85%*85% $img $img-resized;
RM $img;
MV $img-resized $img
Echo $img
Done
For IMG on ' find./-name ' *. [GG] [II] [FF] "'; Todo
Convert-resize 85%*85% $img $img-resized;
RM $img;
MV $img-resized $img
Echo $img
Done

Method 2:

First, you install a imagemagick to perform

The code is as follows Copy Code
$sudo Apt-get Install ImageMagick

The overall command, in fact, is to query the directory of all the files, and then execute the relevant commands, I have a blog post is about the batch renaming the file, the command format is almost
Please refer to:
Method One:

The code is as follows Copy Code
For file in *. JPG; Do MV $file ' echo $file | Sed-e s/[.] Jpg/.jpg/g '; Done

The name extension is. JPG's name is lowercase jpg extension

Method Two:

The code is as follows Copy Code
for filename in *. JPG; Do MV $filename ${filename/jpg/jpg}; Done

This is another kind of substitution

Method Three:

The code is as follows Copy Code
for file in *; Do MV $file ' echo $file | Awk-f. ' {print $. ' ToLower ($)} '; Done

This enables you to modify multiple extensions at the same time, but you can only support file names that contain multiple (.) of the situation

The code is as follows Copy Code

$for img in ' Find./-name ' *. JPG "'; Do convert-quality $img ${img/jpg/jpg}; Done

In one word, the quality of the picture compressed to the original 70% of the extent of the image in the computer to see the basic will not be different, used to bulk compress pictures, upload pictures to the online most convenient.

Convert is part of the ImageMagick package,-quality represents compression quality.
$img is the original file name, change the extension of the original file name to small writing for the compressed file name (${img/jpg/jpg})

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.