def build_pin(path): my_ui_image = ui.Image.named(path) dx, dy = 28, 86 v = ui.View(frame=(0, 0, dx, dx), corner_radius=dx / 2) iv = ui.ImageView(frame=(0, 0, dx, dx)) iv.image = my_ui_image v.add_subview(iv) with ui.ImageContext(dx, dx) as ctx: v.draw_snapshot() # if circular cropped my_ui_image = ctx.get_image() # image with pin and its foot, coloured circle replaced by the photo) with ui.ImageContext(dx, dy) as ctx: foot = ui.Path.oval(dx / 2 - 2, dy / 2 - 1, 4, 2) ui.set_color((0, 0, 0, 1)) foot.fill() pin = ui.Path.rounded_rect(dx / 2 - 1, dx / 2, 2, dy / 2 - dx / 2, 1) ui.set_color((0.6, 0.6, 0.6, 1)) pin.fill() my_ui_image.draw(0, 0, dx, dx) my_ui_image = ctx.get_image() # circular image without foot not pin #d = 28 #v = ui.View(frame=(0,0,d,d),corner_radius=d/2) #iv = ui.ImageView(frame=(0,0,d,d)) #v.add_subview(iv) #iv.image = my_ui_image #with ui.ImageContext(d,d) as ctx: # v.draw_snapshot() # if circular cropped # my_ui_image = ctx.get_image() return my_ui_image
def draw_random(): with ui.ImageContext(canvasWidth, canvasHeight) as imageContext: rect = ui.Path.rect(border, border, canvasWidth - border*2, canvasHeight - border*2) ui.set_color("white") rect.fill() path = ui.Path() center = ui.Point(canvasWidth/2, canvasHeight/2) path.move_to(center.x, center.y) drawLoop = random.randint(3, 100) for i in range(drawLoop): lineType = random.choice(("line", "curve")) if(lineType == "line"): path.line_to(getRandomLength(), getRandomLength()) else: path.add_quad_curve(getRandomLength(), getRandomLength(), getRandomLength(), getRandomLength()) path.close() r = getRandomColor() g = getRandomColor() b = getRandomColor() fillColor = (r, g, b) ui.set_color(fillColor) path.fill() drawColor = (r-0.1, g-0.1, b-0.1) ui.set_color(drawColor) if drawLoop < 40: path.stroke() return imageContext.get_image()
def drawRandom(): with ui.ImageContext(canvasWidth, canvasHeight) as imageContext: rect = ui.Path.rect(border, border, canvasWidth - border * 2, canvasHeight - border * 2) ui.set_color("white") rect.fill() shapeType = random.choice(("rect", "oval")) for i in range(40000): x = random.randint(-10, canvasWidth - 10) y = random.randint(-10, canvasHeight - 10) w = random.randint(2, 20) h = random.randint(2, 20) if (shapeType == "rect"): shape = ui.Path.rect(x, y, w, h) else: shape = ui.Path.oval(x, y, w, h) r = getRandomColor() g = getRandomColor() b = getRandomColor() fillColor = (r, g, b) ui.set_color(fillColor) shape.fill() drawColor = (r - 0.1, g - 0.1, b - 0.1) ui.set_color(drawColor) shape.stroke() return imageContext.get_image()
def preview_init(self): path = ui.Path.rect(0, 0, Settings.width, Settings.height) with ui.ImageContext(Settings.width, Settings.height) as ctx: ui.set_color((0, 0, 0, 1)) path.fill() self.superview['preview'].image = ctx.get_image() return True
def create_image(): img = None x, y, w, h = v['view1'].frame with ui.ImageContext(w, h) as ctx: v['view1'].subviews[0].image.draw(0, 0, w, h) img = ctx.get_image() return img
def character_colorcheck(self): (startPos, endPos) = self.get_current_region() charSize = Settings.charSize clashCount = 0 pixelScale = self.width/(endPos[0]-startPos[0]+1)/Settings.pixelSize #self.height/Settings.height #s = self.width/self.row if self.row > self.column else self.height/self.column with ui.ImageContext(self.width, self.height) as ctx: ui.set_color('red') # Grid line per character for y in xrange(int(startPos[1]/charSize)*charSize, endPos[1]+1, charSize): for x in xrange(int(startPos[0]/charSize*charSize), endPos[0]+1,4): # Check this character for color clash charColors ={(self.background_color[0], self.background_color[1], self.background_color[2])} startIndex = xy_to_index(x,y) for pixelRow in range(0, 8): for pixelCol in range (0, 4): pixelIndex = startIndex + pixelRow*Settings.actualWidth + pixelCol charColors.add((self.pixels[pixelIndex].color[0], self.pixels[pixelIndex].color[1], self.pixels[pixelIndex].color[2])) if len(charColors) > 4: clashCount = clashCount + 1 pixel_path = ui.Path.rect((x-startPos[0])*pixelScale*2, (y-startPos[1])*pixelScale, pixelScale*charSize, pixelScale*charSize) pixel_path.line_width = 2 pixel_path.stroke() self.color_check.image = ctx.get_image() self.superview['debugtext'].text = str(clashCount) + " characters have color clashes." return clashCount
def draw_grid_image(self): (startPos, endPos) = self.get_current_region() charSize = Settings.charSize pixelScale = self.width/(endPos[0]-startPos[0]+1)/Settings.pixelSize #self.height/Settings.height #s = self.width/self.row if self.row > self.column else self.height/self.column pixelGrid = ui.Path.rect(0, 0, *self.frame[2:]) characterGrid = ui.Path.rect(0, 0, *self.frame[2:]) with ui.ImageContext(*self.frame[2:]) as ctx: # Fills entire grid with empty color ui.set_color((0, 0, 0, 0)) pixelGrid.fill() pixelGrid.line_width = 1 # Grid line per pixel for y in xrange(startPos[1], endPos[1]+1): for x in xrange(startPos[0], endPos[0]+1): pixelGrid.append_path(ui.Path.rect((x-startPos[0])*pixelScale*2, (y-startPos[1])*pixelScale, pixelScale*2, pixelScale)) drawColor = (0.5,0.5,0.5,0.5) if self.darkGrid == False else (0.25,0.25,0.25,0.5) ui.set_color(drawColor) pixelGrid.stroke() # Grid line per character for y in xrange(int(startPos[1]/charSize)*charSize, endPos[1]+1, charSize): for x in xrange(int(startPos[0]/charSize*charSize), endPos[0]+1,4): characterGrid.append_path(ui.Path.rect((x-startPos[0])*pixelScale*2, (y-startPos[1])*pixelScale, pixelScale*charSize, pixelScale*charSize)) drawColor = (1,1,1,0.5) if self.darkGrid == False else (0,0,0,0.5) ui.set_color(drawColor) characterGrid.stroke() return ctx.get_image()
def init_pixel_grid(self): s = self.width/self.row if self.row > self.column else self.height/self.column path = ui.Path.rect(0, 0, *self.frame[2:]) charpath = ui.Path.rect(0, 0, *self.frame[2:]) with ui.ImageContext(*self.frame[2:]) as ctx: # Fills entire grid with empty color ui.set_color((0, 0, 0, 0)) path.fill() # Grid line per pixel path.line_width = 1 for y in xrange(self.column): for x in xrange(self.row): # Fills image with pixels # Changing this changes the pixel aspect pixel = Pixel(x*s*2, y*s, s*2, s) pixel.index = len(self.pixels) pixel.position = (x,y) path.append_path(ui.Path.rect(*pixel.rect)) self.pixels.append(pixel) #Adds this pixel to the pixels list ui.set_color((0.5,0.5,0.5,0.5)) path.stroke() # Grid line per character for y in xrange(self.column/8): for x in xrange(self.row/4): #pixel = Pixel(x*s*8, y*s*8, s*8, s*8) #charpath.append_path(ui.Path.rect(*pixel.rect)) charpath.append_path(ui.Path.rect(x*s*8, y*s*8, s*8, s*8)) ui.set_color((1,1,1,0.5)) charpath.stroke() return ctx.get_image()
def draw_random(): with ui.ImageContext(canvasWidth, canvasHeight) as imageContext: rect = ui.Path.rect(border, border, canvasWidth - border * 2, canvasHeight - border * 2) ui.set_color("white") rect.fill() path = ui.Path() center = ui.Point(250, 250) path.move_to(center.x, center.y) drawLoop = random.randint(3, 100) for i in range(drawLoop): path.line_to(getRandomLength(), getRandomLength()) path.close() r = getRandomColor() g = getRandomColor() b = getRandomColor() fillColor = (r, g, b) ui.set_color(fillColor) path.fill() drawColor = (r - 0.1, g - 0.1, b - 0.1) ui.set_color(drawColor) path.stroke() return imageContext.get_image()
def save_button_action(self, sender): path = 'temp.jpg' with ui.ImageContext(self.width, self.height) as ctx: self.draw_snapshot() # draw entire view ui_image = ctx.get_image() pil = Image.open(io.BytesIO(ui_image.to_png())) x_min = 9999 y_min = 9999 x_max = 0 y_max = 0 for s in self.subviews: if 'Photo' in s.name: if s.x < x_min: x_min = int(s.x) if s.y < y_min: y_min = int(s.y) if (s.x + s.width - 1) > x_max: x_max = int(s.x + s.width - 1) if (s.y + s.height - 1) > y_max: y_max = int(s.y + s.height - 1) crop_pil = pil.crop((x_min * 2, y_min * 2, x_max * 2, y_max * 2)) crop_pil.save(path, quality=95) photos.create_image_asset(path) os.remove(path) self.saved = True
def __init__(self, width, height): self.frame = (0, 0, width, height) self.iwidth = 200.0 self.iheight = 150.0 framesize = 5 iw = self.iwidth - 2 * framesize ih = self.iheight - 2 * framesize ratio = ih / iw self.img = [] self.imgcount = photos.get_count() if self.imgcount < 100: console.hud_alert( 'Please wait while {} photos are loading...'.format( self.imgcount)) else: console.hud_alert('Please wait while 100 photos are loading...') self.imgcount = 100 for i in range(self.imgcount): img = ui.Image.from_data(photos.get_image(i, raw_data=True)) w, h = img.size rat = h / w x_ratio = 1.0 y_ratio = 1.0 x = framesize y = framesize if rat > ratio: #portrait x = ((iw - ih * rat) / 2) + framesize x_ratio = ih * rat / iw else: #landscape y = ((ih - iw * rat) / 2) + framesize y_ratio = iw * rat / ih with ui.ImageContext(self.iwidth, self.iheight) as ctx: img.draw(x, y, iw * x_ratio, ih * y_ratio) self.img.append(ctx.get_image())
def render_curve2(p, cs=72., c1=(0, 0, 0), c2=(1, 1, 1)): k = 1. cs *= 11 / 12. x, y = zip(*p) minx, miny = min(x), min(y) maxx, maxy = max(x), max(y) w, h = maxx - minx, maxy - miny y = [h - i for i in y] with ui.ImageContext(w + cs, h + cs) as ctx: a = 0 o = ui.Path() o.line_cap_style = 1 o.line_join_style = 1 o.move_to(x[0] - minx + cs / 2, y[0] + miny + cs / 2) for i in range(1, len(p)): o.line_to(x[i] - minx + cs / 2, y[i] + miny + cs / 2) #o.line_to(x[-1]-minx+cs/2,y[-1]+miny+cs/2) while a < cs / k: o.line_width = cs - a * k if a == 0: ui.set_color(c2) a += cs / 10 / k else: c = (a / cs * k / 4) ui.set_color(tuple(v1 + v2 for v1, v2 in zip(c1, (c, c, c)))) a += k o.stroke() img = ctx.get_image() return img, (minx - cs / 2, -min(y) - cs / 2)
def render_curve(p, cs=72., c=(0, 0, 0), c2=(1, 1, 1)): cs *= 35 / 36. x, y = zip(*p) minx, miny = min(x), min(y) maxx, maxy = max(x), max(y) w, h = maxx - minx, maxy - miny y = [h - i for i in y] sbw = cs / 24. with ui.ImageContext(w + cs, h + cs) as ctx: ui.set_color(c2) for a, b in zip(x, y): o = ui.Path.oval(a + 1.5 * sbw - minx, miny + b + 1.5 * sbw, cs - 3 * sbw, cs - 3 * sbw) o.line_width = 2 * sbw o.stroke() ui.set_color(c) for a, b in zip(x, y): o2 = ui.Path.oval(a + 2 * sbw - minx, miny + b + 2 * sbw, cs - 4 * sbw, cs - 4 * sbw) o2.line_width = 0 o2.fill() ui.set_color((0, 0, 1)) img = ctx.get_image().to_png() s = cStringIO.StringIO(img) img = Image.open(s) return img, minx - cs / 2, -min(y) - cs / 2, w + cs, h + cs
def emoji_to_image(emoji_char, w=32, h=32, font_name='Arial Rounded MT Bold', font_size=28, file_path=None): r = ui.Rect(0, 0, w, h) with ui.ImageContext(r.width, r.height) as ctx: # just draw the string ui.draw_string(emoji_char, rect=r, font=(font_name, font_size), color='black', alignment=ui.ALIGN_CENTER, line_break_mode=ui.LB_TRUNCATE_TAIL) img = ctx.get_image() # write a file if file_path if file_path: with open(file_path, 'wb') as file: file.write(img.to_png()) return img
def __init__(self): self.dropped_sprites = set() # Initialize the floor collision body: w = self.size[0] self.physics_body = sk.PhysicsBody.edge_loop_rect(0, -100, w, 100) # Draw and cache textures for all numbers: self.textures = {} for n in '0123456789:': w, h = s * 2 + 10, s * 2 with ui.ImageContext(w, h) as ctx: ui.draw_string(n, font=('AvenirNext-Regular', h * 0.94), rect=(0, 0, w, h), color='white', alignment=ui.ALIGN_CENTER) img = ctx.get_image() self.textures[n] = sk.Texture(img) margin = (self.size[0] - 7 * s) / 2 self.clock_nodes = [] self.time_str = time.strftime('%H:%M:%S') # Create a sprite node for every character in the time string: for i, c in enumerate(self.time_str): sprite = sk.SpriteNode(self.textures[c]) sprite.position = i * s + margin, self.size[1] - 200 self.add_child(sprite) self.clock_nodes.append(sprite)
def update(self): with ui.ImageContext(self.orig_size[0], self.orig_size[1]) as ctx: ui.set_color((.9, .9, .9, 1.0)) self.path = ui.Path.rect(0, 0, self.orig_size[0], self.orig_size[1]) self.path.fill() if self.shape_type == "oval": self.path = ui.Path.oval(self.position[0], self.position[1], self.size[0], self.size[1]) elif self.shape_type == "rectangle": self.path = ui.Path.rect(self.position[0], self.position[1], self.size[0], self.size[1]) elif self.shape_type == "rounded_rectangle": self.path = ui.Path.rounded_rect(self.position[0], self.position[1], self.size[0], self.size[1], self.corner_radius) ui.set_color(self.fill_color) self.path.fill() ui.set_color(self.stroke_color) self.path.line_width = self.stroke_width self.path.stroke() self.image = ctx.get_image() v['button4'].background_image = self.image v['imageview1'].image = self.image v['view1'].subviews[0].set_needs_display()
def capture_screen(sender): v = bible with ui.ImageContext(v.width, v.height) as c: v.draw_snapshot() c.get_image().show() sound.play_effect('8ve:8ve-tap-kissy') dialogs.hud_alert('captured', duration=.1)
def draw_face(self, diameter, border, font_size): with ui.ImageContext(diameter, diameter) as ctx: ui.set_color('white') circle = ui.Path.oval(border / 2, border / 2, diameter - border, diameter - border) circle.line_width = border - 1 circle.fill() ui.set_color('silver') with ui.GState(): ui.set_shadow((0, 0, 0, 0.35), 0, 1, 5.0) circle.stroke() angle = (-pi / 2) + (pi * 2) / 12.0 for i in range(1, 13): number = str(i) x = diameter / 2 + cos(angle) * (diameter / 2 - font_size * 1.2) y = diameter / 2 + sin(angle) * (diameter / 2 - font_size * 1.2) font = ('HelveticaNeue-UltraLight', font_size) w, h = ui.measure_string(number, font=font) rect = (x - 50, y - h / 2, 100, h) ui.draw_string(number, rect=rect, font=font, alignment=ui.ALIGN_CENTER) angle += (pi * 2.0) / 12.0 return ctx.get_image()
def __init__(self, imgUrls): self.imgUrls = imgUrls # create a placeholder image with ui.ImageContext(100,100) as ctx: oval = ui.Path.oval(0,0,100,100) ui.set_color('red') oval.fill() self.placeholder_img = ctx.get_image()
def clear_image(self, sender): with ui.ImageContext(self.width, self.height) as setup: self.background = ui.Path() ui.set_color(self.bg_color) ui.fill_rect(0, 0, self.width, self.height) bg = self.background.rect(0, 0, self.width, self.height) self.background = setup self.canvas.image = setup.get_image()
def update_paper(self): previousImage = self.canvas.image with ui.ImageContext(self.width, self.height) as newImageCtx: if previousImage: previousImage.draw() ui.set_color(self.color) self.path.stroke() self.canvas.image = newImageCtx.get_image()
def draw_hand(self, width, length, color): with ui.ImageContext(width, length) as ctx: # Leave a few pixels transparent round_rect = ui.Path.rounded_rect(2, 2, width - 4, length - 2, (width - 4) * 0.5) ui.set_color(color) round_rect.fill() return ctx.get_image()
def draw_line(cwidth, cheight): with ui.ImageContext(cwidth, cheight) as ctx: ui.set_color('black') p = ui.Path() p.line_width = cheight p.move_to(0, 0) p.line_to(cwidth, 0) p.stroke() return ctx.get_image()
def get_img(self, sender): w_im = self.width h_im = self.height with ui.ImageContext(w_im, h_im) as ctx: self.main.draw_snapshot() im = ctx.get_image() png = im.to_png() with Image.open(io.BytesIO(png)) as pil: pil.show()
def screenshot_action(self, sender): with ui.ImageContext(self.width, self.height) as c: self.draw_snapshot() #c.get_image().show() filename = actPage.name + '_scrn_{}.png'.format( time.strftime("%d-%H:%M:%S", time.localtime())) logger.info('screenshot ' + filename) with open(filename, 'wb') as out_file: out_file.write(c.get_image().to_png())
def change_tile(self, i, j, t): themap[i][j] = t unit = self.unit with ui.ImageContext(self.unit * mapsize, self.unit * mapsize) as ctx: self.mapimg.draw(0, 0) tiles[t].draw(i * 2 * unit, j * 2 * unit, 2 * unit, 2 * unit) self.mapimg = ctx.get_image() self.ground.texture = Texture(self.mapimg) print('{},{},{}'.format(i, j, t))
def path_action(self, sender): path = sender.path old_img = self.image_view.image width, height = self.image_view.width, self.image_view.height with ui.ImageContext(width, height) as ctx: if old_img: old_img.draw() path.stroke() self.image_view.image = ctx.get_image()
def get_diamond(color): with ui.ImageContext(100, 100) as ctx: ui.set_color(color) with ui.GState(): ui.concat_ctm(ui.Transform.rotation(45/180*math.pi)) a = 0.1 ui.fill_rect((0.5+a)/math.sqrt(2)*100, (a-0.5)/math.sqrt(2)*100, math.sqrt(2)*(0.5-a)*100, math.sqrt(2)*(0.5-a)*100) im = ctx.get_image() return im
def make_frame(t): with ui.ImageContext(W, H, 1) as ctx: for i in range(ncircles): angle = 2*np.pi*(1.0*i/ncircles+t/duration) center = W*( 0.5 + polar2cart(0.1,angle)[0]), W*( 0.5 + polar2cart(0.1,angle)[1]) r = W*(1.0-1.0*i/ncircles) ui.set_color((i%2, i%2, i%2)) ui.Path.oval(center[0]-r, center[1]-r, r*2, r*2).fill() return ui2pil(ctx.get_image())
def uiImgResize(imgIn, size): if imgIn == None: return None w, h = size imgOut = None with ui.ImageContext(w, h) as ctx: imgIn.draw(0, 0, w, h) imgOut = ctx.get_image() return imgOut