How to batch compress image sizes in Linux

Source: Internet
Author: User
Tags imagemagick
How to compress images on the Internet on windows is easy and there are many software, but is there no way to compress images on linux? This article will share with you a powerful free image tool ImageMagick. if you are interested, you can understand that mobile phone users are very large and you have to consider the traffic for mobile phone users, however, the network bandwidth of mobile phones is small. it is difficult to compress images on the internet. it is easy to use windows and there are many software. But is there any way to compress images on linux?
After several searches, I found some methods. here I will share with you:

ImageMagick, a powerful free image tool that can be used with command lines

How to install and use Centos:
Yum install ImageMagick

Enter y to confirm the installation.

Next, call the system command.

Copy codeThe code is as follows:
# Include
Int main ()
{
Printf ("Please install ImageMagick before run this programme, else it will not run correctlyn ");
System ("mkdir small ");
System ("cp *. jpg./small ");
System ("find./small-name '*. jpg'-exec convert-resize 300 × 300 {}{}\;");
System ("pwd ");
System ("rename. jpg _small.jpg small/*. jpg ");
Printf ("nConvert pictures finished, plesae check itn ");
Return 0;
}

The resize parameter in convert can be used to modify imagemagick. it can convert the image format, compress the image size, and add a watermark, without the need to write a third-party program, you only need to write a simple SHELL script to compress the image. next we will introduce it on UBUNTU:
Installation Command:
Sudo apt-get install imagemagick

For other linux systems, there are corresponding software installation commands. the following two scripts are used to perform batch format and compression operations on images:

The following is an example of converting an image in [bB] [mM] [pP] (all BMP files) format to a jpg file that occupies a small space:

Copy codeThe code is as follows:
#! /Bin/sh
For img in 'find./-name '*. [bB] [mM] [pP]'; do
# 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 "/paipai_basename=.jpg ";
# Do convert
Convert $ img $ _ basefullname;
# Remove bmp file
Rm $ img;
Echo "deal $ _ basefullname successfully ";
Done

Image compression script:

Copy codeThe code is as follows:
For img in 'find./-name '*. [jJ] [pP] [gG]'; do
Convert-resize 85% * 85% $ img-resized;
Rm $ img;
Mv $ img-resized $ img
Echo $ img
Done
For img in 'find./-name '*. [pP] [nN] [gG]'; do
Convert-resize 85% * 85% $ img-resized;
Rm $ img;
Mv $ img-resized $ img
Echo $ img
Done
For img in 'find./-name '*. [gG] [iI] [fF]'; do
Convert-resize 85% * 85% $ img-resized;
Rm $ img;
Mv $ img-resized $ img
Echo $ img
Done
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.