r, g, b, a = img.split() img = Image.merge("RGBA", (b, g, r, a)) dit = Dither(BPP, img.size[0], img.size[1], DEPTH * img.size[0], RMASK, GMASK, BMASK, AMASK) except DitherError, err: sys.stderr.write("%s\n" % err) sys.exit(127) #set dither algorithm if ditalgo: dit.set_algorithm(ditalgo) #set brightness if brightness: dit.set_brightness(brightness) #set gamma if gamma: dit.set_gamma(gamma) #set contrast if contrast: dit.set_contrast(contrast) #set charset if charset: dit.set_charset(charset) #create dither dit.bitmap(cv, 0, 0, width, height, img.tostring())
def to_ascii( img, width=60, height=None, font_width=6, font_height=10, brightness=1.0, contrast=1.0, gamma=1.0, ditalgo="fstein", exformat="ansi", charset="ascii"): """ Takes a file-pointer to an image and converts it to ASCII. Options: -h, --help This help -v, --version Version of the program -W, --width=WIDTH Width of resulting image -H, --height=HEIGHT Height of resulting image -x, --font-width=WIDTH Width of output font -y, --font-height=HEIGHT Height of output font -b, --brightness=BRIGHTNESS Brightness of resulting image -c, --contrast=CONTRAST Contrast of resulting image -g, --gamma=GAMMA Gamma of resulting image -d, --dither=DITHER Dithering algorithm to use -f, --format=FORMAT Format of the resulting image -C, --charset=CHARSET Charset of the resulting image DITHER (ditalgo) list: - none: no dithering - ordered2: 2x2 ordered dithering - ordered4: 4x4 ordered dithering - ordered8: 8x8 orederd dithering - random: random dithering - fstein: Floyd-Steinberg dithering FORMAT (exformat) list: - caca: native libcaca format - ansi: ANSI - utf8: UTF-8 with ANSI escape codes - utf8cr: UTF-8 with ANSI escape codes and MS-DOS \\r - html: HTML - html3: backwards-compatible HTML - bbfr: BBCode (French) - irc: IRC with mIRC colours - ps: PostScript document - svg: SVG vector image - tga: TGA image - troff: troff source CHARSET (charset) list: - ascii: use only ascii character - shades: use unicode character - blocks: use unicode quarter-cell combinations """ img = Image.open(img) if "shades" or "blocks" in charset: exformat = "utf8" # Will not work in ascii mode # Explicitly encode argument strings as ASCII ditalgo = ditalgo.encode('ascii') exformat = exformat.encode('ascii') charset = charset.encode('ascii') # Set height to some proportion if height is None: height = round(width * img.size[1] * font_width / img.size[0] / font_height) # Setup the canvas cv = Canvas(width, height) cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_TRANSPARENT) ######################### #### Begin Dithering #### ######################### RMASK = 0x00ff0000 GMASK = 0x0000ff00 BMASK = 0x000000ff AMASK = 0xff000000 BPP = 32 DEPTH = 4 if img.mode == 'RGB': img = img.convert('RGBA') #reorder rgba if img.mode == 'RGBA': r, g, b, a = img.split() img = Image.merge("RGBA", (b, g, r, a)) dit = Dither(BPP, img.size[0], img.size[1], DEPTH * img.size[0], RMASK, GMASK, BMASK, AMASK) # print(dit.get_algorithm_list()); # import ipdb; ipdb.set_trace() dit.set_algorithm(ditalgo) dit.set_brightness(brightness) dit.set_gamma(gamma) dit.set_contrast(contrast) dit.set_charset(charset) dit.bitmap(cv, 0, 0, width, height, img.tobytes()) ######################### #### End Dithering ###### ######################### return cv.export_to_memory(exformat)
def asciiart(img, SC, GCF): img = cv2.convertScaleAbs(img, alpha=GCF, beta=0) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = Image.fromarray(img) RMASK = 0x00ff0000 GMASK = 0x0000ff00 BMASK = 0x000000ff AMASK = 0xff000000 BPP = 32 DEPTH = 4 brightness = None contrast = None gamma = None ditalgo = None exformat = "svg" charset = None width = int(img.size[1] * 2.23 * SC) height = int(img.size[0] * 0.75 * SC) cv = Canvas(width, height) cv.set_color_ansi(caca.COLOR_DEFAULT, caca.COLOR_TRANSPARENT) try: # convert rgb to rgba if img.mode == 'RGB': img = img.convert('RGBA') # reorder rgba if img.mode == 'RGBA': r, g, b, a = img.split() img = Image.merge("RGBA", (b, g, r, a)) dit = Dither(BPP, img.size[0], img.size[1], DEPTH * img.size[0], RMASK, GMASK, BMASK, AMASK) except DitherError as err: print(err) if ditalgo: dit.set_algorithm(ditalgo) # set brightness if brightness: dit.set_brightness(brightness) # set gamma if gamma: dit.set_gamma(gamma) # set contrast if contrast: dit.set_contrast(contrast) # set charset if charset: dit.set_charset(charset) #create dither dit.bitmap(cv, 0, 0, width, height, img.tobytes()) asciiArt = cv.export_to_memory(exformat) ############################################################## bgcolor = 'white' font = ImageFont.truetype("DejaVuSansMono.ttf", 11) #letter_width = font.getsize("X")[0] letter_height = font.getsize("X")[1] lines = asciiArt.split('\n') canvasHeight = int(lines[1].split('"')[3]) canvasWidth = int(lines[1].split('"')[1]) newImg = Image.new("RGB", (canvasWidth, canvasHeight), bgcolor) draw = ImageDraw.Draw(newImg) for char in lines[3:-3]: type = char.split('"')[0].split(" ")[0][1:] x = int(char.split('"')[3]) y = int(char.split('"')[5]) color = char.split('"')[1].split(":")[1] if type == "text": character = str(char.split('"')[-1][1:2]) draw.text((x, y - letter_height), character, color, font=font) else: rectWidth = int(char.split('"')[-4]) rectheight = int(char.split('"')[-2]) draw.rectangle(((x, y), (x + rectWidth, y + rectheight)), fill=color) finalImg = np.asarray(newImg) finalImg = cv2.cvtColor(finalImg, cv2.COLOR_BGR2RGB) return finalImg