Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- from PIL import Image
- def convert_image_to_ansi(image_path):
- try:
- img = Image.open(image_path)
- width, height = img.size
- if width > 80:
- # Calculate new dimensions preserving aspect ratio
- aspect_ratio = height / width
- new_width = 80
- new_height = round(aspect_ratio * new_width)
- width = new_width
- height = new_height
- img = img.resize((width, height))
- new_height = round(height / 2.15) # terminal cells are taller than wide
- img = img.resize((width, new_height))
- if img.mode != 'RGB':
- img = img.convert('RGB')
- ansi_output = ""
- for y in range(new_height):
- for x in range(width):
- r, g, b = img.getpixel((x, y))
- ansi_output += f"\033[48;2;{r};{g};{b}m "
- ansi_output += "\033[0m\n" # reset color at EOL
- return ansi_output
- except Exception as e:
- return f"Error processing image: {e}"
- if __name__ == "__main__":
- if len(sys.argv) < 2:
- print("python image_to_ansi.py <image_path>")
- sys.exit(1)
- image_path = sys.argv[1]
- ansi_art = convert_image_to_ansi(image_path)
- print(ansi_art)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement