-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASCII
More file actions
87 lines (74 loc) · 3.53 KB
/
ASCII
File metadata and controls
87 lines (74 loc) · 3.53 KB
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
# importing required libraries
import pygame as pg
import cv2
import numpy as np
# creating a converter which converts our image to ascii image
class Converter:
def _init_(self, path="Monalisa_1", font_size=12, color_lvl=8):
pg.init() # initializing pygame
self.path = path # setting the path to image file
self.COLOR_LVL = color_lvl
self.cv2_image = cv2.imread(self.path) # reading the image using opencv
self.image, self.gray_image = self.get_image()
self.RES = self.WIDTH, self.HEIGHT = self.image.shape[0], self.image.shape[1]
self.surface = pg.display.set_mode(self.RES) # creating pygame display surface
self.clock = pg.time.Clock() # creating pygame clock for controlling the frame rate
self.ASCII_CHARS = 'ixzao*#MW&8%B@$' # defining ascii characters
self.ASCII_C0EFF = 255 // (len(self.ASCII_CHARS) - 1)
self.font = pg.font.SysFont('Courier', font_size, bold=True) # creating pygame font object
self.CHAR_STEP = int(font_size * 0.6)
self.PALETTE, self.COLOR_COEFF = self.create_palette()
def draw_converted_image(self):
char_indices = self.gray_image // self.ASCII_C0EFF
color_indices = self.image // self.COLOR_COEFF
for x in range(0, self.WIDTH, self.CHAR_STEP):
for y in range(0, self.HEIGHT, self.CHAR_STEP):
char_index = char_indices[x, y]
if char_index:
char = self.ASCII_CHARS[char_index]
color = tuple(color_indices[x, y])
self.surface.blit(self.PALETTE[char][color], (x, y))
# generating color palette
def create_palette(self):
colors, color_coeff = np.linspace(0, 255, num=self.COLOR_LVL, dtype=int, retstep=True)
color_palette = [np.array([r, g, b]) for r in colors for g in colors for b in colors]
palette = dict.fromkeys(self.ASCII_CHARS, None)
color_coeff = int(color_coeff)
for char in palette:
char_palette = {}
for color in color_palette:
color_key = tuple(color // color_coeff)
char_palette[color_key] = self.font.render(char, False, tuple(color))
palette[char] = char_palette
return palette, color_coeff
# converting the image to rgb and grayscale
def get_image(self):
transposed_image = cv2.transpose(self.cv2_image)
image = cv2.cvtColor(transposed_image, cv2.COLOR_BGR2RGB)
gray_image = cv2.cvtColor(transposed_image, cv2.COLOR_BGR2GRAY)
return image, gray_image
def draw_cv2_image(self):
resized_cv2_image = cv2.resize(self.cv2_image, (640, 360), interpolation=cv2.INTER_AREA)
cv2.imshow("img", resized_cv2_image) # displaying the opencv image
def draw(self):
self.surface.fill('black')
self.draw_converted_image()
self.draw_cv2_image()
# saving the image
def save_image(self):
pg.image.save(self.surface, 'output1.jpg')
def run(self):
while True:
for i in pg.event.get():
if i.type == pg.QUIT:
pg.quit()
quit()
self.draw()
pg.display.set_caption(str(self.clock.get_fps()))
pg.display.flip() # Update the display
self.clock.tick() # Set the desired frames per second (e.g., 30)
cv2.imshow('img', self.cv2_image)
self.save_image() # Save the image automatically without pressing 's'
if _name_ == "_main_":
app = Converter()
app.run()