我有一組 rgb 元組
array = [(144, 144, 133), (85, 87, 75), (140, 87, 70), (129, 107, 105), (129, 107, 105), (194, 179, 171), (178, 164, 159), (100, 105, 122), (36, 38, 57), (59, 48, 49), (59, 48, 49), (152, 149,
148), (152, 149, 148), (0, 0, 0), (0, 0, 0), (98, 81, 84)...]
我想制作一個正方形并更改為 PIL 影像。
dt = np.dtype('int,int,int')
array2d = np.array(array, dt)
im_pil = Image.fromarray(obj=array2d.reshape(int(math.sqrt(size)), int(math.sqrt(size))), mode='RGB')
data = io.BytesIO()
im_pil.save(data, "bmp")
由于某些我不知道的原因,它將每個數字分開,例如 (144,144,133) -> (144,0,0), (0,144,0), (0,0,133), (0,0,0)
我的問題是初始陣列的形狀嗎?還是圍繞它的修改?有任何想法嗎?當然我試過 np.array(array, np.uint8) 同樣的問題
提前致謝。
uj5u.com熱心網友回復:
我相信,我們的問題在于你的重塑功能。您將陣列重塑為具有 shape 的二維陣列[sqrt(size),sqrt(size)]。您需要的是一個具有形狀[sqrt(size),sqrt(size),3](x 維度、y 維度、3 個 RGB 值)的 3d 陣列。
為了解決你的問題,你應該做
im_pil = Image.fromarray(obj=array2d.reshape(int(math.sqrt(size)), int(math.sqrt(size)),3), mode='RGB')
uj5u.com熱心網友回復:
而不是這個:
im_pil = Image.fromarray(obj=array2d.reshape(int(math.sqrt(size)), int(math.sqrt(size))), mode='RGB')
試試這個:
arr3d = arr.reshape(2,3,2)
Image.fromarray(arr3d,'RGB').show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471336.html
上一篇:嘗試在Flask網站中使用影像僅顯示損壞的img圖示
下一篇:在python中有:'fromPILimportImageTK,Image',但我也想有'fromexifimportImage',但它們不能共存
