本文整理汇总了Python中drawille.Canvas类的典型用法代码示例。如果您正苦于以下问题:Python Canvas类的具体用法?Python Canvas怎么用?Python Canvas使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Canvas类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_canvas
def make_canvas(matrix):
canvas = Canvas()
for r, row in enumerate(matrix):
for c, val in enumerate(row):
if val > 127:
canvas.set(c, r)
return canvas
示例2: image2term
def image2term(image, threshold=128, ratio=None):
if image.startswith('http://') or image.startswith('https://'):
i = Image.open(StringIO(urllib2.urlopen(image).read())).convert('L')
else:
i = Image.open(open(image)).convert('L')
w, h = i.size
if ratio:
w = int(w * ratio)
h = int(h * ratio)
i = i.resize((w, h), Image.ANTIALIAS)
else:
tw, th = getTerminalSize()
tw *= 2
th *= 2
if tw < w:
ratio = tw / float(w)
w = tw
h = int(h * ratio)
i = i.resize((w, h), Image.ANTIALIAS)
can = Canvas()
x = y = 0
for pix in i.tobytes():
if ord(pix) < threshold:
can.set(x, y)
x += 1
if x >= w:
y += 1
x = 0
return can.frame()
示例3: render
def render(self, width, height, values):
"""
:type width: int
:type height: int
:type values: list[int or float]
:rtype: list[str or unicode]
"""
from drawille import Canvas
vertical_chart_resolution = height * self.char_resolution_vertical
horizontal_chart_resolution = width * self.char_resolution_horizontal
canvas = Canvas()
x = 0
for value in values:
for y in range(1, int(round(value, 0)) + 1):
canvas.set(x, vertical_chart_resolution - y)
x += 1
rows = canvas.rows(min_x=0, min_y=0, max_x=horizontal_chart_resolution, max_y=vertical_chart_resolution)
if not rows:
rows = [u'' for _ in range(height)]
for i, row in enumerate(rows):
rows[i] = u'{0}'.format(row.ljust(width))
return rows
示例4: update
def update(im, delay=0.1):
im = im.resize((CANVAS_W, CANVAS_H))
canvas = Canvas()
any(canvas.set(i % CANVAS_W, i // CANVAS_W)
for i, px in enumerate(im.tobytes()) if px)
print(canvas.frame(0, 0, CANVAS_W, CANVAS_H), end='')
time.sleep(delay)
示例5: main
def main(args, opts):
curses.use_default_colors()
curses.curs_set(0)
win = curses.initscr()
source = Image.open(opts.filename)
c = Canvas()
try:
while True:
(wh, ww) = win.getmaxyx()
try:
source.seek(source.tell() + 1)
except:
if opts.onetime:
break
source.seek(0)
img = (source
.resize(((ww - 1) * 2, wh * 4))
.convert("1")
)
w = img.width
(x, y) = (0, 0)
for v in img.getdata():
if opts.reverse:
if not v:
c.set(x, y)
else:
if v:
c.set(x, y)
x += 1
if w <= x:
x = 0
y += 1
for r in range(wh):
line = c.rows(min_y=(r*4), max_y=((r+1)*4))[0]
win.addnstr(r, 0, pad(line, ww), ww)
win.refresh()
c.clear()
time.sleep(opts.interval)
except KeyboardInterrupt:
pass
示例6: setup
def setup():
# set the size of the board to the size of the terminal at ther start
width, height = os.popen('stty size', 'r').read().split()
height, width = 50, 50
board = Canvas()
frame = (0, 0, width, height)
assert(frame[YSEC] == height)
board.frame(*frame)
draw_bounds(board, frame)
# get some paddles
lpaddle = Paddle('left', board, frame)
rpaddle = Paddle('right', board, frame)
ball = Ball(board, frame)
# get some output!
stdscr = curses.initscr()
stdscr.refresh()
return (lpaddle, rpaddle, ball), board, frame, stdscr
示例7: test_get
def test_get(self):
c = Canvas()
self.assertEqual(c.get(0, 0), False)
c.set(0, 0)
self.assertEqual(c.get(0, 0), True)
self.assertEqual(c.get(0, 1), False)
self.assertEqual(c.get(1, 0), False)
self.assertEqual(c.get(1, 1), False)
示例8: __main__
def __main__(stdscr):
i = 0
c = Canvas()
height = 40
while True:
for x,y in line(0, height, 180, int(math.sin(math.radians(i)) * height + height)):
c.set(x,y)
for x in range(0, 360, 2):
coords = (x/2, height + int(round(math.sin(math.radians(x+i)) * height)))
c.set(*coords)
f = c.frame()
stdscr.addstr(0, 0, '{0}\n'.format(f))
stdscr.refresh()
i += 2
sleep(1.0/24)
c.clear()
示例9: image2term
def image2term(image, threshold=128, ratio=None, invert=False):
if image.startswith('http://') or image.startswith('https://'):
i = Image.open(StringIO(urllib2.urlopen(image).read())).convert('L')
else:
i = Image.open(open(image)).convert('L')
w, h = i.size
if ratio:
w = int(w * ratio)
h = int(h * ratio)
i = i.resize((w, h), Image.ANTIALIAS)
else:
tw = getTerminalSize()[0]
tw *= 2
if tw < w:
ratio = tw / float(w)
w = tw
h = int(h * ratio)
i = i.resize((w, h), Image.ANTIALIAS)
can = Canvas()
x = y = 0
try:
i_converted = i.tobytes()
except AttributeError:
i_converted = i.tostring()
for pix in i_converted:
if invert:
if ord(pix) > threshold:
can.set(x, y)
else:
if ord(pix) < threshold:
can.set(x, y)
x += 1
if x >= w:
y += 1
x = 0
return can.frame(0, 0)
示例10: test_max_min_limits
def test_max_min_limits(self):
c = Canvas()
c.set(0, 0)
self.assertEqual(c.frame(min_x=2), '')
self.assertEqual(c.frame(max_x=0), '')
示例11: test_frame
def test_frame(self):
c = Canvas()
self.assertEqual(c.frame(), '')
c.set(0, 0)
self.assertEqual(c.frame(), 'РаЂ')
示例12: test_set_text
def test_set_text(self):
c = Canvas()
c.set_text(0, 0, "asdf")
self.assertEqual(c.frame(), "asdf")
示例13: test_toggle
def test_toggle(self):
c = Canvas()
c.toggle(0, 0)
self.assertEqual(c.chars, {0: {0: 1}})
c.toggle(0, 0)
self.assertEqual(c.chars, dict())
示例14: test_clear
def test_clear(self):
c = Canvas()
c.set(1, 1)
c.clear()
self.assertEqual(c.chars, dict())
示例15: __init__
class GameOfLife:
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.can = Canvas()
self.state = [[bool(random.getrandbits(1)) for y in range(rows)] for x in range(cols)]
def tick(self):
next_gen = [[False for y in range(self.rows)] for x in range(self.cols)]
for y in range(self.rows):
for x in range(self.cols):
nburs = self._ncount(x, y)
if self.state[x][y]:
# Alive
if nburs > 1 and nburs < 4:
self.can.set(x,y)
next_gen[x][y] = True
else:
next_gen[x][y] = False
else:
# Dead
if nburs == 3:
self.can.set(x,y)
next_gen[x][y] = True
else:
next_gen[x][y] = False
self.state = next_gen
def draw(self):
f = self.can.frame(0,0,self.cols,self.rows)
stdscr.addstr(0, 0, '{0}\n'.format(f))
self.can.clear()
def put(self, matrix, x, y):
for mx in range(len(matrix)):
for my in range(len(matrix[0])):
self.state[min(x+my,self.cols-1)][min(y+mx,self.rows-1)] = matrix[mx][my]
def _ncount(self, x, y):
nburs = 0
# Left
if x > 0:
if self.state[x-1][y]:
nburs += 1
# Top
if y > 0:
if self.state[x-1][y-1]:
nburs += 1
# Bottom
if y < self.rows-1:
if self.state[x-1][y+1]:
nburs += 1
# Right
if x < self.cols-1:
if self.state[x+1][y]:
nburs += 1
# Top
if y > 0:
if self.state[x+1][y-1]:
nburs += 1
# Bottom
if y < self.rows-1:
if self.state[x+1][y+1]:
nburs += 1
# Top
if y > 0:
if self.state[x][y-1]:
nburs += 1
# Bottom
if y < self.rows-1:
if self.state[x][y+1]:
nburs += 1
return nburs