python 生成随机图片

x33g5p2x  于2021-11-29 转载在 Python  
字(0.8k)|赞(0)|评价(0)|浏览(920)

python numpy生成随机图

  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import cv2
  4. grayImage=np.random.randint(0, 255, (400, 400))
  5. cv2.imshow("GRAY", grayImage.astype(np.uint8))
  6. bgrImage=np.random.randint(0, 255, (400, 400,3))
  7. cv2.imshow("bgr", bgrImage.astype(np.uint8))
  8. # cv2.imshow("BGR", bgrImage)
  9. cv2.waitKey(0)

python 生成随机圆

以下内容转自:

OpenCV python 绘制随机实心圆

  1. import numpy as np
  2. import cv2
  3. def main():
  4. # 1.创建白色背景图片
  5. d = 400
  6. img = np.ones((d, d, 3), np.uint8) * 255
  7. # 2.循环随机绘制实心圆
  8. for i in range(0, 100):
  9. # 随机中心点
  10. center_x = np.random.randint(0, high=d)
  11. center_y = np.random.randint(0, high=d)
  12. # 随机半径与颜色
  13. radius = np.random.randint(5, high=d/5)
  14. color = np.random.randint(0, high=256, size=(3, )).tolist()
  15. cv2.circle(img, (center_x, center_y), radius, color, -1)
  16. # 3.显示结果
  17. cv2.imshow("img", img)
  18. cv2.waitKey()
  19. cv2.destroyAllWindows()
  20. if __name__ == '__main__':
  21. main()

相关文章