Four ways to convert PNG and JPG into batches under Linux

Source: Internet
Author: User
Tags echo command

In computer terminology, batching refers to the method of performing a sequence of tasks [1] with a non-interactive program . In this tutorial, we will use the Linux command-line tool and provide 4 simple ways to .PNG convert some format images into .JPG format and back.

Although all the examples we use are command- convert line tools, you can also use mogrify commands to achieve the same effect.

convertThe syntax of the command is as follows:

$ convert 输入选项 输入文件 输出选项 输出文件

And mogrify the following are:

$ mogrify 选项 输入文件

Note: When using a mogrify command, the source image file is overwritten by default when the new file is converted, and you can use explicit action options to suppress the overlay, which can be queried in the manual pages.

Here are a .PNG variety of implementations that convert all format images to .JPG formats in batches. If you want to .JPG convert to .PNG format, you can also use these commands and modify them as needed.

1. Use lsAnd xargscommand to convert PNG and JPG

The LS command [2] lists all PNG image files so that xargs commands can be built and executed from standard input convert , .png converting all images to .jpg images.

----------- 从 PNG 转换到 JPG ----------- $ ls -1 *.png | xargs -n 1 bash -c ‘convert "$0" "${0%.png}.jpg"‘----------- 从 JPG 转换到 PNG ----------- $ ls -1 *.jpg | xargs -n 1 bash -c ‘convert "$0" "${0%.jpg}.png"‘

A description of the above command options:

    1. -1– Tells LS to list an option identifier for an image name per line
    2. -n– Specify the maximum number of parameters, 1 in the example
    3. -c– Instructs Bash to run the given command
    4. ${0%.png}.jpg– Set the name of the newly converted image file, which is % used to delete the extension of the source file


Convert PNG format to JPG in Linux

I use ls -ltr the command to list all the files by the modified date and time [4].

Similarly, you can also use the command above to .JPG convert an image to a .PNG format, just a slight adjustment on the line.

2. Use of GNU parallelcommand to convert PNG and JPG

The GNU parallel enables users to build and execute shell commands in parallel from standard input. Make sure the GNU Parallel is installed on your system, or use the appropriate command below to install it:

$ sudo apt-get install parallel     [在 Debian/Ubuntu 系统中]$ sudo yum install parallel         [在 RHEL/CentOS 和 Fedora 系统中]

Once the parallel tool is installed, you can run the following command to convert all images from the standard input .PNG into a .JPG formatted image.

----------- 从 PNG 转换到 JPG ----------- $ parallel convert ‘{}‘ ‘{.}.jpg‘ ::: *.png----------- 从 JPG 转换到 PNG -----------$ parallel convert ‘{}‘ ‘{.}.png‘ ::: *.jpg


    1. {}– Enter the line substitute instead of the full line read from the input source.
    2. {.}– Remove the input line for the extension.
    3. :::– Specify the symbol for the input source, which is the command line for the example above, where PNG or JPG is the command parameter.


Parallel command – Convert all PNG images to JPG format

Alternatively, you can parallel convert all images in batches by combining LS [6] and commands:

----------- 从 PNG 转换到 JPG ----------- $ ls -1 *.png | parallel convert ‘{}‘ ‘{.}.jpg‘----------- 从 JPG 转换到 PNG -----------$ ls -1 *.jpg | parallel convert ‘{}‘ ‘{.}.png‘
3. Use forLoop command to convert PNG and JPG

To avoid the tedious writing of shell scripts, you can execute loop statements from the command line for as follows:

----------- 从 PNG 转换到 JPG ----------- $ bash -c ‘for image in *.png; do convert "$image" "${image%.png}.jpg"; echo “image $image converted to ${image%.png}.jpg ”; done‘----------- 从 JPG 转换到 PNG -----------$ bash -c ‘for image in *.jpg; do convert "$image" "${image%.jpg}.png"; echo “image $image converted to ${image%.jpg}.png ”; done‘

Description of the option parameter used for the above command:

    1. -cAllows you to execute a loop statement that is included in single quotation marks.
    2. imageA variable is a number register of image names in a directory.
    3. For each conversion operation, in the $image conversion to ${image%.png}.jpg this line, the echo command [7] Notifies the user that the PNG image has been converted to JPG format, and vice versa.
    4. ${image%.png}.jpgStatement creates the converted image name, which % represents the extension that removes the source image file.


For Loop statement – Convert from PNG to JPG format

4. Convert PNG and JPG using Shell scripts

If you don't want to make your command line sloppy as in the previous example, you can write a small script that looks like this:

Note: Swap .png and .jpg extend the name appropriately, as shown in the following example, from one format to another:

#!/bin/bash#convertfor image in *.png; do        convert  "$image"  "${image%.png}.jpg"        

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

$ chmod +x$ ./


Batch image conversion using Shell scripts

In short, we've introduced some important .PNG ways to convert images to .JPG formats and back again. If you still want to optimize the image, you can go to the Linux system to compress PNG and JPG images [10] This guidance article.

Read the original

Four ways to convert PNG and JPG into batches under Linux

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: 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.