Python PIL Image.frombytes()方法

Python PIL Image.frombytes()方法

PIL是Python图像库,它为Python解释器提供了图像编辑功能。图像模块提供了一个同名的类,用来表示一个PIL图像。该模块还提供了一些工厂函数,包括从文件加载图像和创建新图像的函数。

PIL.Image.frombytes()从缓冲区的像素数据中创建一个图像存储器的副本。在其最简单的形式中,该函数需要三个参数(模式、大小和未打包的像素数据)。

语法: PIL.Image.frombytes(mode, size, data, decoder_name=’raw’, *args)

参数:
mode – 图像模式。见。模式。
size – 图像大小。
data – 一个包含给定模式的原始数据的字节缓冲区。
decoder_name – 使用什么解码器。
args – 给定解码器的附加参数。

返回:一个图像对象。

# importing image object from PIL
from PIL import Image
  
# using tobytes data as raw for frombyte function
tobytes = b'xd8\xe1\xb7\xeb\xa8\xe5 \xd2\xb7\xe1'
img = Image.frombytes("L", (3, 2), tobytes)
  
# creating list 
img1 = list(img.getdata())
print(img1)

输出:

[120, 100, 56, 225, 183, 235]

另一个例子:这里我们使用不同的原始数据(tobytes)。

# importing image object from PIL
from PIL import Image
  
# using tobytes data as raw for frombyte function
tobytes = b'\xbf\x8cd\xba\x7f\xe0\xf0\xb8t\xfe'
img = Image.frombytes("L", (3, 2), tobytes)
  
# creating list 
img1 = list(img.getdata())
print(img1)

输出:

[191, 140, 100, 186, 127, 224]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程