如何使用 Python 转换图像文件格式

如何使用 Python 转换图像文件格式

Pillow 是一个强大的Python库,使用它,你可以轻松地转换图像格式。

本文将介绍如何使用几行简单的 Python 代码构建图像类型转换器。无论是单个图像文件还是目录中的所有文件,你都可以轻松地对它们进行格式转换。

安装所需的库

首先,你需要安装 Pillow 库,来在 Python 中构建图像类型转换器。

这个库提升了 Python 解释器的图像处理能力,你也可以使用该库的多个模块创建通用图像处理工具。

比较常用的是 ImageImageFileImageFilterImageStat 模块。

在终端中运行以下命令来安装 Pillow 库:

pip install pillow

加载和显示图像的属性

首先,你需要从PIL库 中导入Image模块来设置代码。接下来,你需要使用Image.open()方法加载图像并将其分配给变量。加载图像后,可以使用show()方法显示它。

图像格式转换器代码可在GitHub 存储库中获得,你可以在 MIT 许可下免费使用。

from PIL import Image
image = Image.open('sample-image.jpg')
image.show()

作为参数传递给open()方法的图像,将在你执行代码后打开。通过这个步骤来确保你已在系统上成功安装 Pillow 库。

Image 模块提供了几个其他属性,你可以使用它们来获取有关图像的更多信息。

# Importing library
from PIL import Image
 
# Loading the image
image = Image.open('sample-image.jpg')
 
# Prints the name of the file
print("Filename: ", image.filename)
 
# Prints the format of the file
# Eg- PNG, JPG, GIF, etc.
print("Format: ", image.format)
 
# Prints the mode of the file
# Eg- RGB, RFBA, CMYK, etc.
print("Mode: ", image.mode)
 
# Prints the size as a width and height tuple (in pixels)
print("Size: ", image.size)
 
# Prints the width of the image (in pixels)
print("Width: ", image.width)
 
# Prints the height of the image (in pixels)
print("Height: ", image.height)
 
# Closing the image
image.close()

你应该会看到一些关于图片的数据:

如何使用 Python 转换图像文件格式

如何使用 Python 转换图像格式

你可以使用 save() 方法简单地转换图像的文件格式。

你只需将新文件名和扩展名作为参数传递给save()方法,save()方法将自动识别你传递的扩展名,然后以识别的格式保存图像。

但在使用save()方法之前,你可能需要指定图像的模式(RGB、RGBA、CMYK、HSV 等)。

提示

根据Pillow官方文档,图像的模式是一个字符串,它定义了图像中像素的类型和深度。Pillow库支持 11 种模式,包括以下标准模式:

RGB(3×8 位像素,真彩色)

RGBA(4×8 位像素,带有透明蒙版的真彩色)

CMYK(4×8 位像素,分色)

HSV(3×8 位像素、色调、饱和度、值颜色空间)

如何将图像从 PNG 转换为 JPG 和 JPG 转换为 PNG

你需要将字符串 filename.jpg 作为参数传递给save()方法,以将任何格式(PNG、GIF、BMP、TIFF 等)的图像文件转换为 JPG 格式,此外,你需要提供图像的模式。

以下代码将图像从PNG 格式转换为 JPG 格式:

# Importing Library
from PIL import Image
 
# Loading the image
image = Image.open('sample-png-image.png')
 
# Specifying the RGB mode to the image
image = image.convert('RGB')
 
# Converting an image from PNG to JPG format
image.save("converted-jpg-image.jpg")
print("Image successfully converted!")

如果将图像转换为 JPG 格式,你将失去图像的任何透明度。如果你尝试使用 RGBA 模式保留透明度,Python 将抛出错误。

你可以使用save()方法将任何格式的图像转换为 PNG 格式。你只需要将 PNG 图像作为参数传递给save()方法。

以下代码将图像从 JPG 格式转换为 PNG 格式:

# Importing Library
from PIL import Image
 
# Loading the image
image = Image.open('sample-jpg-image.jpg')
 
# Converting image from JPG to PNG format
image.save("converted-png-image.png")
print("Image successfully converted!")

将图像转换为 PNG 会保留任何透明度。例如,如果你将透明的 GIF 图像转换为 PNG 图像,结果仍然是透明图像。

如何使用 Python 将图像转换为任何其他格式

与上述步骤类似,你可以使用save()方法将任何格式的图像转换为任何其他格式。你只需为save()方法提供正确的图像扩展名(.webp、.png、.bmp 等)。

例如,以下代码可以将图像从 PNG 转换为 WebP 格式:

# Importing Library
from PIL import Image
 
# Loading the image
image = Image.open('sample-transparent-png-image.png')
 
# Converting an image from PNG to WEBP format
image.save("converted-webp-image.webp")
print("Image successfully converted!")

丢失图像文件的错误处理

如果代码无法找到输入图像,则会抛出错误。你可以使用 FileNotFoundError Python 异常来处理这个问题。

# Importing Library
from PIL import Image
 
try:
    # Loading the image
    image = Image.open('wrong-filename.jpg')
 
    # Converting image from JPG to PNG format
    image.save("converted-png-image.png")
    print("Image successfully converted!")
 
except FileNotFoundError:
    print("Couldn't find the provided image")

将目录中的所有图像转换为不同的格式

如果一个目录中有多个图像文件,你想要转换为不同的格式,你只需使用 Python 中的几行代码即可轻松完成。

你需要导入glob库以遍历当前目录或给定文件夹中的文件。

以下代码将当前目录下的所有 JPG 图片转换为 PNG 格式:

from PIL import Image
import glob
  
for file in glob.glob("*.jpg"):
    image = Image.open(file)
    image.save(file.replace("jpg", "png"))

如果要转换一组不同的文件,请更改传递给glob()方法的字符串参数。

使用 Python 构建 GUI

像 Pillow 这样的 Python 库可以轻松地开发工具来处理 Python 中的图像。

你可以使用命令行界面快速执行任务,你也可以使用 Tkinter 和 PyQt等Python GUI框架,来创建GUI程序来给你的朋友们使用。

如果本站的内容对你有帮助,可以点击这儿,不花一分钱捐赠本站

(1)
疯狂的小黑的头像疯狂的小黑
上一篇 2022年9月28日 下午9:20
下一篇 2022年10月11日 上午9:19

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

微信