Created
October 2, 2023 10:00
-
-
Save vince-vibin/7ed5c3a7f97d0199434adc177498044c to your computer and use it in GitHub Desktop.
Image to ASCII Converter on the Terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PIL.Image | |
# use two hashtags to get a square image | |
char = "##" | |
def display_image(image): | |
width, height = image.size | |
ascii_image = [] | |
for y in range(height): | |
line = [] | |
for x in range(width): | |
pixel = image.getpixel((x, y)) | |
termPixel = f"\033[38;2;{pixel[0]};{pixel[1]};{pixel[2]}m{char}\033[0m" | |
line.append(termPixel) | |
ascii_image.append("".join(line)) | |
for line in ascii_image: | |
print(line) | |
if __name__ == "__main__": | |
image = PIL.Image.open("break64.jpeg") ## IMAGE | |
display_image(image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an example output: