Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. from PIL import Image
  2. from PIL import ImageDraw
  3. from random import randint
  4. from random import choice
  5. import cv2
  6. import numpy
  7.  
  8. cell_h = 2.2 / 6 * 58
  9. cell_w = 1.2 / 4 * 58
  10.  
  11. row = 5
  12. cols = 7
  13. point_radius = 0.5 * (cell_w + cell_h) * 0.5
  14. koef = 0.15
  15. space_count = 1
  16. max_rotate_degree = 4
  17.  
  18. image = Image.new('RGBA', (1000, 1000))
  19.  
  20.  
  21. def empty_arr():
  22. return [[0] * row for i in range(cols)]
  23.  
  24.  
  25. def print_arr(arr):
  26. for i in range(len(arr)):
  27. for j in range(len(arr[i])):
  28. print(arr[i][j], end='')
  29. print()
  30.  
  31.  
  32. def minus(value):
  33. c = choice([True, False])
  34. if c:
  35. return -value
  36. return value
  37.  
  38.  
  39. def draw_point(img, x0, y0, cell_i, cell_j):
  40. x_p = x0 + cell_j * cell_w + cell_w / 2 + minus(randint(0, int(cell_h * koef)))
  41. y_p = y0 + cell_i * cell_h + cell_h / 2 + minus(randint(0, int(cell_h * koef)))
  42.  
  43. draw = ImageDraw.Draw(img)
  44. draw.ellipse((x_p - point_radius, y_p - point_radius, x_p + point_radius, y_p + point_radius), fill='black')
  45. return img
  46.  
  47.  
  48. def draw(img, x0, y0, arr):
  49. for i in range(len(arr)):
  50. for j in range(len(arr[i])):
  51. if arr[i][j] != 0:
  52. img = draw_point(img, x0, y0, i, j)
  53. return img
  54.  
  55.  
  56. def function_map():
  57. d = {}
  58. count = 0
  59. lines = []
  60.  
  61. with open("1.txt", "r") as ins:
  62. for line in ins:
  63. count += 1
  64. lines.append(line.replace('\n', ''))
  65. if count % 8 == 0 and count > 0:
  66. arr = []
  67. for l in lines[1:]:
  68. arr.append(list(map(int, list(l))))
  69. d[lines[0]] = arr
  70. lines = []
  71. return d
  72.  
  73.  
  74. def draw_rectangle(draw, coordinates, color, width=1):
  75. for i in range(width):
  76. rect_start = (coordinates[0][0] - i, coordinates[0][1] - i)
  77. rect_end = (coordinates[1][0] + i, coordinates[1][1] + i)
  78. draw.rectangle((rect_start, rect_end), outline=color, fill=color)
  79.  
  80.  
  81. def generate_future_maps(g):
  82. w = row * cell_w
  83. h = cols * cell_h
  84.  
  85. outline_width = 10
  86. outline_color = "black"
  87.  
  88. for k, v in g.items():
  89. image = Image.new('RGBA', (1000, 1000))
  90. drawing = ImageDraw.Draw(image)
  91. for el in v:
  92. top_left = (el[0], el[1])
  93. bottom_right = (el[0] + w, el[1] + h)
  94. draw_rectangle(drawing, (top_left, bottom_right), color=outline_color, width=outline_width)
  95. name = '_' + k + '.png'
  96. image.save(name)
  97.  
  98.  
  99. def from_pil_cv2(img):
  100. open_cv_image = numpy.array(img)
  101. open_cv_image = open_cv_image[:, :, ::-1].copy()
  102. return open_cv_image
  103.  
  104. def transform(img):
  105.  
  106. rows, cols, ch = img.shape
  107.  
  108. pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
  109. pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
  110.  
  111. M = cv2.getAffineTransform(pts1, pts2)
  112.  
  113. dst = cv2.warpAffine(img, M, (cols, rows))
  114.  
  115. plt.subplot(121), plt.imshow(img), plt.title('Input')
  116. plt.subplot(122), plt.imshow(dst), plt.title('Output')
  117. plt.show()
  118.  
  119.  
  120. def random_rotate(img):
  121. degree = randint(-max_rotate_degree, max_rotate_degree)
  122. return img.rotate(degree)
  123.  
  124.  
  125. def draw_text(img, start_x, start_y, text):
  126. d = function_map()
  127. gen = {}
  128.  
  129. for i, t in enumerate(text):
  130. y = start_y
  131. x = start_x + i * cell_w * (row + space_count)
  132.  
  133. img = draw(img, x, y, d[t])
  134.  
  135. if t in gen:
  136. gen[t].append([x, y])
  137. else:
  138. gen[t] = [[x, y]]
  139. #generate_future_maps(gen)
  140. #img = random_rotate(img)
  141.  
  142. img.save('test.png')
  143.  
  144.  
  145. if __name__ == '__main__':
  146. draw_text(image, 100, 100, '02:3.bs')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement