-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (80 loc) · 2.98 KB
/
Copy pathmain.py
File metadata and controls
103 lines (80 loc) · 2.98 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from PIL import Image
from math import floor
from pillow_heif import register_heif_opener
# from os import remove, listdir, path
# from time import time
# used to open and work with helf/heic images in PIL
register_heif_opener()
# Open image
def open(image):
return Image.open(image)
# Rotate image counter-clockwise by an angle
def rotate(image, angle):
return image.rotate(angle, expand = 1)
# Calculate new dimentions of the image, maintaining aspect ratio
def new_dims(w, h, mw, mh, agent):
# aspect ratio
r = h / w
# The ASCII character glyphs are taller than they are wide. Maintain the aspect
# ratio by reducing the image height
nw = mw
nh = int(nw * r * 0.6)
# If width or height is greater than its max value,
# limit it to the max value and calculate other dimension
if agent == "mobile":
if nh > mh:
nh = mh
nw = int(nh / (r * 0.6))
else:
if w > h:
if nh > mh:
nh = mh
nw = int(nh / (r * 0.55))
else:
nh = mh
nw = int(nh / (r * 0.55))
return nw, nh
# Get size of image
def size(image):
return image.size
# Resize image maintaining aspect ratio
def resize(image, width, height):
# width_percent = (new_width/float(image.size[0]))
# new_height = int((float(image.size[1])*float(width_percent)))
# return image.resize((new_width, new_height), Image.Resampling.LANCZOS)
# orig_width, orig_height = image.size
# r = orig_height / orig_width
# The ASCII character glyphs are taller than they are wide. Maintain the aspect
# ratio by reducing the image height.
# height = int(width * r * 0.6)
return image.resize((width, height), Image.Resampling.LANCZOS)
# Convert image to grayscale
def grayscale(image):
return image.convert("L")
# Convert pixel string to ascii character string
def ascii(image, den):
# Get pixels of image
pixels = image.getdata()
ascii_string = ""
# For each pixel, map its value to ascii character and append it to the ascii_string
for pixel in pixels:
ascii_string += den[map(pixel, 0, 255, 0, len(den) - 1)]
return ascii_string
# Maps value from one range to a value in another range
def map(value, start1, stop1, start2, stop2):
OldRange = (stop1 - start1)
NewRange = (stop2 - start2)
mappedValue = (((value - start1) * NewRange) / OldRange) + start2
return floor(mappedValue)
# Remove sessions in /tmp/ folder
# def remove_sessions():
# dir_path = '/tmp/'
# Get the current time in seconds
# current_time = time()
# Iterate through all files in the /tmp/ folder
# for file_name in listdir(dir_path):
# file_path = path.join(dir_path, file_name)
# Check and delete file if it has no extension and was created more than 10 mins ago
# if path.isfile(file_path) and '.' not in file_name:
# if current_time - path.getctime(file_path) > 600:
# remove(file_path)