Skip to content

Instantly share code, notes, and snippets.

@vince-vibin
Created October 2, 2023 10:00
Show Gist options
  • Save vince-vibin/7ed5c3a7f97d0199434adc177498044c to your computer and use it in GitHub Desktop.
Save vince-vibin/7ed5c3a7f97d0199434adc177498044c to your computer and use it in GitHub Desktop.
Image to ASCII Converter on the Terminal
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)
@vince-vibin
Copy link
Author

Here is an example output:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment