Created
June 7, 2024 05:42
-
-
Save Psychevus/657155359e60dca29b360a53920f03ca to your computer and use it in GitHub Desktop.
This Python script converts images to ASCII art. The resulting ASCII art is saved as a text file. The script processes all .jpg, .jpeg, and .png files in the current directory, excluding a file named image.jpg.
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 os | |
import cv2 | |
DEFAULT_WIDTH = 900 | |
DEFAULT_HEIGHT = 900 | |
ASCII_CHARS = "@%#*+=-:. " | |
def get_image_files(): | |
image_files = [f for f in os.listdir('.') if os.path.isfile(f) and ( | |
f.endswith('.jpg') or f.endswith('.jpeg') or f.endswith('.png')) and f != 'image.jpg'] | |
return image_files | |
def calculate_new_size(width, height, max_width, max_height): | |
aspect_ratio = width / height | |
new_width = max_width | |
new_height = int(max_width / aspect_ratio / 1.5) | |
if new_height > max_height: | |
new_height = max_height | |
new_width = int(max_height * aspect_ratio * 1.5) | |
return new_width, new_height | |
def scale_image(image, new_width, new_height): | |
return cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA) | |
def map_pixels_to_ascii(image, ascii_chars): | |
grayscale_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
grayscale_image = 255 - grayscale_image | |
pixel_indices = (grayscale_image / 255) * (len(ascii_chars) - 1) | |
pixel_indices = pixel_indices.astype(int) | |
ascii_image = [] | |
for row in pixel_indices: | |
line = ''.join([ascii_chars[pixel] for pixel in row]) | |
ascii_image.append(line) | |
return ascii_image | |
def save_ascii_image(ascii_image, filename): | |
with open(filename, 'w', encoding='utf-8') as file: | |
for line in ascii_image: | |
file.write(line + '\n') | |
def delete_dark_image(): | |
if os.path.exists('image.jpg'): | |
os.remove('image.jpg') | |
def process_images(max_width=DEFAULT_WIDTH, max_height=DEFAULT_HEIGHT): | |
image_files = get_image_files() | |
for image_file in image_files: | |
img = cv2.imread(image_file) | |
new_width, new_height = calculate_new_size(img.shape[1], img.shape[0], max_width, max_height) | |
img_resized = scale_image(img, new_width, new_height) | |
ascii_image = map_pixels_to_ascii(img_resized, ASCII_CHARS) | |
output_filename = os.path.splitext(image_file)[0] + '.txt' | |
save_ascii_image(ascii_image, output_filename) | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser(description="Convert images to ASCII art.") | |
parser.add_argument("--width", type=int, default=DEFAULT_WIDTH, help="Maximum width of the ASCII art.") | |
parser.add_argument("--height", type=int, default=DEFAULT_HEIGHT, help="Maximum height of the ASCII art.") | |
args = parser.parse_args() | |
process_images(max_width=args.width, max_height=args.height) | |
delete_dark_image() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment