├── PythonASCII.py └── README.md /PythonASCII.py: -------------------------------------------------------------------------------- 1 | import PIL 2 | from PIL import Image 3 | 4 | img_flag = True 5 | path = input("Enter the path to the image field : \n") 6 | 7 | try: 8 | img = PIL.Image.open(path) 9 | img_flag = True 10 | except: 11 | print(path, "Unable to find image "); 12 | 13 | width, height = img.size 14 | aspect_ratio = height/width 15 | new_width = 120 16 | new_height = aspect_ratio * new_width * 0.55 17 | img = img.resize((new_width, int(new_height))) 18 | 19 | img = img.convert('L') 20 | 21 | chars = ["@", "J", "D", "%", "*", "P", "+", "Y", "$", ",", "."] 22 | 23 | pixels = img.getdata() 24 | new_pixels = [chars[pixel//25] for pixel in pixels] 25 | new_pixels = ''.join(new_pixels) 26 | new_pixels_count = len(new_pixels) 27 | ascii_image = [new_pixels[index:index + new_width] for index in range(0, new_pixels_count, new_width)] 28 | ascii_image = "\n".join(ascii_image) 29 | 30 | with open("ascii_image.txt", "w") as f: 31 | f.write(ascii_image) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image to ASCII Converter 2 | 3 | Convert any image into ASCII art with depth and shading. This Python script generates a text file containing ASCII characters that represent your image, preserving visual details like shadows and depth. 4 | 5 | ## Features 6 | 7 | - Converts images to ASCII art while preserving depth and shadows 8 | - Supports common image formats (JPG, PNG, etc.) 9 | - Generates output as a text file in the same directory as the input image 10 | - Simple command-line interface 11 | 12 | ## Prerequisites 13 | 14 | - Python 3.9.10 or later 15 | - PIL (Python Imaging Library) 16 | 17 | ## Installation 18 | 19 | 1. Clone the repository: 20 | ```bash 21 | git clone https://github.com/NarendraYSF/Python-image-to-ascii.git 22 | cd Python-image-to-ascii 23 | ``` 24 | 25 | 2. Install required dependencies: 26 | ```bash 27 | pip install Pillow 28 | ``` 29 | 30 | ## Usage 31 | 32 | 1. Open your terminal or command prompt 33 | 34 | 2. Navigate to the project directory: 35 | ```bash 36 | cd path/to/Python-image-to-ascii 37 | ``` 38 | 39 | 3. Run the script: 40 | ```bash 41 | python3 PythonASCII.py 42 | ``` 43 | 44 | 4. When prompted, enter the path to your image: 45 | ```bash 46 | Enter the path to the image file: Image.jpg 47 | ``` 48 | 49 | ## Output 50 | 51 | The script will generate a text file containing the ASCII art in the same directory as your input image. The output filename will be based on your input image name with a `.txt` extension. 52 | 53 | ## Example 54 | 55 | Input image: `python.jpg` 56 | ![Alt text](https://raw.githubusercontent.com/docker-library/docs/01c12653951b2fe592c1f93a13b4e289ada0e3a1/python/logo.png) 57 | 58 | 59 | Output file: `python.txt` 60 | 61 | ```.txt 62 | +++++*+*****************++***++ 63 | +++*********************************++ 64 | =****************************************+ 65 | +*******************************************+ 66 | +******=::.:-+********************************+ 67 | +*****-. .=********************************+ 68 | +*****+. .********************************* 69 | +******:. .-*********************************= 70 | +*******=. .=**********************************= 71 | +*********++*************************************= 72 | +************************************************= 73 | +************************************************= 74 | ++++++++++++++++++++++++************************= 75 | :************************= 76 | +++*************************************************************= .::::::::::::::: 77 | =+*****************************************************************= .::::::::::::::::: 78 | ++*******************************************************************= .:::::::::::::::::: 79 | +*********************************************************************= .:::::::::::::::::::. 80 | +**********************************************************************= .:::::::::::::::::::: 81 | +***********************************************************************= .::::::::::::::::::::: 82 | =+*********************************************************************** :::::::::::::::::::::: 83 | +***********************************************************************+ ::::::::::::::::::::::: 84 | +***********************************************************************= ::::::::::::::::::::::: 85 | -***********************************************************************= ::::::::::::::::::::::::. 86 | +*********************************************************************+ :::::::::::::::::::::::::: 87 | +*******************************************************************++ :::::::::::::::::::::::::::: 88 | +**********************************+***************************+*++ :::::::::::::::::::::::::::::: 89 | +******************************+= ::::::::::::::::::::::::::::::::::: 90 | +***************************+ :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 91 | +*************************+ :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 92 | =************************= :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 93 | -***********************= ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 94 | +*********************+ .:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 95 | =*********************= ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 96 | +******************** .:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 97 | =*******************+ ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::. 98 | +******************+ :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 99 | +*****************+ ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 100 | +****************+ :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 101 | +**************+ :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 102 | =+*++++++++++ :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::. 103 | ::::::::::::::::::::::::: 104 | ::::::::::::::::::::::::::::::::::::::::::::::::: 105 | :::::::::::::::::::::::::::::::::::::::::::::::::: 106 | :::::::::::::::::::::::::::::::::::::::::::::::::: 107 | :::::::::::::::::::::::::::::::::::::::::::::::::: 108 | :::::::::::::::::::::::::::::::::::. .:::::::: 109 | ::::::::::::::::::::::::::::::::::. .:::::: 110 | :::::::::::::::::::::::::::::::::. .:::::: 111 | ::::::::::::::::::::::::::::::::::. .::::::: 112 | :::::::::::::::::::::::::::::::::::...:::::::: 113 | :::::::::::::::::::::::::::::::::::::::::: 114 | :::::::::::::::::::::::::::::::::::::: 115 | :::::::::::::::::::::::::::::::::: 116 | :::::::::::::::::::::::: 117 | ``` 118 | 119 | 120 | ## Contributing 121 | 122 | Feel free to open issues or submit pull requests for any improvements or bug fixes. 123 | 124 | ## License 125 | 126 | This project is open source and available under the MIT License. 127 | 128 | ## Author 129 | 130 | NarendraYSF 131 | 132 | ## Acknowledgments 133 | 134 | Special thanks to the Python Imaging Library (PIL) team for providing the image processing capabilities that make this project possible. 135 | --------------------------------------------------------------------------------