python 生成白畫布,黑畫布和指定顏色畫布(純白色圖片或黑色圖片或純色圖片)(python generate white, black or pure color canva)


 

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就是黑色,然后在設置好圖形形狀之后,你就可以根據填充你想要的像素值得到相應的純色圖片啦~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM