[英]Converting multi-dimensional array to a tuple in python
我正在以 rgb 值的形式從網絡攝像頭獲取一些幀數據。
import numpy as np
frame = get_video()
print np.shape(frame)
輸出為 (480, 640, 3)。 現在我想從這些值構建圖像。 所以,我想用
im = Image.new("RGB", (480, 640),frame)
但是,這里的第三個參數需要一個元組。 我收到這個錯誤
SystemError: new style getargs format but argument is not a tuple
所以,我的問題是將這個幀數據轉換為元組的最佳方法是什么,以便我可以構建我的圖像。
我在這里假設您正在從 PIL 導入類 Image。
使用控制台命令 Image.new 訪問的 Image.new() 文檔? 是:
在 [3]: Image.new?
類型:函數
基類:
字符串形式:
命名空間:交互式
文件:/usr/lib/python2.7/dist-packages/PIL/Image.py
定義:Image.new(mode, size, color=0)
文檔字符串:創建一個新圖像
第三個參數是 RGB 顏色,例如 (255,255,255),用於填充孔洞圖像。 您無法使用此功能初始化單個像素。
我還假設該框架是一個 3D 陣列。 正如您的輸出所暗示的那樣,它是 480 行和 640 行 RGB 元組。
我不知道是否有更簡單的方法可以做到這一點,但我會通過 putpixel() 函數設置像素值,例如:
im = Image.new( "RGB", (480, 640), (0, 0, 0) ) #initialize 480:640 black image
for i in range(480):
for j in range(640):
im.putpixel( (i, j), tuple(frame[i][j]) )
始終通過控制台檢查文檔字符串,這樣可以節省大量時間。 我還建議使用 ipython 作為您的控制台。
我發現這個實現更快
from PIL import Image
im = Image.new("RGB", (480, 640), (0, 0, 0) ) #initialize 480:640 black image
while True:
frame = get_video()
im = Image.fromarray(frame)
im.save('some-name', 'JPEG')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.