Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Color to Terminal Printed File #36

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
Source: https://github.com/github/gitignore/blob/master/Python.gitignore

# User generated
ENV/
.vscode
.idea
.DS_Store
.history


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.pytest_cache/

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
.static_storage/
.media/
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

# js
node_modules/
.next/

# poetry
poetry.lock
.vercel
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,23 @@ Here's the algorithm -
## How to use ASCIIFY
- Ensure you have the required dependency "PIL" for Python installed. (pip install pillow)
- Clone the repo
- Run the python script, and pass the image path as the parameter
- Run the python script, and pass the image path as the parameter:
- For the Black and White version run: python3 asciify.py file_name
- For the Colour version run: python3 asciify_color.py file_name
- The script will print the output in the terminal, and will also write into a file 'img.txt' in the same directory as the python script
- Profit!

-------------------------------------------------------------------------------------------------------
## Future Plans
- Alternatively support colored outputs by printing the text onto an image


-------------------------------------------------------------------------------------------------------
## Sources

- Pillow <https://pillow.readthedocs.io/en/stable/reference/Image.html?highlight=getdata#PIL.Image.Image.getdata>
- Stack Overflow <https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal>

-------------------------------------------------------------------------------------------------------
## Support Me
If you liked this, leave a star! :star:
Expand Down
182 changes: 91 additions & 91 deletions asciify.py
Original file line number Diff line number Diff line change
@@ -1,91 +1,91 @@
from PIL import Image
ASCII_CHARS = ['.',',',':',';','+','*','?','%','S','#','@']
ASCII_CHARS = ASCII_CHARS[::-1]
'''
method resize():
- takes as parameters the image, and the final width
- resizes the image into the final width while maintaining aspect ratio
'''
def resize(image, new_width=100):
(old_width, old_height) = image.size
aspect_ratio = float(old_height)/float(old_width)
new_height = int(aspect_ratio * new_width)
new_dim = (new_width, new_height)
new_image = image.resize(new_dim)
return new_image
'''
method grayscalify():
- takes an image as a parameter
- returns the grayscale version of image
'''
def grayscalify(image):
return image.convert('L')
'''
method modify():
- replaces every pixel with a character whose intensity is similar
'''
def modify(image, buckets=25):
initial_pixels = list(image.getdata())
new_pixels = [ASCII_CHARS[pixel_value//buckets] for pixel_value in initial_pixels]
return ''.join(new_pixels)
'''
method do():
- does all the work by calling all the above functions
'''
def do(image, new_width=100):
image = resize(image)
image = grayscalify(image)
pixels = modify(image)
len_pixels = len(pixels)
# Construct the image from the character list
new_image = [pixels[index:index+new_width] for index in range(0, len_pixels, new_width)]
return '\n'.join(new_image)
'''
method runner():
- takes as parameter the image path and runs the above code
- handles exceptions as well
- provides alternative output options
'''
def runner(path):
image = None
try:
image = Image.open(path)
except Exception:
print("Unable to find image in",path)
#print(e)
return
image = do(image)
# To print on console
print(image)
# Else, to write into a file
# Note: This text file will be created by default under
# the same directory as this python file,
# NOT in the directory from where the image is pulled.
f = open('img.txt','w')
f.write(image)
f.close()
'''
method main():
- reads input from console
- profit
'''
if __name__ == '__main__':
import sys
import urllib.request
if sys.argv[1].startswith('http://') or sys.argv[1].startswith('https://'):
urllib.request.urlretrieve(sys.argv[1], "asciify.jpg")
path = "asciify.jpg"
else:
path = sys.argv[1]
runner(path)
from PIL import Image

ASCII_CHARS = ['.',',',':',';','+','*','?','%','S','#','@']
ASCII_CHARS = ASCII_CHARS[::-1]

'''
method resize():
- takes as parameters the image, and the final width
- resizes the image into the final width while maintaining aspect ratio
'''
def resize(image, new_width=100):
(old_width, old_height) = image.size
aspect_ratio = float(old_height)/float(old_width)
new_height = int(aspect_ratio * new_width)
new_dim = (new_width, new_height)
new_image = image.resize(new_dim)
return new_image
'''
method grayscalify():
- takes an image as a parameter
- returns the grayscale version of image
'''
def grayscalify(image):
return image.convert('L')

'''
method modify():
- replaces every pixel with a character whose intensity is similar
'''
def modify(image, buckets=25):
initial_pixels = list(image.getdata())
new_pixels = [ASCII_CHARS[pixel_value//buckets] for pixel_value in initial_pixels]
return ''.join(new_pixels)

'''
method do():
- does all the work by calling all the above functions
'''
def do(image, new_width=100):
image = resize(image)
image = grayscalify(image)

pixels = modify(image)
len_pixels = len(pixels)

# Construct the image from the character list
new_image = [pixels[index:index+new_width] for index in range(0, len_pixels, new_width)]

return '\n'.join(new_image)

'''
method runner():
- takes as parameter the image path and runs the above code
- handles exceptions as well
- provides alternative output options
'''
def runner(path):
image = None
try:
image = Image.open(path)
except Exception:
print("Unable to find image in",path)
#print(e)
return
image = do(image)

# To print on console
print(image)

# Else, to write into a file
# Note: This text file will be created by default under
# the same directory as this python file,
# NOT in the directory from where the image is pulled.
f = open('img.txt','w')
f.write(image)
f.close()

'''
method main():
- reads input from console
- profit
'''
if __name__ == '__main__':
import sys
import urllib.request
if sys.argv[1].startswith('http://') or sys.argv[1].startswith('https://'):
urllib.request.urlretrieve(sys.argv[1], "asciify.jpg")
path = "asciify.jpg"
else:
path = sys.argv[1]
runner(path)
Loading