def pil_to_ascii(img, scalefactor=0.2, invert=False, equalize=True, lut='simple', aspect_correction_factor=None ): """ Generates an ascii string from a PIL image. Parameters ---------- img : PIL.Image PIL image to transform. scalefactor : float ASCII characters per pixel. invert : bool Invert luminance? equalize : bool equalize histogram (for best results do this). lut : str Name of the lookup table to use. Currently supports 'simple' and 'binary'. Returns ------- str Examples -------- >>> from asciisciit.misc import open_pil_img >>> img = open_pil_img("http://i.imgur.com/l2FU2J0.jpg") >>> text_img = pil_to_ascii(img, scalefactor=0.3) >>> print(text_img) >>> from PIL import Image >>> img = Image.open("some_image.png") >>> text_img = pil_to_ascii(img) >>> print(text_img) """ lookup = get_lut(lut) if aspect_correction_factor is None: aspect_correction_factor = get_aspect_correction_factor(lookup.exemplar) img = img.resize( (int(img.size[0]*scalefactor), int(img.size[1]*scalefactor*aspect_correction_factor)), Image.BILINEAR) img = img.convert("L") # convert to mono if equalize: img = ImageOps.equalize(img) if invert: img = ImageOps.invert(img) img = np.array(img, dtype=np.uint8) return u"\n" + u"".join(lookup.apply(img).flatten().tolist())
def pil_to_ascii(img, scalefactor=0.2, invert=False, equalize=True, lut='simple', aspect_correction_factor=None): """ Generates an ascii string from a PIL image. Parameters ---------- img : PIL.Image PIL image to transform. scalefactor : float ASCII characters per pixel. invert : bool Invert luminance? equalize : bool equalize histogram (for best results do this). lut : str Name of the lookup table to use. Currently supports 'simple' and 'binary'. Returns ------- str Examples -------- >>> from asciisciit.misc import open_pil_img >>> img = open_pil_img("http://i.imgur.com/l2FU2J0.jpg") >>> text_img = pil_to_ascii(img, scalefactor=0.3) >>> print(text_img) >>> from PIL import Image >>> img = Image.open("some_image.png") >>> text_img = pil_to_ascii(img) >>> print(text_img) """ lookup = get_lut(lut) if aspect_correction_factor is None: aspect_correction_factor = get_aspect_correction_factor( lookup.exemplar) img = img.resize( (int(img.size[0] * scalefactor), int(img.size[1] * scalefactor * aspect_correction_factor)), Image.BILINEAR) img = img.convert("L") # convert to mono if equalize: img = ImageOps.equalize(img) if invert: img = ImageOps.invert(img) img = np.array(img, dtype=np.uint8) return u"\n" + u"".join(lookup.apply(img).flatten().tolist())
def numpy_to_ascii(img, scalefactor=0.2, invert=False, equalize=True, lut="simple", aspect_correction_factor=None): """ Generates an ascii string from a numpy image. Parameters ---------- img : ndarray PIL image to transform. scalefactor : float ASCII characters per pixel. invert : bool Invert luminance? equalize : bool equalize histogram (for best results do this). lut : str Name of the lookup table to use. Currently supports 'simple' and 'binary'. Returns ------- str """ lookup = get_lut(lut) if aspect_correction_factor is None: aspect_correction_factor = get_aspect_correction_factor(lookup.exemplar) h, w = img.shape img = cv2.resize( img, ( int(w*scalefactor), int(h*scalefactor*aspect_correction_factor) ) ) if img.ndim == 3: # weak check for RGB # works in opencv 3.4.3 but who knows, they keep moving/renaming stuff img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) if equalize: img=cv2.equalizeHist(img) if invert: img = 255-img return u"\n" + u"".join(lookup.apply(img).flatten().tolist())
def numpy_to_ascii(img, scalefactor=0.2, invert=False, equalize=True, lut="simple", aspect_correction_factor=None): """ Generates an ascii string from a numpy image. Parameters ---------- img : ndarray PIL image to transform. scalefactor : float ASCII characters per pixel. invert : bool Invert luminance? equalize : bool equalize histogram (for best results do this). lut : str Name of the lookup table to use. Currently supports 'simple' and 'binary'. Returns ------- str """ lookup = get_lut(lut) if aspect_correction_factor is None: aspect_correction_factor = get_aspect_correction_factor( lookup.exemplar) h, w = img.shape img = cv2.resize(img, (int( w * scalefactor), int(h * scalefactor * aspect_correction_factor))) if img.ndim == 3: # weak check for RGB # works in opencv 3.4.3 but who knows, they keep moving/renaming stuff img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) if equalize: img = cv2.equalizeHist(img) if invert: img = 255 - img return u"\n" + u"".join(lookup.apply(img).flatten().tolist())
def lut(self, val): self._lut = val lookup = get_lut(val) self.aspect_correction_factor = get_aspect_correction_factor( lookup.exemplar, self.font_path) # default correction factor for converting
def test_get_lut(): assert(u"\u3105\u3106\u3107\u3108" not in lut.UNICODE_LUTS) l1 = lut.get_lut("\u3105\u3106\u3107\u3108") assert(u"\u3105\u3106\u3107\u3108" in lut.UNICODE_LUTS) l2 = lut.get_lut("\u3105\u3106\u3107\u3108")