Git Product home page Git Product logo

gif-for-cli's Introduction

gif-for-cli logo

Takes in a GIF, short video, or a query to the Tenor GIF API and converts it to animated ASCII art. Animation and color support are performed using ANSI escape sequences.

Example use cases:

  • run gif-for-cli in your .bashrc or .profile to get an animated ASCII art image as your MOTD!
  • git hooks ;)

This script will automatically detect how many colors the current terminal uses and display the correct version:

Original GIF No Colors Supported 256 Colors Supported 256 Colors Supported (with foreground and background colors) Truecolor Supported
Original GIF No Color Animated ASCII Art 256 Colors Animated ASCII Art 256 FG/BG Colors Animated ASCII Art Truecolor Animated ASCII Art

Installation

Requires Python 3 (with setuptools and pip), zlib, libjpeg, and ffmpeg, other dependencies are installed by setup.py.

Install dependencies:

# Debian based distros
sudo apt-get install ffmpeg zlib* libjpeg* python3-setuptools
# Mac
brew install ffmpeg zlib libjpeg python

Your Python environment may need these installation tools:

sudo easy_install3 pip
# This should enable a pre-built Pillow wheel to be installed, otherwise
# you may need to install Python, zlib, and libjpeg development libraries
# so Pillow can compile from source.
pip3 install --user wheel

Install gif-for-cli:

Install from PyPI:

pip3 install --user gif-for-cli

Or download this repo and run:

python3 setup.py install --user

The gif-for-cli command will likely be installed into ~/.local/bin or similar, you may need to put that directory in your $PATH by adding this to your .profile:

# Linux
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
# Mac, adjust for Python version
if [ -d "$HOME/Library/Python/3.6/bin/" ] ; then
    PATH="$HOME/Library/Python/3.6/bin/:$PATH"
fi

Usage

File/URL

gif-for-cli path/to/some.gif
gif-for-cli http://example.com/foo.gif
gif-for-cli http://example.com/foo.mp4

Executing as a Python module is also supported:

python3 -m gif_for_cli path/to/some.gif

Queries to Tenor's GIF API can also be performed:

# get current top trending GIF
gif-for-cli

# get top GIF for "Happy Birthday"
gif-for-cli "Happy Birthday"

# get GIF with ID #11699608
# browse https://tenor.com/ for more!
gif-for-cli 11699608
gif-for-cli https://tenor.com/view/rob-delaney-peter-deadpool-deadpool2-untitled-deadpool-sequel-gif-11699608

Override display mode

gif-for-cli --display-mode=nocolor 11699608
gif-for-cli --display-mode=256 11699608
gif-for-cli --display-mode=256fgbg 11699608
gif-for-cli --display-mode=truecolor 11699608

Change max width/height

The default number of rows and columns may be too large and result in line wrapping. If you know your terminal size, you can control the output size with the following options:

gif-for-cli --rows 10 --cols 100 11699608

Set to current terminal size:

gif-for-cli --rows `tput lines` --cols `tput cols` 11699608

Note: Generated ASCII art is cached based on the number of rows and columns, so running that command after resizing your terminal window will likely result in the ASCII Art being regenerated.

Loop forever

gif-for-cli -l 0 11699608

Use CTRL + c to exit.

Export/Share

Want to share your generated ASCII Art outside a CLI env (e.g. social media)?

gif-for-cli 11699608 --export=foo.gif

Help

See more generation/display options:

gif-for-cli --help

About Tenor

Tenor is the API that delivers the most relevant GIFs for any application, anywhere in the world. We are the preferred choice for communication products of all types and the fastest growing GIF service on the market.

Check out our API Docs: https://tenor.com/gifapi

Testing

python3 -m unittest discover

With coverage:

coverage run --source gif_for_cli -m unittest discover
coverage report -m

Development

To reuse the shared Git hooks in this repo, run:

git config core.hooksPath git-hooks

Troubleshooting

If you get an error like the following:

-bash: gif-for-cli: command not found

