In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to convert between PNG and JPG under Linux". In daily operation, I believe many people have doubts about how to convert between PNG and JPG under Linux. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to convert between PNG and JPG under Linux". Next, please follow the editor to study!
Although we use the convert command-line tool in all the examples, you can also use the mogrify command to achieve the same effect. The syntax of the convert command is:
$convert input option input file output option output file
Mogrify is:
$mogrify option input file
Note: when using the mogrify command, the source image file will be overwritten by the converted new file by default. You can use explicit operation options to disable overwriting, which can be queried in the man page. Here are various implementations of batch conversion of all .PNG format images to .JPG format. If you want to convert .JPG to .PNG format, you can also use these commands to modify as needed.
The content operation is as follows: 1. Use ls and xargs commands to convert PNG and JPG.
The ls command lists all png image files, and xargs makes it possible to build and execute convert commands from standard input, converting all .png images to .jpg images.
-convert from PNG to JPG-$ls-1 * .png | xargs-n 1 bash-c 'convert "$0"${0%.png} .jpg"'-from JPG to PNG-$ls-1 * .jpg | xargs-n 1 bash-c 'convert "$0"${0%.jpg} .png"'
A description of the above command options:
-1-tells ls the option identification to list one image name per line-n-specifies the maximum number of parameters, in this example, 1m c-instructs bash to run the given command ${0%.png} .jpg-sets the name of the newly converted image file, and the% symbol is used to delete the extension of the source file
PNG format in Linux to JPG format I use the ls-ltr command to list all files by date and time of modification. Similarly, you can use the above command to convert a .JPG image to .PNG format with a slight adjustment.
2. Use the parallel command of GNU to convert PNG and JPG
GNU's parallel enables users 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 appropriate command to install:
$sudo apt-get install parallel [in Debian/Ubuntu systems] $sudo yum install parallel [in RHEL/CentOS and Fedora systems]
Once the parallel tool is installed, you can run the following command to convert all standard input. PNG images into .JPG images.
-convert from PNG to JPG-$parallel convert'{}'{.} .jpg': * .png-convert from JPG to PNG-$parallel convert'{}'{.} .png': * .jpg'
Where:
{}-enter a line substitute instead of the full line read from the input source. {.}-removes the input line of the extension. ::-specifies the symbol of the input source, that is, the command line of the above example, where png or jpg are command parameters.
Parallel command-convert all PNG images to JPG format or you can batch convert all images in combination with the ls and parallel commands, as shown in the figure:
-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 for loop statements from the command line, as follows:
-convert from PNG to JPG-$bash-c 'for image in * .png; do convert "$image"${image%.png} .jpg"; echo "image $image converted to ${image%.png} .jpg"; done'- from JPG to PNG-$bash-c' for image in * .jpg Do convert "$image"${image%.jpg} .png"; echo "image $image converted to ${image%.jpg} .png"; done'
A description of the option parameters used by the above command:
-c allows the execution of circular statements enclosed in single quotation marks. The image variable is a counter for the number of image names in the directory. For each conversion operation, in the line from $image to ${image%.png} .jpg, the echo command informs the user that the png image has been converted to jpg format, and vice versa. The ${image%.png} .jpg statement creates the converted image name, where% represents the extension of the source image file.
For Loop statement-convert from PNG to JPG format
4. Use Shell script to convert PNG and JPG
If you don't want to make your command line sloppy like the previous example, you can write a small script like this: note: swap .png and .jpg extensions appropriately, as shown in the following example, from one format to another:
#! / 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 a convert.shconvert.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
At this point, the study on "how to convert between PNG and JPG under Linux" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.