Giter Club home page Giter Club logo

img2txt's Introduction

img2txt

Image to Ascii Text, can output to html or ansi terminal.

See also gif2txt for animated version.

Example

  1. img2txt.py jiaozhu.jpg > without-color.html : demo
  2. img2txt.py jiaozhu.jpg --dither > without-color-dither.html : demo
  3. img2txt.py jiaozhu.jpg --color > with-color.html: demo
  4. img2txt.py jiaozhu.jpg --ansi: demo

Installation

$ virtualenv venv
$ . venv/bin/activate
(venv)$ pip install img2txt.py

Usage

Usage:
  img2txt.py <imgfile> [--maxLen=<n>] [--fontSize=<n>] [--color] [--ansi] [--bgcolor=<#RRGGBB>] [--targetAspect=<n>] [--antialias] [--dither]
  img2txt.py (-h | --help)

Options:
  -h --help             show this screen.
  --ansi                output an ANSI rendering of the image
  --color               output a colored HTML rendering of the image.
  --antialias           causes any resizing of the image to use antialiasing
  --dither              dither the colors to web palette. Useful when converting
                        images to ANSI (which has a limited color palette)
  --fontSize=<n>        sets font size (in pixels) when outputting HTML,
                        default: 7
  --maxLen=<n>          resize image so that larger of width or height matches
                        maxLen, default: 100px
  --bgcolor=<#RRGGBB>   if specified, is blended with transparent pixels to
                        produce the output. In ansi case, if no bgcolor set, a
                        fully transparent pixel is not drawn at all, partially
                        transparent pixels drawn as if opaque
  --targetAspect=<n>    resize image to this ratio of width to height. Default is
                        1.0 (no resize). For a typical terminal where height of a
                        character is 2x its width, you might want to try 0.5 here

Authors

  • @EdRowe (#4, #7)
  • @shakib609 (#10)
  • @mattaudesse (#11)
  • @hit9

License

BSD.

img2txt's People

Contributors

edrowe avatar hit9 avatar mattaudesse avatar mynameisfiber avatar shakib609 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

img2txt's Issues

.png and others cause unpacking tuple error

Line 69 will fail if different (or non) tuple is unpacked.

This may happen if RGBT (transparency) is used (for example .png).

Fixed by changing line 36
From " img = Image.open(imgname)"
To " img = Image.open(imgname).convert ('RGB)"

Provide binary

It's hard to use Python 2 utilities with conda. (No easy way to run a command in specific virtual env AFAIK)

合并相同颜色块的Span以减少体积

比如,一个**人的照片,头发都是黑色的,没必要这么生成html

<span color="#000">x</span><span color="#000">x</span><span color="#000">x</span>

更好的应该这样

<span color="#000">xxx</span>

Possible numpy code optimazation

The code that maps rgb pixel inputs to ascii characters in the generate_grayscale_for_image() function can be written more efficiently by using matrix calculations from numpy. Currently, it looks like this:

for h in range(height):
      for w in range(width):
          rgba = pixels[w, h]
          if rgba[3] != 255 and bgcolor is not None:
              rgba = alpha_blend(rgba, bgcolor)
			# ...
        rgb = rgba[:3]
        string += color[int(sum(rgb) / 3.0 / 256.0 * 16)]
    string += "\n

The calculation int(sum(rgb) / 3.0 / 256.0 * 16) in the inner-body of the loop can be done on the pixels input variable when it's represented as a matrix. The code will likely become less readable, but a comment could be added to elaborate on the new calculations. With this optimization in place, I was able to build a program that converts webcam input images to ascii text with a really decent framerate.

Release new version ?

Hi, thanks for this really cool tool !

I've noticed the version available on Pypi doesn't contains the range/xrange fix (and so is not compatible with Python 3). Could you release a new version to fix this ?

I have to admit I fix this by myself when trying the tool, then when I was about to send a PR about this, I realised it was already fixed in the repo ;-)

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.