python 生成白色畫布方法:
參考資料: https://stackoverflow.com/questions/10465747/how-to-create-a-white-image-in-python
方法一:
In [7]: import numpy as np In [8]: width = 100 In [9]: height = 200 In [10]: image = np.zeros([height, width, 3], dtype=np.uint8) In [11]: unique, counts = np.unique(image, return_counts=True) In [12]: unique Out[12]: array([0], dtype=uint8) In [13]: image.fill(255) In [14]: np.unique(image) Out[14]: array([255], dtype=uint8)
方法二:
In [1]: import numpy as np In [2]: width = 100 In [3]: height = 200 In [4]: image = 255 * np.ones((height, width, 3), dtype=np.uint8) In [5]: image.shape Out[5]: (200, 100, 3) In [6]: np.unique(image) Out[6]: array([255], dtype=uint8)
生成黑色畫布:
In [8]: image = np.zeros((height, width, 3), dtype=np.uint8) In [9]: np.unique(image) Out[9]: array([0], dtype=uint8)
生成有顏色的純色畫布:
In [10]: image = [75, 19, 77] * np.ones((640, 480, 3), np.uint8) In [11]: np.unique(image) Out[11]: array([19, 75, 77])
注意: np.unique是得到數組中存在的不重復的元素,具體用法可以看 https://www.cnblogs.com/ttweixiao-IT-program/p/13895384.html
以上的方法只要你理解了,圖片的本質就是一個二維數組或者三維的數組,當數組的類型為uint8時,最大的255就是白色,最小的0就是黑色,然后在設置好圖形形狀之后,你就可以根據填充你想要的像素值得到相應的純色圖片啦~