You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.0 KiB
Plaintext
75 lines
2.0 KiB
Plaintext
11 months ago
|
#!/usr/bin/env python
|
||
|
|
||
|
import requests
|
||
|
from PIL import Image
|
||
|
import sys
|
||
|
import os
|
||
|
|
||
|
img_url = 'https://blog.finxter.com/wp-content/uploads/2022/04/greenland_02a.jpg'
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
if len(sys.argv) < 2:
|
||
|
print("No file given as argument")
|
||
|
exit(1)
|
||
|
|
||
|
input_file = sys.argv[1]
|
||
|
img = Image.new("RGBA", (0,0)) # create empty image to override
|
||
|
if os.path.isfile(input_file):
|
||
|
img = Image.open(input_file)
|
||
|
if not os.path.isfile(input_file):
|
||
|
img_url = f'https://raw.githubusercontent.com/msikma/pokesprite/master/pokemon-gen7x/regular/{input_file}'
|
||
|
img = Image.open(requests.get(img_url, stream = True).raw)
|
||
|
|
||
|
output = ""
|
||
|
last_color = ""
|
||
|
|
||
|
bb = [0, 0, img.size[0] - 1, img.size[1] - 1]
|
||
|
while bb[0] < img.size[0] - 1:
|
||
|
if any([img.getpixel((bb[0], y))[3] != 0 for y in range(img.size[1])]):
|
||
|
break
|
||
|
else:
|
||
|
bb[0] += 1
|
||
|
|
||
|
while bb[1] < img.size[1] - 1:
|
||
|
if any([img.getpixel((x, bb[1]))[3] != 0 for x in range(img.size[0])]):
|
||
|
break
|
||
|
else:
|
||
|
bb[1] += 1
|
||
|
|
||
|
while bb[2] > bb[0]:
|
||
|
if any([img.getpixel((bb[2], y))[3] != 0 for y in range(img.size[1])]):
|
||
|
break
|
||
|
else:
|
||
|
bb[2] -= 1
|
||
|
|
||
|
while bb[3] > bb[1]:
|
||
|
if any([img.getpixel((x, bb[3]))[3] != 0 for x in range(img.size[0])]):
|
||
|
break
|
||
|
else:
|
||
|
bb[3] -= 1
|
||
|
|
||
|
# output += "\n\n"
|
||
|
for y in range(bb[1], bb[3] + 1):
|
||
|
# output += " "
|
||
|
for x in range(bb[0], bb[2] + 1):
|
||
|
[r, g, b, a] = img.getpixel((x, y))
|
||
|
if a > 0:
|
||
|
curr_color = f'48;2;{r};{g};{b}'
|
||
|
else:
|
||
|
curr_color = "0"
|
||
|
|
||
|
if curr_color != last_color:
|
||
|
output += f'\033[{curr_color}m'
|
||
|
last_color = curr_color
|
||
|
|
||
|
output += " "
|
||
|
if last_color != "0":
|
||
|
output += "\033[0m"
|
||
|
last_color = "0"
|
||
|
|
||
|
if y < bb[3]:
|
||
|
output += "\n"
|
||
|
|
||
|
# output += "\n"
|
||
|
print(output)
|