Guest User

Untitled

a guest
May 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. # coding=utf-8
  2. '''
  3. python gen_cannon.py [img_n] [show_each]
  4. img_n: integer, generate img_n cannon images with random color
  5. show_each:
  6. 1, show each image
  7. 0, O/W
  8.  
  9. e.g.
  10. $ python gen_cannon.py 3 1
  11. generate 3 cannon images with random color and show each image.
  12. '''
  13. import sys
  14. import os
  15.  
  16. import numpy as np
  17. import cv2 as cv
  18. import matplotlib.pyplot as plt
  19.  
  20. W = 512
  21. H = 512
  22. margin = 50
  23. cannon_w = 100 # 砲管寬
  24. cannon_h = 300 # 砲管高
  25. radius = 75 # 兩大圓圈
  26. # 內部小圈
  27. cir_r = 10 # 半徑
  28. cir_n = radius//cir_r # 數量
  29. # 大砲前端
  30. head_bottom = 150 # 寬
  31. head_top = 120 # 寬
  32. head_h = 125 # 上半部分高度
  33. head_lh = 20 # 下半部分的高度
  34.  
  35.  
  36. def rand_color():
  37. return np.random.randint(0, 256, size=3, dtype=np.uint8).tolist()
  38.  
  39.  
  40. def shadow(color):
  41. return [round(i*0.4) for i in color] # power-law
  42.  
  43.  
  44. def gen_cannon(name, bg_color=[255, 255, 255], c_color=[255, 488, 242]):
  45.  
  46. img = np.zeros((W, H, 3), np.uint8) # BGR
  47. img[:] = bg_color
  48. color = c_color
  49.  
  50. # 座標最左上是 (0,0)
  51. mid = W//2
  52. # 底座
  53. cv.rectangle(img, (margin, H-margin+1), (W-margin, H-1), (0, 0, 0), -1)
  54. # 砲管
  55. connon_pos_y = H-margin-cannon_h
  56. cv.rectangle(img, (mid-cannon_w//2, connon_pos_y),
  57. (mid+cannon_w//2, H-margin), color, -1) # 左上和右下的點
  58. # shadow
  59. pts2 = np.array([[mid, H-margin],
  60. [mid+cannon_w//2, H-margin],
  61. [mid+cannon_w//2, connon_pos_y]], np.int32)
  62. cv.fillPoly(img, pts2.reshape(-1, 3, 2), shadow(color))
  63.  
  64. # 兩圓圈
  65. cv.circle(img, (mid-cannon_w//2-1-radius, H-margin-radius),
  66. radius, color, -1)
  67. cv.circle(img, (mid+cannon_w//2+1+radius, H-margin-radius),
  68. radius, color, -1)
  69. # 內部小圈
  70. for i, rc in zip(range(1, cir_n), [rand_color() for i in range(cir_n-1)]):
  71. cv.circle(img, (mid-cannon_w//2-1-radius, H-margin-radius),
  72. radius-i*cir_r, rc, -1)
  73. cv.circle(img, (mid+cannon_w//2+1+radius, H-margin-radius),
  74. radius-i*cir_r, rc, -1)
  75.  
  76. # 大砲前端
  77. # 左上、右上、右下、左下
  78. pts1 = np.array([[mid-head_top//2, connon_pos_y-head_h-head_lh-1],
  79. [mid+head_top//2, connon_pos_y-head_h-head_lh-1],
  80. [mid+head_bottom//2, connon_pos_y-head_lh-1],
  81. [mid-head_bottom//2, connon_pos_y-head_lh-1]], np.int32)
  82. pts2 = np.array([[mid-head_bottom//2, connon_pos_y-head_lh],
  83. [mid+head_bottom//2, connon_pos_y-head_lh],
  84. [mid+head_top//2, connon_pos_y-1],
  85. [mid-head_top//2, connon_pos_y-1]], np.int32) # shadow1
  86. pts3 = np.array([[mid+head_bottom//4, connon_pos_y-head_lh-1],
  87. [mid+head_bottom//2, connon_pos_y-head_lh-1],
  88. [mid+head_top//2, connon_pos_y-head_h-head_lh-1]],
  89. np.int32) # shadow2
  90.  
  91. # cv.polylines(img, pts1.reshape((-1, 4, 2)), True, color, 5) # can't fill with color (thickness=-1)
  92. cv.fillPoly(img, pts1.reshape((-1, 4, 2)), color)
  93. cv.fillPoly(img, pts2.reshape((-1, 4, 2)), shadow(color))
  94. cv.fillPoly(img, pts3.reshape((-1, 3, 2)), shadow(color))
  95.  
  96. return img
  97.  
  98.  
  99. def show(img, name):
  100. # show
  101. p_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
  102. plt.figure(num="The Armstrong Cannon")
  103. # plt.xticks([]), plt.yticks([])
  104. plt.title(name)
  105. plt.tight_layout()
  106. plt.imshow(p_img)
  107. plt.show()
  108.  
  109.  
  110. def main():
  111. n, show_each = 1, False
  112. if len(sys.argv) > 1:
  113. try:
  114. n = int(sys.argv[1])
  115. except:
  116. print("Not a number!! So I just give it one~")
  117. if len(sys.argv) > 2:
  118. try:
  119. show_each = int(sys.argv[2])
  120. except:
  121. print("I guess that you don't want to show each image.")
  122.  
  123. for i in range(n):
  124. bg_color = rand_color()
  125. c_color = rand_color()
  126. name = "(Gintama)cannon"+str(i)+".jpg"
  127. img = gen_cannon(name, c_color=c_color)
  128. cv.imwrite(os.path.join(os.getcwd(), name), img)
  129. if show_each:
  130. show(img, name)
  131.  
  132. print("-"*20, "generate "+str(n)+" image(s)", sep='\n')
  133.  
  134.  
  135. if __name__ == "__main__":
  136. main()
Add Comment
Please, Sign In to add comment