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 FormatFolder Path作为参数给出,从而进一步扩展其功能。

参考文献 (References)

  • The complete code used here is available on my github

    此处使用的完整代码可在我的github上找到

  • Zen of Python

    Python之禅

  • Pillow

    枕头

翻译自: https://www.freecodecamp.org/news/how-to-build-an-image-type-convertor-in-six-lines-of-python-d63c3c33d1db/

python 图像格式转换

python 图像格式转换_如何用六行Python构建图像类型转换器相关推荐

  1. python神经网络构建图_如何用卷积神经网络构建图像?

    原标题:如何用卷积神经网络构建图像? 原标题 |Everything you need to know to master Convolutional Neural Networks 作者 | Tir ...

  2. python 文件格式转换_数据分析:基于Python的自定义文件格式转换系统

    ( 白宁超 2018年7月16日14:47:41 ) 导读:随着大数据的快速发展,自然语言处理.数据挖掘.机器学习技术应用愈加广泛.针对大数据的预处理工作是一项庞杂.棘手的工作.首先数据采集和存储,尤 ...

  3. python格式化转换_(转)python 格式化输出及%用法

    一.格式化输出1.整数的输出%o -- oct 八进制%d -- dec 十进制%x -- hex 十六进制 print('%o' % 20)24 print('%d' % 20)20 print(' ...

  4. google python代码规范_如何用好python编码规范,写一手漂亮的代码

    前一段时间在编写python 代码的时候编辑器中一直在提示规范问题,因为强迫症的原因,我决定遵循python 的编码规范去编码,然后把需要注意的点记录下来, 帮助自己和大家一起成长. 这是我的main ...

  5. pep8 python 编码规范_如何用好python编码规范,写一手漂亮的代码

    前一段时间在编写python 代码的时候编辑器中一直在提示规范问题,因为强迫症的原因,我决定遵循python 的编码规范去编码,然后把需要注意的点记录下来, 帮助自己和大家一起成长. 这是我的main ...

  6. python 图像格式转换文件夹下 jpg 转 bmp | 目录遍历

    python 图像格式转换文件夹下 jpg 转 bmp import os from PIL import Imagedef jpgToBmp(imgFile):dst_dir = "/ho ...

  7. python编程基础_月隐学python第2课

    python编程基础_月隐学python第2课 学习目标 掌握变量的输入和输出 掌握数据类型的基本概念 掌握算数运算 1.变量的输入和输出 1.1 变量输入 使用input输入 input用于输入数据 ...

  8. 查看Python的版本_查看当前安装Python的版本

    一.查看Python的版本_查看当前安装Python的版本 具体方法: 首先按[win+r]组合键打开运行: 然后输入cmd,点击[确定]: 最后执行[python --version]命令即可. 特 ...

  9. python实现新闻网站_如何用 100 行 Python 代码实现新闻爬虫?这样可算成功?

    每天我都要坐地铁上班,而地铁里信号差.但我希望在坐地铁的时候读些新闻,于是就写了下面这个新闻爬虫.我并没有打算做很漂亮的应用,所以只完成了原型,它可以满足我最基本的需求.其思路很简单: 找到新闻源;用 ...

最新文章

  1. java日历表打印_Java打印日历表
  2. 【深度学习】解析神经网络中的数值稳定性、模型初始化和分布偏移(Pytorch)
  3. 5 图片 渐变_AI教程!5分钟教你绘制超酷炫的字体效果
  4. iOS中的JSON解析
  5. Java 遍历HashTable
  6. 9. OD-PEID的入门及BASIC(VB)开发的程序破解
  7. c#获取本地ip地址网关子网掩码_C#设置本地网络如DNS、网关、子网掩码、IP等等...
  8. java SimpleDateFormat类浅析
  9. MIMO-OTFS in High-Doppler Fading Channels:Signal Detection and Channel Estimation(3)
  10. mac os 录屏快捷键_免费的录屏软件有哪些?不限制时长的录制软件
  11. 如何获取海量长尾关键词流量
  12. 为你的整轨APE音乐制作CUE文件(图文)
  13. 电动汽车结构原理基础知识
  14. ORAN C平面传输和基本功能
  15. win下激活python虚拟环境及激活失败解决方案
  16. StbM 和 Time Synchronization Over CAN and Ethernet(二) 以EthTSyn和StbM为例
  17. Android 8内测版本,安卓qq v8.0.8内测版
  18. 音视频开发:直播推流技术指南
  19. 常见问题:try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候执行?
  20. 100万美金VS100美金FBI本可以在iPhone 5c解锁案中省下一大笔钱

热门文章

  1. 加载本地json文件,并利用批处理调用Chrome显示html
  2. Linux—scp或ssh出现WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
  3. 【C++ STL学习之七】STL算法之find和find_if
  4. 继承演练 c# 1613706361
  5. java类与对象 演练 客户姓名添加与查看
  6. python-基本装饰器
  7. python-计算机二级考试-报考笔记
  8. 前端_JavaScript_Vue
  9. JSON返回的自定义
  10. android 中 DOM解析xml