PIL图像与NumPy数组的无缝转换指南

Python Imaging Library (PIL),更常见的称呼是其分支库Pillow,提供了丰富的图像处理功能。Pillow允许你将图像文件转换为数组,同时也支持将数组转换回图像文件。这种转换在图像处理、计算机视觉等领域非常有用。以下是一个使用Pillow将图像与数组进行转换的示例。

图片[1]_PIL图像与NumPy数组的无缝转换指南_知途无界

安装Pillow

首先,你需要确保已经安装了Pillow库。你可以使用pip来安装它:

pip install pillow
pip install pillow
pip install pillow

图像转换为数组

要将图像转换为数组,你可以使用PIL.Image模块加载图像,然后使用numpy库将其转换为数组。以下是一个示例:

from PIL import Image
import numpy as np
# 加载图像
image = Image.open('path_to_your_image.jpg')
# 将图像转换为RGB数组(对于彩色图像)
image_array = np.array(image)
# 或者,如果你想要一个灰度图像数组
gray_image = image.convert('L')
gray_image_array = np.array(gray_image)
print(image_array.shape) # 输出图像的维度,例如 (height, width, 3) 对于RGB图像
print(gray_image_array.shape) # 输出 (height, width) 对于灰度图像
from PIL import Image
import numpy as np

# 加载图像
image = Image.open('path_to_your_image.jpg')

# 将图像转换为RGB数组(对于彩色图像)
image_array = np.array(image)

# 或者,如果你想要一个灰度图像数组
gray_image = image.convert('L')
gray_image_array = np.array(gray_image)

print(image_array.shape)  # 输出图像的维度,例如 (height, width, 3) 对于RGB图像
print(gray_image_array.shape)  # 输出 (height, width) 对于灰度图像
from PIL import Image import numpy as np # 加载图像 image = Image.open('path_to_your_image.jpg') # 将图像转换为RGB数组(对于彩色图像) image_array = np.array(image) # 或者,如果你想要一个灰度图像数组 gray_image = image.convert('L') gray_image_array = np.array(gray_image) print(image_array.shape) # 输出图像的维度,例如 (height, width, 3) 对于RGB图像 print(gray_image_array.shape) # 输出 (height, width) 对于灰度图像

数组转换为图像

要将数组转换回图像,你可以使用PIL.Image.fromarray方法。以下是一个示例:

# 假设你有一个NumPy数组
array = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) # 创建一个随机的RGB图像数组
# 将数组转换为图像
image_from_array = Image.fromarray(array)
# 保存图像
image_from_array.save('image_from_array.jpg')
# 假设你有一个NumPy数组
array = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)  # 创建一个随机的RGB图像数组

# 将数组转换为图像
image_from_array = Image.fromarray(array)

# 保存图像
image_from_array.save('image_from_array.jpg')
# 假设你有一个NumPy数组 array = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) # 创建一个随机的RGB图像数组 # 将数组转换为图像 image_from_array = Image.fromarray(array) # 保存图像 image_from_array.save('image_from_array.jpg')

对于灰度图像,你需要确保数组是二维的,并且数据类型是uint8

# 创建一个随机的灰度图像数组
gray_array = np.random.randint(0, 256, (100, 100), dtype=np.uint8)
# 将灰度数组转换为图像
gray_image_from_array = Image.fromarray(gray_array, 'L') # 注意这里的'L'表示灰度模式
# 保存灰度图像
gray_image_from_array.save('gray_image_from_array.jpg')
# 创建一个随机的灰度图像数组
gray_array = np.random.randint(0, 256, (100, 100), dtype=np.uint8)

# 将灰度数组转换为图像
gray_image_from_array = Image.fromarray(gray_array, 'L')  # 注意这里的'L'表示灰度模式

# 保存灰度图像
gray_image_from_array.save('gray_image_from_array.jpg')
# 创建一个随机的灰度图像数组 gray_array = np.random.randint(0, 256, (100, 100), dtype=np.uint8) # 将灰度数组转换为图像 gray_image_from_array = Image.fromarray(gray_array, 'L') # 注意这里的'L'表示灰度模式 # 保存灰度图像 gray_image_from_array.save('gray_image_from_array.jpg')

注意事项

  • 当将图像转换为数组时,确保图像已经以正确的模式(如RGB或灰度)加载。
  • 当将数组转换为图像时,指定正确的模式(如’RGB’或’L’)是很重要的。
  • 数组的数据类型应该是uint8,因为图像像素值通常在这个范围内(0到255)。

通过以上步骤,你可以轻松地在Pillow图像和NumPy数组之间进行转换,从而利用这两个库提供的强大功能进行图像处理和分析。

© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞69 分享
Put blinders on to those things that conspire to hold you back, especially the ones in your own head.
不要去想那些阻碍你的事,尤其是那些自己想象出来的事
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容