1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
from PIL import Image
from math import floor
import os
import pygame
import cv2
import numpy as np
import glob
def find_index(num, min_num, max_num, min_range, max_range):
n = num / (max_num - min_num + 1)
return (max_range - min_range + 1) * n + min_range
def convert_image(img):
pix = img.load()
# density = "Ñ@#W$9876543210?!abc;:+=-,._ "
# density = density[::-1]
density = ' .:-i|=+%O#@'
density_length = len(density)
output = []
for y in range(height):
line = ""
for x in range(width):
r = pix[x, y][0]
g = pix[x, y][1]
b = pix[x, y][2]
# r,g,b = pix[x,y]
avg = (r + g + b) / 3
avg = min(avg * 2, 255)
density_index = floor(find_index(avg, 0, 255, 0, density_length - 1))
line += density[density_index]
output.append(line)
return output
class Label:
def __init__(self, x, y, text, font_size, text_colour=(0, 0, 0), antialias=True):
self.position = pygame.Vector2(x, y)
self.font = pygame.font.Font("cour.ttf", font_size)
self.text_colour = text_colour
self.antialias = antialias
self.text, self.width, self.height = self.create_text(text)
def create_text(self, text):
out = []
max_width = 0
max_height = 0
for line in text:
text_surface = self.font.render(line, self.antialias, self.text_colour)
width = text_surface.get_width()
height = text_surface.get_height()
out.append((text_surface, width, height))
max_width = max(max_width, width)
max_height += height
return out, max_width, max_height
def update(self, new_text=None):
if new_text is not None:
self.text, self.width, self.height = self.create_text(new_text)
def draw(self, surface):
prev_height = 0
for line in self.text:
text_surface, width, height = line
surface.blit(text_surface, self.position + pygame.Vector2(0, prev_height))
prev_height += height
pygame.init()
directory = 'C:/Users/admon/PycharmProjects/coding_club/text_art/video'
output_directory = 'C:/Users/admon/PycharmProjects/coding_club/text_art/video_output/'
for i, file_name in enumerate(os.listdir(directory)):
img = Image.open(directory + '/' + file_name)
width, height = img.size
resize = 0.05
img = img.resize((floor(width * resize), floor(height * resize)))
width, height = img.size
x = Label(0,0, convert_image(img), 12, text_colour=(255,255,255))
SCREEN_WIDTH = x.width
SCREEN_HEIGHT = x.height
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
x.draw(screen)
pygame.display.update()
pygame.image.save(screen, output_directory + file_name)
img_array = []
for file_name in glob.glob('C:/Users/admon/PycharmProjects/coding_club/text_art/video_output/*.png'):
img = cv2.imread(file_name)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
out = cv2.VideoWriter('rick roll.avi', cv2.VideoWriter_fourcc(*'DIVX'), 5, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
pygame.quit()
|