-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.py
More file actions
55 lines (47 loc) · 1.7 KB
/
viewer.py
File metadata and controls
55 lines (47 loc) · 1.7 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
import sys
from math import ceil
try:
import pip
try:
__import__("PIL")
except ImportError:
pip.main(['install', "Pillow"])
except ImportError:
print("Please install pip3 for your version of python")
sys.exit(0)
from PIL import Image
def rgb(red, green, blue, colored=True):
if colored:
return f'\x1b[38;2;{red};{green};{blue}m'
else:
avg = int(sum((red, green, blue)) / 3)
return f'\x1b[38;2;{avg};{avg};{avg}m'
def printPic(file, size, char="███", colorQuality=256, colored=True):
print(rgb(255,255,255))
file = args[1]
file.strip("'")
file.strip('"')
img = Image.open(file)
img.load()
img.thumbnail((size, size))
pixels = list(img.getdata())
width, height = img.size
pixels = [pixels[i * width:(i + 1) * width] for i in range(height)]
for y in pixels:
for x in y:
a = x[3] if len(x) > 3 else 255
x = [x[0], x[1], x[2], a]
x[0] = int((round((x[0] / 256)*colorQuality)/colorQuality)*x[3])
x[1] = int((round((x[1] / 256)*colorQuality)/colorQuality)*x[3])
x[2] = int((round((x[2] / 256)*colorQuality)/colorQuality)*x[3])
if colored:
print(rgb(x[0], x[1], x[2]) + char, end="")
else:
avg = int(sum((x[0], x[1], x[2])) / 3)
print(rgb(avg, avg, avg) + char, end="")
print(rgb(255,255,255))
if __name__ == "__main__":
args = sys.argv
qual = int(args[3]) if len(args) > 3 else 256
colored = int(args[4])==1 if len(args) > 4 else True
printPic(args[1], int(args[2]), colorQuality=qual, colored=colored)