mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
翻译完成
This commit is contained in:
parent
9b40d8fdd3
commit
1ecb248d1d
@ -1,183 +0,0 @@
|
||||
(翻译中 by runningwater)
|
||||
# 4 Ways to Batch Convert Your PNG to JPG and Vice-Versa
|
||||
|
||||
In computing, Batch processing is the [execution of a series of tasks][11] in a program non-interactively. In this guide will offer you 4 simple ways to batch convert several `.PNG` images to `.JPG` and vice-versa using Linux command-line tools.
|
||||
|
||||
We will use convert command line tool in all the examples, however, you can as well make use of mogrify to achieve this.
|
||||
|
||||
The syntax for using convert is:
|
||||
|
||||
```
|
||||
$ convert input-option input-file output-option output-file
|
||||
|
||||
```
|
||||
|
||||
And for mogrify is:
|
||||
|
||||
```
|
||||
$ mogrify options input-file
|
||||
|
||||
```
|
||||
|
||||
Note: With mogrify, the original image file is replaced with the new image file by default, but it is possible to prevent this, by using certain options that you can find in the man page.
|
||||
|
||||
Below are the various ways to batch convert your all `.PNG` images to `.JPG` format, if you want to convert `.JPG`to `.PNG`, you can modify the commands according to your needs.
|
||||
|
||||
### 1\. Convert PNG to JPG Using ‘ls’ and ‘xargs’ Commands
|
||||
|
||||
The [ls command][10] allows you to list all your png images and xargs make it possible to build and execute a convert command from standard input to convert all `.png` images to `.jpg`.
|
||||
|
||||
```
|
||||
----------- Convert PNG to JPG -----------
|
||||
$ ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "${0%.png}.jpg"'
|
||||
|
||||
----------- Convert JPG to PNG -----------
|
||||
$ ls -1 *.jpg | xargs -n 1 bash -c 'convert "$0" "${0%.jpg}.png"'
|
||||
|
||||
```
|
||||
|
||||
Explanation about the options used in the above command.
|
||||
|
||||
1. `-1` – flag tells ls to list one image per line.
|
||||
2. `-n` – specifies the maximum number of arguments, which is 1 for the case.
|
||||
3. `-c` – instructs bash to run the given command.
|
||||
4. `${0%.png}.jpg` – sets the name of the new converted image, the % sign helps to remove the old file extension.
|
||||
|
||||
[
|
||||
![Convert PNG to JPG Format in Linux](http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-in-Linux.png)
|
||||
][9]
|
||||
|
||||
Convert PNG to JPG Format in Linux
|
||||
|
||||
I used `ls -ltr` command to [list all files by modified date and time][8].
|
||||
|
||||
Similarly, you can use above command to convert all your `.jpg` images to `.png` by tweaking the above command.
|
||||
|
||||
### 2\. Convert PNG to JPG Using GNU ‘Parallel’ Command
|
||||
|
||||
GNU Parallel enables a user to build and execute shell commands from standard input in parallel. Make sure you have GNU Parallel installed on your system, otherwise install it using the appropriate commands below:
|
||||
|
||||
```
|
||||
$ sudo apt-get install parallel [On Debian/Ubuntu systems]
|
||||
$ sudo yum install parallel [On RHEL/CentOS and Fedora]
|
||||
|
||||
```
|
||||
|
||||
Once Parallel utility installed, you can run the following command to convert all `.png` images to `.jpg` format from the standard input.
|
||||
|
||||
```
|
||||
----------- Convert PNG to JPG -----------
|
||||
$ parallel convert '{}' '{.}.jpg' ::: *.png
|
||||
|
||||
----------- Convert JPG to PNG -----------
|
||||
$ parallel convert '{}' '{.}.png' ::: *.jpg
|
||||
|
||||
```
|
||||
|
||||
Where,
|
||||
|
||||
1. `{}` – input line which is a replacement string substituted by a complete line read from the input source.
|
||||
2. `{.}` – input line minus extension.
|
||||
3. `:::` – specifies input source, that is the command line for the example above where *png or *jpg is the argument.
|
||||
|
||||
[
|
||||
![Parallel Command - Converts All PNG Images to JPG Format](http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-Parallel-Command.png)
|
||||
][7]
|
||||
|
||||
Parallel Command – Converts All PNG Images to JPG Format
|
||||
|
||||
Alternatively, you can as well use [ls][6] and parallel commands together to batch convert all your images as shown:
|
||||
|
||||
```
|
||||
----------- Convert PNG to JPG -----------
|
||||
$ ls -1 *.png | parallel convert '{}' '{.}.jpg'
|
||||
|
||||
----------- Convert JPG to PNG -----------
|
||||
$ ls -1 *.jpg | parallel convert '{}' '{.}.png'
|
||||
|
||||
```
|
||||
|
||||
### 3\. Convert PNG to JPG Using ‘for loop’ Command
|
||||
|
||||
To avoid the hustle of writing a shell script, you can execute a `for loop` from the command line as follows:
|
||||
|
||||
```
|
||||
----------- Convert PNG to JPG -----------
|
||||
$ bash -c 'for image in *.png; do convert "$image" "${image%.png}.jpg"; echo “image $image converted to ${image%.png}.jpg ”; done'
|
||||
|
||||
----------- Convert JPG to PNG -----------
|
||||
$ bash -c 'for image in *.jpg; do convert "$image" "${image%.jpg}.png"; echo “image $image converted to ${image%.jpg}.png ”; done'
|
||||
|
||||
```
|
||||
|
||||
Description of each option used in the above command:
|
||||
|
||||
1. -c allows for execution of the for loop statement in single quotes.
|
||||
2. The image variable is a counter for number of images in the directory.
|
||||
3. For each conversion operation, the [echo command][1] informs the user that a png image has been converted to jpg format and vice-versa in the line $image converted to ${image%.png}.jpg”.
|
||||
4. “${image%.png}.jpg” creates the name of the converted image, where % removes the extension of the old image format.
|
||||
|
||||
[
|
||||
![for loop - Convert PNG to JPG Format](http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-for-loop-Command.png)
|
||||
][5]
|
||||
|
||||
for loop – Convert PNG to JPG Format
|
||||
|
||||
### 4\. Convert PNG to JPG Using Shell Script
|
||||
|
||||
If you do not want to make your command line dirty as in the previous example, write a small script like so:
|
||||
|
||||
Note: Appropriately interchange the `.png` and `.jpg` extensions as in the example below for conversion 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 it as `convert.sh` and make the script executable and then run it from within the directory that has your images.
|
||||
|
||||
```
|
||||
$ chmod +x convert.sh
|
||||
$ ./convert.sh
|
||||
|
||||
```
|
||||
[
|
||||
![Batch Image Convert Using Shell Script](http://www.tecmint.com/wp-content/uploads/2016/11/Batch-Image-Convert-Using-Shell-Script.png)
|
||||
][4]
|
||||
|
||||
Batch Image Convert Using Shell Script
|
||||
|
||||
In summary, we covered some important ways to batch convert `.png` images to `.jpg` format and vice-versa. If you want to optimize images, you can go through our guide that shows [how to compress png and jpg images in Linux][3].
|
||||
|
||||
You can as well share with us any other methods including [Linux command line tools][2] for converting images from one format to another on the terminal, or ask a question via the comment section below.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/linux-image-conversion-tools/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
|
||||
译者:[runningwater](https://github.com/runningwater)
|
||||
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
[1]:http://www.tecmint.com/echo-command-in-linux/
|
||||
[2]:http://www.tecmint.com/tag/linux-tricks/
|
||||
[3]:http://www.tecmint.com/optimize-and-compress-jpeg-or-png-batch-images-linux-commandline/
|
||||
[4]:http://www.tecmint.com/wp-content/uploads/2016/11/Batch-Image-Convert-Using-Shell-Script.png
|
||||
[5]:http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-for-loop-Command.png
|
||||
[6]:http://www.tecmint.com/tag/linux-ls-command/
|
||||
[7]:http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-Parallel-Command.png
|
||||
[8]:http://www.tecmint.com/sort-ls-output-by-last-modified-date-and-time/
|
||||
[9]:http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-in-Linux.png
|
||||
[10]:http://www.tecmint.com/tag/linux-ls-command/
|
||||
[11]:http://www.tecmint.com/using-shell-script-to-automate-linux-system-maintenance-tasks/
|
@ -0,0 +1,182 @@
|
||||
# 4 种方式将 PNG 和 JPG 批量转换
|
||||
|
||||
计算机术语中,批处理指的是用一个非交互式的程序来[执行一序列的任务][11]。这篇教程里,我们会使用 Linux 命令行工具,并提供 4 种简单的处理方式来把一些 `.PNG` 格式的图像批量转换成 `.JPG` 格式的,反之亦然。
|
||||
|
||||
虽然所有示例中我们使用的都是 convert 命令行工具,但是您也可以使用 mogrify 命令来达到同样的效果。
|
||||
|
||||
convert 命令的语法如下:
|
||||
|
||||
```
|
||||
$ convert 输入选项 输入文件 输出选项 输出文件
|
||||
|
||||
```
|
||||
|
||||
而 mogrify 的为:
|
||||
|
||||
```
|
||||
$ mogrify 选项 输入文件
|
||||
|
||||
```
|
||||
|
||||
注意:在使用 mogrify 命令时,默认情况下源图像文件会被转换后的新文件覆盖掉,您可以使用明确的操作选项来禁止覆盖,具体的选项可以在手册页中查询得到。
|
||||
|
||||
下面是把所有 `.PNG` 格式图像批量转换为 `.JPG` 格式的各种实现方式。如果想把 `.JPG` 转换为 `.PNG` 格式,也可使用这些命令,按需修改。
|
||||
|
||||
### 1\. 使用 ‘ls’ 和 ‘xargs’ 命令来转换 PNG 和 JPG
|
||||
|
||||
[ls 命令](10) 可以列出所有的 png 图像文件, xargs 使得可以从标准输入构建和执行 convert 命令,将所有 `.png` 图像转换为 `.jpg` 图像。
|
||||
|
||||
```
|
||||
----------- 从 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"'
|
||||
|
||||
```
|
||||
|
||||
关于上面命令选项的说明:
|
||||
|
||||
1. `-1` – 告诉ls 每行列出一个图像名称的选项标识
|
||||
2. `-n` – 指定最多参数个数,例子中为 1
|
||||
3. `-c` – 指示 bash 运行给定的命令
|
||||
4. `${0%.png}.jpg` – 设置新转换的图像文件的名字,% 符号用来删除源文件的扩展名
|
||||
|
||||
[
|
||||
![Convert PNG to JPG Format in Linux](http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-in-Linux.png)
|
||||
][9]
|
||||
|
||||
Linux 中 PNG 格式转为 JPG 格式
|
||||
|
||||
我使用 `ls -ltr` 命令按[修改的日期和时间列出所有文件][8]。
|
||||
|
||||
类似的,也可以使用上面的命令要把 `.jpg` 图像转换为 `.png` 格式,只需稍微调整就行。
|
||||
|
||||
### 2\. 使用 GNU 的 'Parallel' 命令来转换 PNG 和 JPG
|
||||
|
||||
GNU Parallel 使用户能够从标准输入并行构建和执行 shell 命令。确保您的系统上安装了 GNU Parallel,否则请使用以下适当的命令进行安装:
|
||||
|
||||
```
|
||||
$ sudo apt-get install parallel [在 Debian/Ubuntu 系统中]
|
||||
$ sudo yum install parallel [在 RHEL/CentOS 和 Fedora 系统中]
|
||||
|
||||
```
|
||||
|
||||
安装好 Parallel 工具后,您就可以运行下面的命令来把所有从标准输入的 `.png` 图像转换成 `.jpg` 格式的图像。
|
||||
|
||||
```
|
||||
----------- 从 PNG 转换到 JPG -----------
|
||||
$ parallel convert '{}' '{.}.jpg' ::: *.png
|
||||
|
||||
----------- 从 JPG 转换到 PNG -----------
|
||||
$ parallel convert '{}' '{.}.png' ::: *.jpg
|
||||
|
||||
```
|
||||
|
||||
其中:
|
||||
|
||||
1. `{}` – 输入行替代符,代替了从输入源读取的完整行。
|
||||
2. `{.}` – 去除扩展名的输入行。
|
||||
3. `:::` – 指定输入源的符号,即上面示例的命令行,在这里 *png 或 jpg* 是命令参数。
|
||||
|
||||
[
|
||||
![Parallel Command - Converts All PNG Images to JPG Format](http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-Parallel-Command.png)
|
||||
][7]
|
||||
|
||||
Parallel 命令 – 把所有 PNG 图像转换为 JPG 格式
|
||||
|
||||
或者,您也可以结合 [ls][6] 和 parallel 命令来批量转换所有图像,如图所示:
|
||||
|
||||
```
|
||||
----------- 从 PNG 转换到 JPG -----------
|
||||
$ ls -1 *.png | parallel convert '{}' '{.}.jpg'
|
||||
|
||||
----------- 从 JPG 转换到 PNG -----------
|
||||
$ ls -1 *.jpg | parallel convert '{}' '{.}.png'
|
||||
|
||||
```
|
||||
|
||||
### 3\. 使用 ‘for loop’ 命令来转换 PNG 和 JPG
|
||||
|
||||
为了避免编写 shell 脚本的繁琐,你可以从命令行执行 `for loop` 语句,如下所示:
|
||||
|
||||
```
|
||||
----------- 从 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'
|
||||
|
||||
```
|
||||
|
||||
对上面的命令所使用的选项参数的描述:
|
||||
|
||||
1. -c 允许执行包括在单引号中的 loop 语句。
|
||||
2. image 变量是目录中的图像名的数量记数器。
|
||||
3. 对于每个转换操作,在 “$image converted to ${image%.png}.jpg” 这行中,[echo 命令][1]通知用户 png 图像已经转换为 jpg 格式,反之亦然。
|
||||
4. "${image%.png}.jpg" 语句创建了转换后的图像名字,其中 % 表示去除源图像文件的扩展名。
|
||||
|
||||
[
|
||||
![for loop - Convert PNG to JPG Format](http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-for-loop-Command.png)
|
||||
][5]
|
||||
|
||||
for loop 语句 – 从 PNG 转换到 JPG 格式
|
||||
|
||||
### 4\. 使用 Shell 脚本来转换 PNG 和 JPG
|
||||
|
||||
如果你不想像前面的例子那样让你的命令行变得邋遢的话,可以写一个小脚本,如下示:
|
||||
|
||||
笔记:适当地交换 `.png` 和 `.jpg` 扩展名,如下面的例子所示,从一种格式到另一种格式的转换
|
||||
|
||||
```
|
||||
#!/bin/bash
|
||||
#convert
|
||||
for image in *.png; do
|
||||
convert "$image" "${image%.png}.jpg"
|
||||
echo “image $image converted to ${image%.png}.jpg ”
|
||||
done
|
||||
exit 0
|
||||
|
||||
```
|
||||
|
||||
把上面的脚本保存为 `convert.sh` 文件,然后使此脚本文件可执行,接着从存有图像文件的目录下执行。
|
||||
|
||||
```
|
||||
$ chmod +x convert.sh
|
||||
$ ./convert.sh
|
||||
|
||||
```
|
||||
[
|
||||
![Batch Image Convert Using Shell Script](http://www.tecmint.com/wp-content/uploads/2016/11/Batch-Image-Convert-Using-Shell-Script.png)
|
||||
][4]
|
||||
|
||||
使用 Shell 脚本来批量图像转换
|
||||
|
||||
总之,我们介绍了一些重要的将 .png 图像批量转换为 .jpg 格式的方法,反之亦然。如果还想对图像进行一些优化的话, 您可以移步到[ Linux 系统中如何压缩 png 和 jpg 图像][3]这篇指导文章。
|
||||
|
||||
您可以给我们分享一些在终端下把图像从一种格式转成另一种格式的方式方法,或者是 [ Linux 命令行工具][2],或者在下面的评论部分畅所欲言。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/linux-image-conversion-tools/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
|
||||
译者:[runningwater](https://github.com/runningwater)
|
||||
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
[1]:http://www.tecmint.com/echo-command-in-linux/
|
||||
[2]:http://www.tecmint.com/tag/linux-tricks/
|
||||
[3]:http://www.tecmint.com/optimize-and-compress-jpeg-or-png-batch-images-linux-commandline/
|
||||
[4]:http://www.tecmint.com/wp-content/uploads/2016/11/Batch-Image-Convert-Using-Shell-Script.png
|
||||
[5]:http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-for-loop-Command.png
|
||||
[6]:http://www.tecmint.com/tag/linux-ls-command/
|
||||
[7]:http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-Using-Parallel-Command.png
|
||||
[8]:http://www.tecmint.com/sort-ls-output-by-last-modified-date-and-time/
|
||||
[9]:http://www.tecmint.com/wp-content/uploads/2016/11/Convert-PNG-to-JPG-in-Linux.png
|
||||
[10]:http://www.tecmint.com/tag/linux-ls-command/
|
||||
[11]:http://www.tecmint.com/using-shell-script-to-automate-linux-system-maintenance-tasks/
|
Loading…
Reference in New Issue
Block a user