Coder Social home page Coder Social logo

uvipen / ascii-generator Goto Github PK

View Code? Open in Web Editor NEW
1.5K 36.0 226.0 532.4 MB

ASCII generator (image to text, image to image, video to video)

License: MIT License

Python 100.00%
ascii ascii-art cv2 pil python python3 opencv character-ascii ascii-generator

ascii-generator's Introduction

[PYTHON] ASCII generator

Introduction

Here is my python source code for ASCII generator. With my code:

  • Given input image, we could generate ASCII art stored under text format in different languages (.txt)
  • Given input image, we could generate ASCII art stored under image formats in different languages (.png, .jpg, ...). In each format, there are 2 options: Black background and white characters, or vice versa
  • Given input video, we could generate ASCII art stored under video formats in different languages (.avi, .mp4, ...)
  • Video/image outputs could be in grayscale or color format. It is totally up to you

Multiple Language Conversion

We could generate ASCII art with different alphabets (english, german, french, korean, chinese, japanese, ...). Below are example output:


English


Japanese (Dragon Ball)


German


Korean (Dae Jang-geum)


French


Chinese (Actress)


Spanish


Russian

Video to video

By running the sript video2video_color.py or video2video.py with different values for background and mode, we will have different outputs, for example:


Colored complex-character ASCII output


White-background simple-character ASCII output

Image to text

By running the sript img2txt.py with different values for mode, we will have following outputs:


Input image


Simple character ASCII output


Complex character ASCII output

Image to image

By running the sript img2img_color.py or img2img.py with different values for background and mode, we will have following outputs:


Input image


Colored complex-character ASCII output


White-background simple-character ASCII output


Black-background simple-character ASCII output


White-background complex-character ASCII output


Black-background complex-character ASCII output

Requirements

  • python 3.6
  • cv2
  • PIL
  • numpy

ascii-generator's People

Contributors

uvipen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ascii-generator's Issues

CLI

Awesome project, I love ASCII at all.

You may use https://github.com/pylover/easycli to combine your features in a single command line interface and a python package(setup.py & etc...).

Just remember to use entry_points argument of the setup(...) function to install your command.

Incorrect aspect ratio in output of `img2img.py` (and similars: `img2img_color.py`, `video2video.py` and `video2video_color.py`)

From the demo image (dimension: 976×538, aspect ratio = 976/538 = 1.81):

python3 img2img.py --num_cols 100 --language general --mode complex --background white --output data/output.png gives:

output

which is a picture of dimension 1200×515 (aspect ratio = 1200/515 = 2.33, which is very different from 1.81).

The reason is this line of code: cell_height = scale * cell_width (in line 36 of img2img.py).

The factor cell_height / cell_width needs to be the same as the factor char_height / char_width, so the previous code becomes:

cell_height = (char_height / char_width) * cell_width.

Moreover, the line char_width, char_height = font.getsize(sample_character) generate a warning:
DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.

So on utils.py, all lines similar to char_width, char_height = font.getsize("◊") (with various values for ◊) needs to be replaced by:

char_bbox = font.getbbox("◊")
char_width = char_bbox[2] - char_bbox[0]
char_height = char_bbox[3]

