I also put a lot of work into optimizing the output line-by-line
- so it needs as few ANSI escape sequences as possible. You can use
- :class:`DebugImage` to visualize these optimizations.
+ so it needs as few ANSI escape sequences as possible. If your
+ terminal is kinda slow, you're gonna want to buy me a drink ;) You
+ can use :class:`DebugImage` to visualize these optimizations.
+ The generated output will only include spaces with different
+ background colors. In the future routines will be provided to
+ overlay text on top of these images.
from PIL import Image as PillsPillsPills
self.img = PillsPillsPills.open(path)
+ # when reading pixels, gifs will return colors corresponding
+ # to a palette if we don't do this :\
+ self.img = self.img.convert("RGBA")
+ """I allow Image to behave as an iterable
+ By using me with a for loop, you can use each line as they're
+ created. When printing a large image, this helps you not have
+ to wait for the whole thing to be converted.
+ :return: Yields lines of text (without line end character)
for line in self.reduce(self.convert()):
+ """I return the entire image as one big string
+ Unlike the iteration approach, you have to wait for the entire
+ :return: String containing all lines joined together.
+ """Returns size of image
def resize(self, width=None):
+ """Resizes image to fit inside terminal
+ Called by the constructor automatically.
width = min(iw, utils.term.width)
self.img = self.img.resize((width, height))
def reduce(self, colors):
+ """Converts color codes into optimized text
+ This optimizer works by merging adjacent colors so we don't
+ have to repeat the same escape codes for each pixel. There is
+ no loss of information.
+ :param colors: Iterable yielding an xterm color code for each
+ pixel, None to indicate a transparent pixel, or
+ ``'EOL'`` to indicate th end of a line.
+ :return: Yields lines of optimized text.
for color, items in itertools.groupby(colors):
color, self.pad * len(list(items))))
+ """Yields xterm color codes for each pixel in image
(width, height) = self.img.size
+ bgcolor = utils.term.bgcolor
color = gf.Color.NewFromRgb(*[c / 255.0 for c in rgba])
- rgba = gf.Color.AlphaBlend(color,
+ rgba = gf.Color.AlphaBlend(color, bgcolor).rgb
*[int(c * 255.0) for c in rgba])
"""I provide a command-line interface for this module
- for imgpath in args
for line in Image(imgpath):
if __name__ == '__main__':