python 图像格式转换
by AMR
通过AMR
如何用六行Python构建图像类型转换器 (How to build an image type convertor in six lines of Python)
One of the advantage of being a programmer is your ability to build utility tools to improve your life. Unlike a non-programmer, you are probably not spending hours digging through multiple Google search result pages to find a tool that, in the first place, was supposed to improve your productivity (irony wins). This likely makes you feel more powerful knowing a programming language — especially if that Programming language is as versatile and awesome as Python is.
成为程序员的好处之一是您具有构建实用工具以改善生活的能力。 与非程序员不同,您可能不需要花费大量时间在多个Google搜索结果页面上进行挖掘,而是找到了一种首先可以提高生产率的工具( 具有讽刺意味的优势 )。 这可能会使您了解一种编程语言,从而感觉更强大-尤其是如果该编程语言像Python一样具有多种用途且功能强大。
One of the points in the The Zen of Python says:
《 Python的禅宗》中的要点之一是:
Simple is better than complex.
简单胜于复杂。
With this philosophy in place, a lot of niche tool development using Python can be done so succinctly that it makes me wonder if it’s worth calling it a tool at all. Sometimes the word script
would be more accurate. Either way, we’re setting out here to build one such script
that converts images from one file format (image type) to another — in just 6 lines of Python code.
有了这种理念,使用Python进行的许多利基工具开发就可以如此简洁地完成,这使我怀疑是否值得将其称为工具。 有时,文字script
会更准确。 无论哪种方式,我们都在这里着手构建一个这样的script
,该script
只需6行Python代码即可将图像从一种文件格式(图像类型)转换为另一种文件格式。
Disclaimer: The number of lines (6) excludes empty lines and comments
免责声明:第(6)行不包括空行和注释
In this tutorial, we’re going to build an image type convertor that converts a PNG image to a JPG image. Before your grey matter cells are rushing to judge whether I’m crazy to build this tool, let me say that this is not just for one image — but for all the images inside a folder. That’d definitely require more manual effort to do without coding (I know you can smell bash
ing).
在本教程中,我们将构建一个图像类型转换器,将PNG图像转换为JPG图像。 在您的灰质细胞急于判断我是否为构建此工具而疯狂之前,我要说这不仅是针对一张图像,而是文件夹中的所有图像。 这肯定会需要更多的人工劳动无需编码(我知道你可以闻到做bash
ING)。
Python包 (Python Package)
We’re going to use the Python package PIL
(which stands for Python Image Library) for this purpose. The original PIL
didn’t get any updates for the latest Python version, so some good souls have created a friendly fork called Pillow
that supports even > Python 3.0.
为此,我们将使用Python软件包PIL
(代表Python图像库)。 原始的PIL
没有针对最新的Python版本进行任何更新,因此一些好心人创建了一个名为Pillow
的友好派生工具 ,甚至支持> Python 3.0。
Install it using pip3 install Pillow
.
使用pip3 install Pillow
安装它。
起始脚本 (Beginning Script)
There are two primary sections in this code. The first section is where we import the required packages, and the second section is where the actual operation happens. The actual operation can be further broken down as follows:
此代码中有两个主要部分。 第一部分是导入所需软件包的位置,第二部分是实际操作的位置。 实际操作可以进一步细分如下:
Iterate through all the files with the given extension — in our case
.png
— and repeat all the following:遍历具有给定扩展名的所有文件(在我们的示例中为
.png
,然后重复以下所有操作:- Open the image file (as an image file) 打开图像文件(作为图像文件)
Convert the image file to a different format (
RGB
)将图像文件转换为其他格式(
RGB
)Finally save the file — with the new extension
.jpg
最后保存文件-新扩展名为
.jpg
Lines 1 and 2:
第1行和第2行:
from PIL import Image # Python Image Library - Image Processing
import glob
This section just imports the required packages. PIL
for Image Processing and glob
for iterating through files of the given folder in the OS.
本节仅导入所需的软件包。 用于图像处理的PIL
和用于遍历OS中给定文件夹的文件的glob
。
Lines 3–6:
第3-6行:
# based on SO Answer: https://stackoverflow.com/a/43258974/5086335
for file in glob.glob("*.png"):
im = Image.open(file)
rgb_im = im.convert('RGB')
rgb_im.save(file.replace("png", "jpg"), quality=95)
鳍 (FIN)
So that’s the end of our tool! You can save these 6 lines as a .py
file and then invoke them in your computer where you’ve got images to convert.
这就是我们工具的终点! 您可以将这6行保存为.py
文件,然后在要转换图像的计算机中调用它们。
进一步的发展 (Further Development)
If you are planning on to improve this script further, you can convert this entire script into a Command Line Interface Tool — then all these details like File Format
and Folder Path
can be given as arguments thus extending its power further.
如果您打算进一步改进此脚本,则可以将整个脚本转换为命令行界面工具-然后可以将所有这些详细信息(例如File Format
和Folder Path
作为参数给出,从而进一步扩展其功能。
参考文献 (References)
The complete code used here is available on my github
此处使用的完整代码可在我的github上找到
python 图像格式转换