本文整理汇总了Python中drawille.Canvas.set方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.set方法的具体用法?Python Canvas.set怎么用?Python Canvas.set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类drawille.Canvas
的用法示例。
在下文中一共展示了Canvas.set方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_canvas
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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: test_get
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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)
示例5: main
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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: __main__
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
def __main__(stdscr, projection=False):
angleX, angleY, angleZ = 0, 0, 0
c = Canvas()
while 1:
# Will hold transformed vertices.
t = []
for v in vertices:
# Rotate the point around X axis, then around Y axis, and finally around Z axis.
p = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ)
if projection:
# Transform the point from 3D to 2D
p = p.project(50, 50, 50, 50)
#Put the point in the list of transformed vertices
t.append(p)
for f in faces:
for x,y in line(t[f[0]].x, t[f[0]].y, t[f[1]].x, t[f[1]].y):
c.set(x,y)
for x,y in line(t[f[1]].x, t[f[1]].y, t[f[2]].x, t[f[2]].y):
c.set(x,y)
for x,y in line(t[f[2]].x, t[f[2]].y, t[f[3]].x, t[f[3]].y):
c.set(x,y)
for x,y in line(t[f[3]].x, t[f[3]].y, t[f[0]].x, t[f[0]].y):
c.set(x,y)
f = c.frame(-40, -40, 80, 80)
stdscr.addstr(0, 0, '{0}\n'.format(f))
stdscr.refresh()
angleX += 2
angleY += 3
angleZ += 5
sleep(1.0/20)
c.clear()
示例7: update
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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)
示例8: main
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
def main(stdscr):
global frame_no, speed, fps, position, delta, score
c = Canvas()
bar_width = 16
bars = [Bar(bar_width)]
stdscr.refresh()
while True:
frame_no += 1
for bar in bars:
if check_collision(position, bar):
return
while not keys.empty():
if keys.get() == 113:
return
speed = 32.0
c.set(0,0)
c.set(width, height)
if frame_no % 50 == 0:
bars.append(Bar(bar_width))
for x,y in bird:
c.set(x,y+position)
for bar_index, bar in enumerate(bars):
if bar.x < 1:
bars.pop(bar_index)
score += 1
else:
bars[bar_index].x -= 1
for x,y in bar.draw():
c.set(x,y)
f = c.frame()+'\n'
stdscr.addstr(0, 0, f)
stdscr.addstr(height/4+1, 0, 'score: {0}'.format(score))
stdscr.refresh()
c.clear()
speed -= 2
position -= speed/10
if position < 0:
position = 0
speed = 0.0
elif position > height-13:
position = height-13
speed = 0.0
sleep(1.0/fps)
示例9: image2term
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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: __main__
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
def __main__(projection=False):
angleX, angleY, angleZ = 0, 0, 0
c = Canvas()
while 1:
# Will hold transformed vertices.
t = []
for v in vertices:
# Rotate the point around X axis, then around Y axis, and finally around Z axis.
p = v.rotateX(angleX).rotateY(angleY).rotateZ(angleZ)
if projection:
# Transform the point from 3D to 2D
p = p.project(50, 50, 50, 50)
# Put the point in the list of transformed vertices
t.append(p)
for f in faces:
for x, y in line(t[f[0]].x, t[f[0]].y, t[f[1]].x, t[f[1]].y):
c.set(x, y)
for x, y in line(t[f[1]].x, t[f[1]].y, t[f[2]].x, t[f[2]].y):
c.set(x, y)
for x, y in line(t[f[2]].x, t[f[2]].y, t[f[3]].x, t[f[3]].y):
c.set(x, y)
for x, y in line(t[f[3]].x, t[f[3]].y, t[f[0]].x, t[f[0]].y):
c.set(x, y)
f = c.frame(-20, -20, 20, 20)
s = 'broadcast "\\n\\n\\n'
for l in format(f).splitlines():
s += l.replace(" ", " ").rstrip("\n") + "\\n"
s = s[:-2]
s += '"'
print(s)
sys.stdout.flush()
angleX += 2
angleY += 3
angleZ += 5
sleep(1.0 / 10)
c.clear()
示例11: Canvas
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
from __future__ import print_function
from drawille import Canvas
import math
s = Canvas()
for x in range(1800):
s.set((x/10, math.sin(math.radians(x)) * 10))
print(s.frame())
s.clear()
for x in range(0, 1800, 10):
s.set((x/10, 10 + math.sin(math.radians(x)) * 10))
s.set((x/10, 10 + math.cos(math.radians(x)) * 10))
print(s.frame())
s.clear()
for x in range(0, 3600, 20):
s.set((x/20, 4 + math.sin(math.radians(x)) * 4))
print(s.frame())
s.clear()
for x in range(0, 360, 4):
s.set((x/4, 30 + math.sin(math.radians(x)) * 30))
示例12: __init__
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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
示例13: int
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
tb = tb / ( ppd * ppd )
sum = (tr + tg + tb) / 3
#python doesn't have bools, it's like see where 0 is false and anything else is true
if (dither or trippy) :
#because you can't divide by zero
if (sum == 0) :
canvo.unset(x,y)
else:
#the RGB scale is exponential , so the ratio of light from 255 to 80 would be 255^2/80^2
# however since the image is 3 dimensional, then the dither turns on in 1/mod * 1/mod
# so mod should be 255/80 instead
mod = int(255 / sum)
# the ratio here is (2x-1)/x^2 where x is 255/sum
if (trippy) :
if (((x % mod) == 0) or ((y % mod) == 0)) :
canvo.set(x,y)
else:
canvo.unset(x,y)
#ratio is 1/x^2
elif (dither) :
if (((x % mod) == 0) and ((y % mod) == 0)) :
canvo.set(x,y)
else:
canvo.unset(x,y)
else:
if (threshup >= sum >= threshdown ) :
# turns the dot on at x,y
canvo.set(x,y)
else :
# it seems if an entire character is blank, draille does not print it regardless of this
canvo.unset(x,y)
示例14: test_max_min_limits
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
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), '')
示例15: test_unset_empty
# 需要导入模块: from drawille import Canvas [as 别名]
# 或者: from drawille.Canvas import set [as 别名]
def test_unset_empty(self):
c = Canvas()
c.set(1, 1)
c.unset(1, 1)
self.assertEqual(c.pixels, dict())