Conversion of PNG and JPG files in batches in Linux

Source: Internet
Author: User
Tags echo command

Conversion of PNG and JPG files in batches in Linux
In computer terms, batch processing refers to the method of executing a sequence of tasks using a non-interactive program. In this tutorial, we will use the Linux Command Line tool and provide four simple processing methods to batch convert some. PNG images into. JPG images and convert them back.

Although all examples use the convert command line tool, you can also use the mogrify command to achieve the same effect.
The convert command syntax is as follows:

$ Convert input option input file output option output file

Mogrify:

$ Mogrify option input file

Note: When you use the mogrify command, by default, the source image file will be overwritten by the converted new file. You can use clear operation options to disable overwriting, you can query the specific options on the workbook page.
The following describes how to convert all. PNG images to. JPG images in batches. If you want to convert. JPG to. PNG format, you can also use these commands and modify them as needed.

The operations are as follows:1. Use the ls and xargs commands to convert PNG and JPG files.

The ls command can list all png image files. xargsallows you to create and execute the convertcommand from the standard input, and convert all .png images to .jpg images.

----------- Convert from PNG to JPG ----------- $ ls-1 *. png | xargs-n 1 bash-c 'convert "$0" "$%0%.png%.jpg" '----------- convert from JPG to PNG ----------- $ ls-1 *. jpg | xargs-n 1 bash-c 'convert "$0" "{%0%.jpg%.png "'

The preceding Command Options are described as follows:

-1-tells ls to list the option ID of an image name in each row-n-specifies the maximum number of parameters. In this example, 1-c-Indicates bash to run the command "Running 02.16.png).jpg"-set the name of the newly converted image file, % symbol used to delete the extension of the source file


Convert PNG to JPG in Linux
I used the ls-ltr command to list all objects by the modified date and time.
Similarly, you can also use the above command to convert. JPG images to. PNG format, with only a slight adjustment.

2. Use the GNU parallel command to convert PNG and JPG files.

GNU parallel allows you to build and execute shell commands in parallel from standard input. Make sure that GNU Parallel is installed on your system. Otherwise, use the following command to install it:

$ Sudo apt-get install parallel [In Debian/Ubuntu] $ sudo yum install parallel [in RHEL/CentOS and Fedora systems]

After installing the parallel tool, you can run the following command to convert all. PNG images from the standard input to. JPG images.

----------- Convert from PNG to JPG ----------- $ parallel convert '{} ''{.}.jpg ':::*. png ----------- convert from JPG to PNG ----------- $ parallel convert '{} ''mirrors .w.png ':::*. jpg

Where:

{}-The input line replacement character, instead of the complete row read from the input source. {.}-Remove the input line with the extension. :-Specifies the symbol of the input source, that is, the command line in the preceding example. png or jpg is a command parameter.


Parallel command-convert all PNG images to JPG format
Alternatively, you can use the ls and parallel commands to convert all images in batches ,:

----------- Convert from PNG to JPG ----------- $ ls-1 *. png | parallel convert '{} ''{.}.jpg' ----------- convert from JPG to PNG ----------- $ ls-1 *. jpg | parallel convert '{}' '{.}.png'
3. Use the for loop command to convert PNG and JPG

To avoid the hassle of writing shell scripts, You can execute the for loop statement from the command line, as shown below:

----------- Convert from PNG to JPG ----------- $ bash-c 'for image in *. png; do convert "$ image" "Your images image%.png%.jpg"; echo "image $ image converted to your images image%.png%.jpg "; done '----------- convert from JPG to PNG ----------- $ bash-C' for image in *. jpg; do convert "$ image" "Your images image%.jpg%.png"; echo "image $ image converted to your images image%.jpg%.png"; done'

Description of the Option parameters used in the preceding command:

-C allows the execution of loop statements included in single quotes. The image variable is the number of image names in the directory. For each conversion operation, the echo command notifies the user that the png image has been converted to the jpg format, and vice versa. The ${image%.png%.jpg statement creates the name of the converted image. % indicates the extension of the source image file.

For Loop statement-convert from PNG to JPG format

4. Use the Shell script to convert PNG and JPG

If you don't want to mess up your command line as in the previous example, you can write a small script, as shown below:
Note: The. PNG and. jpg extensions can be switched from one format to another as shown in the following example:

#!/bin/bash#convertfor image in *.png; doconvert "$image" "${image%.png}.jpg"echo “image $image converted to ${image%.png}.jpg ”doneexit 0

Save the above script as the convert. shconvert. sh file, make the script file executable, and then execute it from the directory where the image file is stored.

$ chmod +x convert.sh$ ./convert.sh


Batch image conversion using Shell scripts
In short, we introduced some important methods to convert. PNG images to. JPG images in batches and then convert them back.

From: https://linux.cn/article-8014-1.html

Address: http://www.linuxprobe.com/linux-jpg-png.html


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.