Quickstart¶
Basic setup¶
- note:
this lib is developed on python 3.8, and is not guaranteed to work on other versions.
- installing:
to install or update from PyPi, run
pip install -U asciipy-any
to install with optional dependencies for youtube and urls, runpip install -U asciipy-any[full]
for all optional dependencies, orpip install -U asciipy-any[youtube]
/pip install -U asciipy-any[url]
- video support:
to properly support video conversion, you will need to install ffmpeg manually.
Examples¶
Simple CLI¶
1from asciipy import ImageConverter, BackgroundConfig 2import sys 3 4back = BackgroundConfig(enabled=True, alpha=True) 5# create a background config that copys the sources alpha channel 6 7img = ImageConverter(None, back) 8# instantiate an image converter with the standard config, and the custom background config 9 10img.convert(sys.argv[1], './ascii.png') 11# convert an image from a path passed as an argument 12 13print(f"{sys.argv[1]} converted and written to ./ascii.png")
Gray-Scale-image-converter¶
1from asciipy import ImageConverter, ConverterConfig, BackgroundConfig 2from typing import Tuple 3import sys 4 5conf = ConverterConfig(transparent=True) 6back = BackgroundConfig(enabled=True, alpha=True, darken=1) 7# create our custom configs 8 9 10# create a custom converter class that creates grayscale images 11class GrayScaleImage(ImageConverter): 12 def _get_back_color(self, col: Tuple[int, int, int, int]) -> Tuple[int, int, int, int]: 13 gray = 0 14 for i in col: 15 gray += i 16 gray = round(gray / 3) 17 dark = self._darken_rgb(gray, gray, gray) 18 return *dark, col[3] 19 20 def _get_color(self, col: Tuple[int, int, int, int]) -> Tuple[int, int, int, int]: 21 gray = 0 22 for i in col: 23 gray += i 24 gray = round(gray / 3) 25 return (gray, gray, gray, col[3]) 26 27img = GrayScaleImage(conf, back) # instantiate our custom converter 28 29img.convert(sys.argv[1], 'gray.png') # convert an image from a path passed as an argument 30 31print(f"{sys.argv[1]} converted and written to ./gray.png")
Hacky Gray-Scale converters¶
1from asciipy import BaseConverter, ImageConverter, VideoConverter, ConverterConfig 2from typing import Tuple 3import sys 4 5conf = ConverterConfig(transparent=True) 6 7def _get_color(self, col: Tuple[int, int, int, int]) -> Tuple[int, int, int, int]: 8 gray = 0 9 for i in col: 10 gray += i 11 gray = round(gray / 3) 12 return (gray, gray, gray, col[3]) 13 14BaseConverter._get_color = _get_color 15# doing it this way allows us to change behavior for all converters 16# without having to inherit from them, or modify each separately. 17 18 19im = ImageConverter(conf) 20 21im.convert(sys.argv[1], './gray.png') 22 23vid = VideoConverter(conf) 24 25vid.convert(sys.argv[2], './gray.mp4')