![](/img/trans.png)
[英]How to convert black and white image to array with 3 dimensions in python?
[英]Convert black and white array into an image in python?
我有一個50x50元素的數組,每個元素都是True或False - 這代表一個50x50的黑白圖像。
我無法將其轉換為圖像? 我嘗試了無數不同的功能,但沒有一個能夠正常工作。
import numpy as np
from PIL import Image
my_array = np.array([[True,False,False,False THE DATA IS IN THIS ARRAY OF 2500 elements]])
im = Image.fromarray(my_array)
im.save("results.jpg")
^這個給了我:“無法處理這種數據類型”。
我已經看到PIL有一些功能,但它們只轉換RGB像素列表,我有一個簡單的黑白數組,沒有其他通道。
首先,你應該使你的數組50x50而不是1d數組:
my_array = my_array.reshape((50, 50))
然后,要獲得標准的8位圖像,您應該使用無符號的8位整數dtype:
my_array = my_array.reshape((50, 50)).astype('uint8')
但是你不希望True
s為1
,你希望它們是255
:
my_array = my_array.reshape((50, 50)).astype('uint8')*255
最后,您可以轉換為PIL圖像:
im = Image.fromarray(my_array)
我會像這樣一次做到這一切:
im = Image.fromarray(my_array.reshape((50,50)).astype('uint8')*255)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.