Chances are gif-for-cli was installed in a location not on your PATH. This can happen if running gif-for-cli in your .bashrc, but it was installed into ~/.local/bin, and that directory hasn't been added to your PATH. You can either specify the full path to gif-for-cli to run it, or add its location to your $PATH.

Some of our Favorites

gif-for-cli 10988977
gif-for-cli 5863633
gif-for-cli 5437241

Module Usage

To add gifs to your cli tool include gif-for-cli import and call execute.

import os
import sys

from gif_for_cli.execute import execute

execute(os.environ,
    ["https://tenor.com/view/yay-pokemon-pikachu-gif-8081211"],
    sys.stdout)

Disclaimer

This is not an officially supported Google product.

gif-for-cli's People

Contributors

g-mc avatar pdxjohnny avatar seanhayes 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  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

gif-for-cli's Issues

AttributeError: 'NoneType' object has no attribute 'group' when using remote gif URL

I had the same issue detailed in issue 7 and on deleting the directory specified got the following regex exception instead:

$ gif-for-cli 10988977

Traceback (most recent call last):
File "/home/me/envs/terminal/bin/gif-for-cli", line 11, in
sys.exit(main())
File "/home/me/envs/terminal/lib/python3.6/site-packages/gif_for_cli/main.py", line 105, in main
execute(os.environ, sys.argv[1:], sys.stdout)
File "/home/me/envs/terminal/lib/python3.6/site-packages/gif_for_cli/main.py", line 72, in execute
cpu_pool_size=args.cpu_pool_size,
File "/home/me/envs/terminal/lib/python3.6/site-packages/gif_for_cli/generate/init.py", line 170, in generate
num_frames, seconds = _run_ffmpeg(*options)
File "/home/me/envs/terminal/lib/python3.6/site-packages/gif_for_cli/generate/init.py", line 73, in _run_ffmpeg
num_frames = int(re.search(r'frame=\s
(\d+)', err).group(1))
AttributeError: 'NoneType' object has no attribute 'group'

python3.6 on debian 8.0

Make better image accuracy by improving the algorithm

Now, gif-for-cli still uses a predefined constant . ,\'-:;!" ^/+? *&8#$@% to match the brightness and generate the image, like all similar tools do. This approach does not make effective use of all the visible characters in the ASCII character set, and some uneven symbols like ^ and , would give the output image a "broken" feel.

Perhaps we can introduce a kind of "oversampling" method: since the input is a bitmap, we could also divide the glyph of each character into 𝑛² pieces (say 4 or 9 pieces), and compare in the scaled original image for an equally sized area. By using an optimized π‘˜-d tree (π‘˜ = 𝑛²), the single most accurate character can be found instantly for each 𝑛² pixels. This would be a major improvement for gif-for-cli.

Screenshot 2022-01-03 123405

Screenshot 2022-01-03 125115

Original: http://9front.org/img/9iknowthis01.png

I have written an example implementing this idea for reference (strear/saam), but its code is in C++. Do the maintainers and the pull request people have any good ideas on how to merge this implementation in?

Automation of unit tests.

Through runner.py we can execute all our unit tests in a go.
If you allow, i'll present the code with my PR.

Support for Raspberry Pi

Hi!
I'm trying this out on raspberry pi 3.
I'm encountering the following error:
image

Kindly advise how to set this up in raspberry pi.

Error: get error in testing

I got error in testing after command: python3 -m unittest discover

danghai@ubuntu:~/test/gif-for-cli$ python3 -m unittest discover

..................................EE..........
======================================================================
ERROR: tests.test_export (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_export
Traceback (most recent call last):
  File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
    __import__(name)
  File "/home/danghai/test/gif-for-cli/tests/test_export.py", line 22, in <module>
    from gif_for_cli.export import _export_txt_frames, _get_txt_frames, _run_ffmpeg, export, export_txt_frame
  File "/home/danghai/test/gif-for-cli/gif_for_cli/export.py", line 23, in <module>
    from . import third_party
ImportError: cannot import name 'third_party'


======================================================================
ERROR: tests.test_main (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_main
Traceback (most recent call last):
  File "/usr/lib/python3.5/unittest/loader.py", line 428, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib/python3.5/unittest/loader.py", line 369, in _get_module_from_name
    __import__(name)
  File "/home/danghai/test/gif-for-cli/tests/test_main.py", line 21, in <module>
    from gif_for_cli.__main__ import execute
  File "/home/danghai/test/gif-for-cli/gif_for_cli/__main__.py", line 25, in <module>
    from .export import export
  File "/home/danghai/test/gif-for-cli/gif_for_cli/export.py", line 23, in <module>
    from . import third_party
ImportError: cannot import name 'third_party'


----------------------------------------------------------------------
Ran 46 tests in 0.035s

FAILED (errors=2)

--export cuts off right half of gif when using -c

When I run, for example,
gif-for-cli -c=\ + -ch=1 -cw=1 --rows=100 --cols=90 --display-mode=256 arial\ tramway
the entire gif shows up in the terminal window, but if I add a --export=gif.gif to the end it cuts off the right half of the image like this:
arialtramway_1
Any ideas on how to export a full image and still use multiple characters in -c?

No such file or directory error

anaconda 3, created new environment, pulled the code, installed dependencies. ran setup.

Traceback:
image

This happens no matter which gif I use. The folders are created in the cache directory, with nothing in them

Python3 devel requirement

The readme needs updating with this requirement if you don't install python devel it will fail to compile because of missing Python.h file.

Subgif feature

Could we get a feature to make a sub gif out of a main one?

Something like:
gif-for-cli --start-time 3s --end-time 5s

The above command would make a 2 second gif out of the original

--export does not work with local files

It seems to me that this should be possible:

$ gif-for-cli file://test.json --export test.gif
Processed 24/24 frames...
Exported to:
/home/dtantsur/test.gif

However, the resulting file has no relationship to test.json and is probably a trending gif on your website.

Process and play in separate calls

Would be nice to be able to process the image and play it in separate steps.

Use case: Play a gif after an operation. Pasing can be done earlier so that user does not have to wait for celebratory animation.

gif-for-cli --output=yay.cligif "yay" & # probably something like --quiet would be a good idea
PROC_PID=$!

# … do something

# celebrate
wait $PROC_PID
if [ -r yay.cligif ]
then
  gif-for-cli yay.cligif
  rm yay.cligif
fi

Most (all?) of the options would be valid with --output, but not when playing processed gif.

Option to output to GIF/MP4?

I realize this feature would be the absolute height of software-generated waste but would be helpful for sharing the results online and on social media :)

Can't use gif-for-cli (Float division by zero on execute.py)

archlinux :: ~/Projects/gif-for-cli β€Ήmaster*β€Ί Β» gif-for-cli
Traceback (most recent call last):
File "/home/lark/.local/bin/gif-for-cli", line 33, in
sys.exit(load_entry_point('gif-for-cli==1.1.2', 'console_scripts', 'gif-for-cli')())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lark/.local/lib/python3.11/site-packages/gif_for_cli-1.1.2-py3.11.egg/gif_for_cli/main.py", line 23, in main
execute(os.environ, sys.argv[1:], sys.stdout)
File "/home/lark/.local/lib/python3.11/site-packages/gif_for_cli-1.1.2-py3.11.egg/gif_for_cli/execute.py", line 94, in execute
seconds_per_frame=config['seconds'] / config['num_frames'],
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: float division by zero

ZeroDivisionError: float division by zero

Some GIFs giving this below error:

[16:28:43 abhasker@wsl -> ~$ gif 12122337
Processed 15/15 frames...
Traceback (most recent call last):
  File "/home/abhasker/.local/bin/gif-for-cli", line 11, in <module>
    sys.exit(main())
  File "/home/abhasker/.local/lib/python3.5/site-packages/gif_for_cli/__main__.py", line 89, in main
    execute(os.environ, sys.argv[1:], sys.stdout)
  File "/home/abhasker/.local/lib/python3.5/site-packages/gif_for_cli/__main__.py", line 84, in execute
    seconds_per_frame=config['seconds'] / config['num_frames']
ZeroDivisionError: float division by zero

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.