Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

build: add dockerfile #25

Merged
merged 3 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions Dockerfile
@@ -0,0 +1,11 @@
FROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "community_version.py"]
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -46,6 +46,12 @@ In order to get started on this project, it is recommended that you watch the se
8. Stare with amazement 😮
9. Start chatting with other ZTM students in the #hacktoberfest-2022 channel on our Discord to get help, work together, and share your contributions!

### ❇️ Run with Docker

1. Install [Docker](https://docs.docker.com/get-docker/)
2. Run `docker build -t ascii-art .`
3. Run `docker run -i ascii-art < path/to/image`

## ❇️ How to contribute?

Now that you see how this command line tool works, let's see how we can evolve it with our ZTM community help!! Maybe we want to display this on an HTML web page where users can submit images and we convert it to ASCII art? Maybe we want to improve how the Command Line Tool works/make it more customizeable? Or maybe modify the script to do many other types of art beyond ASCII.
Expand Down
54 changes: 37 additions & 17 deletions community_version.py
Expand Up @@ -4,6 +4,8 @@
from math import ceil
from random import choice
from time import sleep
from sys import stdin
from io import BytesIO

from PIL import Image, ImageChops
from rich.console import Console
Expand Down Expand Up @@ -120,17 +122,10 @@ def inverse_image_color(image):
return inverted_image


def handle_image_conversion(image_filepath, range_width, inverse_color, color=None):
image = None
try:
image = Image.open(image_filepath)
if inverse_color:
image = inverse_image_color(image)
except Exception as e:
print(f"Unable to open image file {image_filepath}.")
print(e)
return

def handle_image_conversion(image: Image, range_width, inverse_color, color=None):
if inverse_color:
image = inverse_image_color(image)

image_ascii = convert_image_to_ascii(image, range_width=range_width, ASCII_CHARS=ASCII_CHARS)
return image_ascii, color

Expand Down Expand Up @@ -175,6 +170,13 @@ def init_args_parser():
"The result will be great if you choose a svg file extension."
)
)
parser.add_argument(
dest="stdin",
nargs='?',
type=argparse.FileType("rb"),
help="Read image from stdin",
default=stdin
)

args = parser.parse_args()

Expand Down Expand Up @@ -229,15 +231,33 @@ def store_ascii_art():
else:
raise Exception("The value you chose is neither an integer nor a list.")

image_file_path = args.image_file_path

if not image_file_path:
image_file_path = input("Oops, you forgot to specify an Image path: ")
image = None
is_stdin = not args.stdin.isatty()

if is_stdin:
buffer = args.stdin.buffer.read()
image = Image.open(BytesIO(buffer))
else:
image_file_path = args.image_file_path
if not image_file_path:
image_file_path = input("Oops, you forgot to specify an Image path: ")

if image_file_path:
print(image_file_path)
try:
image = Image.open(image_file_path)
except Exception as e:
print(f"Unable to open image file {image_file_path}.")
print(e)
exit(1)


if not image:
raise Exception("No image was loaded")

print(image_file_path)
# convert the image to ASCII art
image_ascii, color = handle_image_conversion(
image_file_path, range_width, args.inverse_image, args.color_ascii
image, range_width, args.inverse_image, args.color_ascii
)
# display the ASCII art to the console
capture = handle_image_print(image_ascii, color, args.store_art)
Expand Down