本文整理汇总了Python中caca.canvas.Canvas.export_to_memory方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.export_to_memory方法的具体用法?Python Canvas.export_to_memory怎么用?Python Canvas.export_to_memory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类caca.canvas.Canvas
的用法示例。
在下文中一共展示了Canvas.export_to_memory方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import export_to_memory [as 别名]
def main():
""" Main function. """
try:
pig = Canvas(0, 0)
pig.import_from_memory(STRING, "text")
cv = Canvas(pig.get_width() * 2, pig.get_height() * 2)
except CanvasError as err:
sys.stderr.write("%s\n" % err)
sys.exit(2)
cv.blit(0, 0, pig, NullCanvas())
pig.flip()
cv.blit(pig.get_width(), 0, pig, NullCanvas())
pig.flip()
pig.flop()
cv.blit(0, pig.get_height(), pig, NullCanvas())
pig.flop()
pig.rotate_180()
cv.blit(pig.get_width(), pig.get_height(), pig, NullCanvas())
for j in range(0, cv.get_height()):
for i in range(0, cv.get_width(), 2):
cv.set_color_ansi(caca.COLOR_LIGHTBLUE + (i + j) % 6,
caca.COLOR_DEFAULT)
a = cv.get_attr(-1, -1)
cv.put_attr(i, j, a)
cv.put_attr(i+1, j, a)
print("%s" % cv.export_to_memory('utf8'))
cv.rotate_left()
print("%s" % cv.export_to_memory('utf8'))
示例2: test_draw_thin_line
# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import export_to_memory [as 别名]
def test_draw_thin_line(self):
""" module canvas: Canvas.draw_thin_line()
"""
cv = Canvas(10, 1)
cv.draw_thin_line(0, 0, 10, 1)
self.assertEqual("-" * 10, cv.export_to_memory("utf8").strip('\n'))
self.assertRaises(CanvasError, cv.draw_thin_line, 0, "a", 10, 1)
示例3: main
# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import export_to_memory [as 别名]
def main():
""" Main function. """
if len(sys.argv) < 3:
sys.stderr.write("Usage: %s <figfont file> <word>\n" \
% os.path.basename(sys.argv[0]))
sys.exit(2)
try:
cv = Canvas(0, 0)
except CanvasError as err:
sys.stderr.write("%s\n" % err)
sys.exit(2)
if cv.set_figfont(sys.argv[1]):
sys.stderr.write("Could not open font...\n")
sys.exit(2)
if sys.version_info[0:2] >= (3,0):
word = sys.argv[2]
else:
word = codecs.decode(sys.argv[2], "utf8")
for c in word:
cv.put_figchar(c)
sys.stderr.write(cv.export_to_memory("utf8"))
示例4: test_clear
# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import export_to_memory [as 别名]
def test_clear(self):
""" module canvas: Canvas.clear()
"""
cv = Canvas(5, 1)
cv.put_str(0, 0, "test!")
cv.clear()
data = cv.export_to_memory("utf8").strip('\n')
self.assertEqual(" ", data)
示例5: test_blit
# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import export_to_memory [as 别名]
def test_blit(self):
""" module canvas: Canvas.blit()
"""
cv1 = Canvas(10, 1)
cv2 = Canvas(10, 1)
cv2.put_str(0, 0, "teststring")
self.assertEqual(0, cv1.blit(0, 0, cv2))
self.assertEqual("teststring", cv1.export_to_memory("utf8").strip('\n'))
self.assertRaises(CanvasError, cv1.blit, 0, 0, "abc")
self.assertRaises(CanvasError, cv1.blit, "a", 0, cv2)
示例6: test_blit_mask
# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import export_to_memory [as 别名]
def test_blit_mask(self):
""" module canvas: Canvas.blit() (with mask)
"""
cv1 = Canvas(10, 1)
cv2 = Canvas(10, 1)
mask = Canvas(10, 1)
badm = Canvas()
cv2.put_str(0, 0, "teststring")
mask.put_str(0, 0, "####")
self.assertEqual(0, cv1.blit(0, 0, cv2, mask))
self.assertEqual("test ", cv1.export_to_memory("utf8").strip("\n"))
self.assertRaises(CanvasError, cv1.blit, 0, 0, cv2, badm)
示例7: to_ascii
# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import export_to_memory [as 别名]
#.........这里部分代码省略.........
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)
示例8: main
# 需要导入模块: from caca.canvas import Canvas [as 别名]
# 或者: from caca.canvas.Canvas import export_to_memory [as 别名]
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())
#print export to screen
sys.stdout.write("%s" % cv.export_to_memory(exformat))
if __name__ == "__main__":
#Import PIL package
try:
from PIL import Image
except ImportError, err:
sys.stderr.write("You need to install PIL module !\n")
sys.exit(2)
main()