-
Notifications
You must be signed in to change notification settings - Fork 0
/
icat.py
102 lines (91 loc) · 3.27 KB
/
icat.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
import cairosvg
from numpy import asarray
import os
import pathlib
from PIL import Image, ImageOps, ImageFilter
import sys
"""
This file contains the core functionality of ICat. This file is exectuted by a bash file
named "icat" which passes user supplied args to here.
"""
"""
Function to print images to dark terminal backgrounds. If this function was used on a light
terminal background, the image printed will appear inverted. The same is true for
printImageLightBG() on dark terminal backgrounds.
"""
def printImageDarkBG():
data = asarray(image)
for i in range(data.shape[0]):
# One char represents one pixel.
for j in range(data.shape[1]):
if data[i][j] >= 204:
print("█", end="")
elif data[i][j] >= 153:
print("▓", end="")
elif data[i][j] >= 102:
print("▒", end="")
elif data[i][j] >= 51:
print("░", end="")
else:
print(" ", end="")
print()
# Function to print images to light terminal backgrounds.
def printImageLightBG():
data = asarray(image)
for i in range(data.shape[0]):
for j in range(data.shape[1]):
if data[i][j] >= 204:
print(" ", end="")
elif data[i][j] >= 153:
print("░", end="")
elif data[i][j] >= 102:
print("▒", end="")
elif data[i][j] >= 51:
print("▓", end="")
else:
print("█", end="")
print()
if pathlib.Path(sys.argv[1]).suffix == ".svg":
cairosvg.svg2png(url=sys.argv[1], write_to=("icat-output.png"))
image = Image.open("icat-output.png")
else:
image = Image.open(sys.argv[1])
"""
icat_settings.cfg will determine whether images are printed in full width or half width
pixels. Half width pixels have two times as much horizontal detail as full width ones.
icat_settings.cfg will also determine the right function to use when printing to terminal
with a light or dark background.
"""
settings = open("/usr/local/bin/icat_settings.cfg", "r")
if "half" in settings.readline():
width, height = image.size
# Resizes image to be 50% as wide since image will be printed in half width "pixels".
image = image.resize((width, round(height / 2)), Image.LANCZOS)
# Is a multiplier used in doubling horizontal detail for half width pixels.
scaleX = 2
else:
# Is a multiplier used in keeping horizontal detail the same (for full width pixels).
scaleX = 1
options = sys.argv[2:]
if "0x1.5" in options:
# .convert("L") converts image to black and white.
image = ImageOps.contain(image, (60 * scaleX, 60)).convert("L")
elif "0x2" in options:
image = ImageOps.contain(image, (80 * scaleX, 80)).convert("L")
else:
# Default size printed if user gives no args.
image = ImageOps.contain(image, (40 * scaleX, 40)).convert("L")
if "0-s" in options:
image = image.filter(ImageFilter.SHARPEN)
if "0-c" in options:
image = image.filter(ImageFilter.CONTOUR)
if "0-i" in options:
image = ImageOps.invert(image)
if "light" in settings.readline():
printImageLightBG()
else:
printImageDarkBG()
image.close()
if pathlib.Path(sys.argv[1]).suffix == ".svg":
os.remove("icat-output.png")
settings.close()