GeometrizeTwitterBot  1.0
Python Twitter bot for geometrizing images into geometric primitives
 All Classes Namespaces Files Functions Variables
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
 
def _generate_grayscale_for_image
 
def print_image_to_console
 

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 with minor modifications, BSD license as follows:

Function Documentation

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

Here is the caller graph for this function:

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Here is the call graph for this function:

Here is the caller graph for this function: