PIL是Python Imaging Library,它為python解釋器提供了圖像編輯函數。的Image
模塊提供了一個具有相同名稱的類,用於表示PIL圖像。該模塊還提供了許多出廠函數,包括從文件加載圖像和創建新圖像的函數。
PIL.Image.frombuffer()
在字節緩衝區中創建引用像素數據的圖像存儲器。
請注意,此函數僅解碼像素數據,而不解碼整個圖像。如果字符串中包含整個圖像文件,則將其包裝在BytesIO對象中,然後使用open()加載它。
用法: PIL.Image.frombuffer(mode, size, data, decoder_name=’raw’, *args)
參數:
mode-圖像模式。另請:模式
size-圖像尺寸。
data-包含給定模式原始數據的字節緩衝區。
decoder_name-使用什麽解碼器。
args-給定解碼器的其他參數。對於默認編碼器(“raw”),建議您提供完整的參數集:frombuffer(mode, size, data, "raw", mode, 0, 1)
返回:Image對象。
使用的圖片:
# importing image object from PIL
from PIL import Image
# creating an image object
im = Image.open(r"C:\Users\System-Pc\Desktop\rose.jpg")
im1 = im.tobytes("xbm", "rgb")
img = Image.frombuffer("L", (4, 4), im1, 'raw', "L", 0, 1)
# creating list
img2 = list(img.getdata())
print(img2)
輸出:
[48, 120, 102, 102, 44, 48, 120, 102, 102, 44, 48, 120, 102, 102, 44, 48]
另一個示例:在這裏,我們使用另一個圖像。
使用的圖片:
# importing image object from PIL
from PIL import Image
# creating an image object
im = Image.open(r"C:\Users\System-Pc\Desktop\ellipse1.png")
im1 = im.tobytes("xbm", "rgb")
img = Image.frombuffer("L", (10, 10), im1, 'raw', "L", 0, 1)
# creating list
img2 = list(img.getdata())
print(img2)
輸出:
[48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 10, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56, 44, 48, 120, 56, 56]
相關用法
- Python os.dup()用法及代碼示例
- Python next()用法及代碼示例
- Python set()用法及代碼示例
- Python object()用法及代碼示例
- Python bytes()用法及代碼示例
- Python os.times()用法及代碼示例
- Python os.chmod用法及代碼示例
- Python hash()用法及代碼示例
- Python os.ftruncate()用法及代碼示例
- Python os.truncate()用法及代碼示例
- Python os.fsdecode()用法及代碼示例
- Python dict pop()用法及代碼示例
- Python os.abort()用法及代碼示例
- Python os.WEXITSTATUS()用法及代碼示例
注:本文由純淨天空篩選整理自Sunitamamgai大神的英文原創作品 Python PIL | Image.frombuffer() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。