This article was reproduced from: https://linux.cn/article-8014-1.html
In computer terminology, batching refers to the method of performing a sequence of tasks 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 lists all PNG image files so that xargs commands can be built and executed from standard input convert , .png transforming all images into .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– Tells LS to list an option identifier for an image name per line
-n– Specify the maximum number of parameters, 1 in the example
-c– Instructs Bash to run the given command
${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 files by the date and time modified.
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 [in Debian/ubuntu system]
-
$ sudo yum install parallel [in Rhel/centos fedora system]
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
which
{}– Enter the line substitute instead of the full line read from the input source.
{.}– Remove the input line for the extension.
:::– 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 combine LS and parallel commands to convert all images in batches:
----------- 从 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:
-cAllows you to execute a loop statement that is included in single quotation marks.
imageA variable is a number register of image names in a directory.
- For each conversion operation, in the
$image conversion to ${image%.png}.jpg this line, the echo command notifies the user that the PNG image has been converted to JPG format, and vice versa.
${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
#convert
for image in *.png; do
convert "$image" "${image%.png}.jpg"
echo “image $image converted to ${image%.png}.jpg ”
done
exit 0
Save the above script as a convert.sh file, then 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've introduced some important .PNG ways to convert images to .JPG formats and back again. If you want to optimize the image, you can go to the guidance article on how to compress PNG and JPG images in a Linux system.
You can share with us some ways to turn images from one format to another, including Linux command-line tools, or to speak freely in the comments section below.
Linux Slice conversion tool ["Go"