GeometrizeTwitterBot  1.0
Python Twitter bot for geometrizing images into geometric primitives
Functions
image_console_printer Namespace Reference

This module contains code that converts image data to formats suitable for printing to console. More...

Functions

def _alpha_blend (src, dst)
 
def _generate_grayscale_for_image (pixels, width, height, bgcolor)
 
def print_image_to_console (img)
 

Detailed Description

This module contains code that converts image data to formats suitable for printing to console.

Based on https://github.com/hit9/img2txt by @hit9 with minor modifications, BSD license as follows:

Function Documentation

◆ _alpha_blend()

def image_console_printer._alpha_blend (   src,
  dst 
)
private
37 def _alpha_blend(src, dst):
38  # Does not assume that dst is fully opaque
39  # See https://en.wikipedia.org/wiki/Alpha_compositing - section on "Alpha Blending"
40  src_multiplier = (src[3] / 255.0)
41  dst_multiplier = (dst[3] / 255.0) * (1 - src_multiplier)
42  result_alpha = src_multiplier + dst_multiplier
43  if result_alpha == 0: # special case to prevent div by zero below
44  return (0, 0, 0, 0)
45  else:
46  return (
47  int(((src[0] * src_multiplier) + (dst[0] * dst_multiplier)) / result_alpha),
48  int(((src[1] * src_multiplier) + (dst[1] * dst_multiplier)) / result_alpha),
49  int(((src[2] * src_multiplier) + (dst[2] * dst_multiplier)) / result_alpha),
50  int(result_alpha * 255)
51  )
52 
Here is the caller graph for this function:

◆ _generate_grayscale_for_image()

def image_console_printer._generate_grayscale_for_image (   pixels,
  width,
  height,
  bgcolor 
)
private
53 def _generate_grayscale_for_image(pixels, width, height, bgcolor):
54 
55  # grayscale
56  color = "MNHQ$OC?7>!:-;. "
57 
58  string = ""
59  # first go through the height, otherwise will rotate
60  for h in range(height):
61  for w in range(width):
62 
63  rgba = pixels[w, h]
64 
65  # If partial transparency and we have a bgcolor, combine with bg
66  # color
67  if rgba[3] != 255 and bgcolor is not None:
68  rgba = _alpha_blend(rgba, bgcolor)
69 
70  # Throw away any alpha (either because bgcolor was partially
71  # transparent or had no bg color)
72  # Could make a case to choose character to draw based on alpha but
73  # not going to do that now...
74  rgb = rgba[:3]
75 
76  string += color[int(sum(rgb) / 3.0 / 256.0 * 16)]
77 
78  string += "\n"
79 
80  return string
81 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_image_to_console()

def image_console_printer.print_image_to_console (   img)
82 def print_image_to_console(img):
83  if img.mode != 'RGBA':
84  img = img.convert('RGBA')
85 
86  fill_string = "\x1b[49m"
87  fill_string += "\x1b[K" # does not move the cursor
88  sys.stdout.write(fill_string)
89 
90  pixels = img.load()
91  sys.stdout.write(_generate_grayscale_for_image(pixels, img.width, img.height, None))
92 
93  # Undo residual color changes, output newline because
94  # generate_ANSI_from_pixels does not do so
95  # removes all attributes (formatting and colors)
96  sys.stdout.write("\x1b[0m\n")
Here is the call graph for this function:
Here is the caller graph for this function:
image_console_printer._alpha_blend
def _alpha_blend(src, dst)
Definition: image_console_printer.py:37
image_console_printer.print_image_to_console
def print_image_to_console(img)
Definition: image_console_printer.py:82
image_console_printer._generate_grayscale_for_image
def _generate_grayscale_for_image(pixels, width, height, bgcolor)
Definition: image_console_printer.py:53