(caution: there is no missing char_bbox[1] in the previous code. And strangely "bottom" really gives the height. See this: python-pillow/Pillow#7802).

This correction must also be made in line 44 of img2img.py: remplace char_width, char_height = font.getsize(sample_character) with

char_bbox = font.getbbox(sample_character)
char_width = char_bbox[2] - char_bbox[0]
char_height = char_bbox[3]

So, by rearranging order of some calculus (because the calculus of cell_height needs to known the value of char_height / char_width), I suggest this correction in the code of img2img.py (extract of the code for the main function):

def main(opt):
    if opt.background == "white":
        bg_code = 255
    else:
        bg_code = 0
    char_list, font, sample_character, scale = get_data(opt.language, opt.mode)
    num_chars = len(char_list)
    num_cols = opt.num_cols
    image = cv2.imread(opt.input)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    height, width = image.shape
    char_bbox = font.getbbox(sample_character)
    char_width = char_bbox[2] - char_bbox[0]
    char_height = char_bbox[3]
    cell_width = width / opt.num_cols
    #cell_height = scale * cell_width
    cell_height = (char_height/char_width) * cell_width
    num_rows = int(height / cell_height)
    if num_cols > width or num_rows > height:
        print("Too many columns or rows. Use default setting")
        cell_width = 6
        #cell_height = 12
        cell_height = (char_height/char_width) * cell_width
        num_cols = int(width / cell_width)
        num_rows = int(height / cell_height)
    #char_width, char_height = font.getsize(sample_character)
    out_width = char_width * num_cols
    out_height = scale * char_height * num_rows
    out_image = Image.new("L", (out_width, out_height), bg_code)
    draw = ImageDraw.Draw(out_image)

For comparison, the old code for the same portion was:

def main(opt):
    if opt.background == "white":
        bg_code = 255
    else:
        bg_code = 0
    char_list, font, sample_character, scale = get_data(opt.language, opt.mode)
    num_chars = len(char_list)
    num_cols = opt.num_cols
    image = cv2.imread(opt.input)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    height, width = image.shape
    cell_width = width / opt.num_cols
    cell_height = scale * cell_width
    num_rows = int(height / cell_height)
    if num_cols > width or num_rows > height:
        print("Too many columns or rows. Use default setting")
        cell_width = 6
        cell_height = 12
        num_cols = int(width / cell_width)
        num_rows = int(height / cell_height)
    char_width, char_height = font.getsize(sample_character)
    out_width = char_width * num_cols
    out_height = scale * char_height * num_rows
    out_image = Image.new("L", (out_width, out_height), bg_code)
    draw = ImageDraw.Draw(out_image)

So, with the modified code, python3 img2img.py --num_cols 100 --language general --mode complex --background white --output data/NewOutput.png gives:

NewOutput

The dimension of this corrected image is 1200×648, and it's aspect ratio is 1200/648=1.85, which is near the 1.81 aspect ratio of the original image.

I will show a more visible difference, by scaling the outputted image, so it's width is the same as the inputed image, and displaying it in a graphic manipulation software, with a transparent backgroung over the inputed image:

Before the correction:

image

After the correction:

image

Help

OpenCV: FFMPEG: tag 0x44495658/'XVID' is not supported with codec id 12 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v'

Language Problem

When I change the character list to katakana or hiragana it just ends up as squares? Like the characters aren't recognised?

How did you get it to work? I installed the languages packs on Windows but still doesn't work.

video

Excuse me, can the video2video.py code cancel the original video in the lower right corner of the video output? Thank you
Annotation 2020-05-12 035804

getsize in PIL dosen't work anymore

in /ASCII-generator/utils.py at char_width, char_height = font.getsize("A") PIL says getsize is deprecated and will be replcaed in PIL 10, error message:

DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.
  char_width, char_height = font.getsize(sample_character)

how to replicate the error:

python3 img2img.py --language chinese

i don't understand PIL a lot and i couldn't find what getsize does so hopefully someone else can fix this.

Use cases

I wonder what is the use cases of ASCII generator?

Rendering Colored Output

Great script, thanks for sharing.

Do you have any thoughts on generating colored ASCII output? Is that possible based on the tools you've strung together?

Undefined name 'out' in video2video.py

flake8 testing of https://github.com/vietnguyen91/ASCII-generator on Python 3.7.1

$ flake8 . --count --select=E901,E999,F821,F822,F823 --show-source --statistics

./video2video.py:79:13: F821 undefined name 'out'
            out
            ^
1     F821 undefined name 'out'
1

E901,E999,F821,F822,F823 are the "showstopper" flake8 issues that can halt the runtime with a SyntaxError, NameError, etc. These 5 are different from most other flake8 issues which are merely "style violations" -- useful for readability but they do not effect runtime safety.

  • F821: undefined name name
  • F822: undefined name name in __all__
  • F823: local variable name referenced before assignment
  • E901: SyntaxError or IndentationError
  • E999: SyntaxError -- failed to compile a file into an Abstract Syntax Tree

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.