Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- from PIL import ImageDraw
- from random import randint
- from random import choice
- import cv2
- import numpy
- cell_h = 2.2 / 6 * 58
- cell_w = 1.2 / 4 * 58
- row = 5
- cols = 7
- point_radius = 0.5 * (cell_w + cell_h) * 0.5
- koef = 0.15
- space_count = 1
- max_rotate_degree = 4
- image = Image.new('RGBA', (1000, 1000))
- def empty_arr():
- return [[0] * row for i in range(cols)]
- def print_arr(arr):
- for i in range(len(arr)):
- for j in range(len(arr[i])):
- print(arr[i][j], end='')
- print()
- def minus(value):
- c = choice([True, False])
- if c:
- return -value
- return value
- def draw_point(img, x0, y0, cell_i, cell_j):
- x_p = x0 + cell_j * cell_w + cell_w / 2 + minus(randint(0, int(cell_h * koef)))
- y_p = y0 + cell_i * cell_h + cell_h / 2 + minus(randint(0, int(cell_h * koef)))
- draw = ImageDraw.Draw(img)
- draw.ellipse((x_p - point_radius, y_p - point_radius, x_p + point_radius, y_p + point_radius), fill='black')
- return img
- def draw(img, x0, y0, arr):
- for i in range(len(arr)):
- for j in range(len(arr[i])):
- if arr[i][j] != 0:
- img = draw_point(img, x0, y0, i, j)
- return img
- def function_map():
- d = {}
- count = 0
- lines = []
- with open("1.txt", "r") as ins:
- for line in ins:
- count += 1
- lines.append(line.replace('\n', ''))
- if count % 8 == 0 and count > 0:
- arr = []
- for l in lines[1:]:
- arr.append(list(map(int, list(l))))
- d[lines[0]] = arr
- lines = []
- return d
- def draw_rectangle(draw, coordinates, color, width=1):
- for i in range(width):
- rect_start = (coordinates[0][0] - i, coordinates[0][1] - i)
- rect_end = (coordinates[1][0] + i, coordinates[1][1] + i)
- draw.rectangle((rect_start, rect_end), outline=color, fill=color)
- def generate_future_maps(g):
- w = row * cell_w
- h = cols * cell_h
- outline_width = 10
- outline_color = "black"
- for k, v in g.items():
- image = Image.new('RGBA', (1000, 1000))
- drawing = ImageDraw.Draw(image)
- for el in v:
- top_left = (el[0], el[1])
- bottom_right = (el[0] + w, el[1] + h)
- draw_rectangle(drawing, (top_left, bottom_right), color=outline_color, width=outline_width)
- name = '_' + k + '.png'
- image.save(name)
- def from_pil_cv2(img):
- open_cv_image = numpy.array(img)
- open_cv_image = open_cv_image[:, :, ::-1].copy()
- return open_cv_image
- def transform(img):
- rows, cols, ch = img.shape
- pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
- pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
- M = cv2.getAffineTransform(pts1, pts2)
- dst = cv2.warpAffine(img, M, (cols, rows))
- plt.subplot(121), plt.imshow(img), plt.title('Input')
- plt.subplot(122), plt.imshow(dst), plt.title('Output')
- plt.show()
- def random_rotate(img):
- degree = randint(-max_rotate_degree, max_rotate_degree)
- return img.rotate(degree)
- def draw_text(img, start_x, start_y, text):
- d = function_map()
- gen = {}
- for i, t in enumerate(text):
- y = start_y
- x = start_x + i * cell_w * (row + space_count)
- img = draw(img, x, y, d[t])
- if t in gen:
- gen[t].append([x, y])
- else:
- gen[t] = [[x, y]]
- #generate_future_maps(gen)
- #img = random_rotate(img)
- img.save('test.png')
- if __name__ == '__main__':
- draw_text(image, 100, 100, '02:3.bs')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement