-
Notifications
You must be signed in to change notification settings - Fork 3
/
color_graphics.py
142 lines (119 loc) · 4.39 KB
/
color_graphics.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
##
# Logic class for creating ascii art work from images.
# This file handles the "backend" logic for reading pictures and printing correct values.
# This file was created for the purpose of learning and having fun.
# Created by Aidan Gutierrez March first 2020
##
from PIL import Image
import os
import time
import sys
def resize(path):
image = path
# opens image and assigns it to new variable to preserve original
originImg = Image.open(image, 'r')
tempImg = originImg
# resize with thumbnail caps the size at 100 pixels wide or long and resizes the image propotianally
tempImg.thumbnail((100, 100), Image.LANCZOS)
width, height = tempImg.size
# This resize squishes the picture a bit to minimize elongation
# This along with the skipline variable cuts out elongation whithout distorting the picture too much
tempImg = tempImg.resize((width, height - 10), )
print(tempImg.size)
return tempImg
def convert(path, color):
image = resize(path)
width, height = image.size
# create a list out or pixel rgb values
pix_val = list(image.getdata())
x = 0
skipLine = False
# initialize the picture string.
picture = "'"
# for every single pixel we will observe the R value (red) and determine how dark that pixel is.
# Then assign an approprite ascii character.
for pixel in pix_val:
r, g, b = pixel
if skipLine == False:
if 190 <= r <= 225:
picture = picture + " "
elif 130 <= r <= 189:
picture = picture + "."
elif 70 <= r <= 129:
picture = picture + ":"
elif 31 <= r <= 69:
picture = picture + "!"
elif 16 <= r <= 30:
picture = picture + "|"
elif 0 <= r <= 15:
picture = picture + "I"
#picture = picture + random.choice("QWERTYUIOPASDFGHJKLZXCVBNM") // this prints random letters
else:
picture = picture + " "
else:
picture = picture + ""
# This portion deals with the skip line variable.
# It skips printing every other line to reduce enlongation of an image.
# Elongation is due to the fact that a pixel is smaller than a character.
x += 1
if x % width == 0 and x != 0:
if not skipLine:
picture = picture + "\n"
skipLine = not skipLine
# close picture string
picture += "'"
if color:
os.system('printf ' + picture + '| lolcat')
else:
os.system('printf ' + picture)
# Animation function
def animation(path, color):
x=1
for file in os.listdir(path):
extention = ''
if file.endswith('.jpg') or file.endswith('.jpeg') or file.endswith('.png'):
if file.endswith('.jpg'):
extention = '.jpg'
elif file.endswith('.jpeg'):
extention = '.jpeg'
else:
extention = '.png'
os.system('clear')
convert(path + str(x) + extention, color)
x += 1
time.sleep(.05)
# loop function
def loop(path, times, color):
x = 0
while x < times:
animation(path, color)
x += 1
# loop function
# this turnes a still into a spining animation
def spin(path):
loading = [" "," "," "," "," "," "," "," "," "," "," "," "," "]
# open the image using its path
image = resize(path)
# creates new path in the animations and loops folders
folderName = path.split("/")
folderName = folderName[1].split(".")
aniPath = "animations/" + folderName[0] + "/"
loopPath = "loops/" + folderName[0] + "/"
os.system("mkdir " + aniPath)
os.system("mkdir " + loopPath)
# create a new image so we dont ruin the original
wreckedImg = image
# rotate picture 30 degrees 12 times to get a full circle
# x is set to 1 in order to follow naming conventions for animations
x = 1
while x <= 12:
#create file name to follow convention
#note we use 30 times x because it saves picture quality
filename = str(x) + '.' + folderName[1]
newImg = wreckedImg.rotate(30 * x, resample=Image.BILINEAR, expand=False, fillcolor='white')
newImg.save(aniPath + filename)
newImg.save(loopPath + filename)
loading[x - 1] = '#'
x += 1
sys.stdout.write(f'\r{loading}')
animation(aniPath,False)