def getProgressItems(self, curStepIndex, steps): total = len(steps) marginTop = 4 dia = 9 space = 12 image = Image.new("RGB", (self.width, dia + int(marginTop * 1.5))) draw = Draw(image) w = ((dia + space) * total) - space marginLeft = (self.width - w) / 2 for s in range(total): left = marginLeft + ((dia + space) * s) draw.ellipse((left, marginTop, left + dia, marginTop + dia), Pen(self.colors[steps[s]]), Brush(self.colors[steps[s]])) if curStepIndex < s: draw.ellipse( (left + (dia * 0.25), marginTop + (dia * 0.25), left + (dia * 0.75), marginTop + (dia * 0.75)), Pen("#000"), Brush("#000")) draw.flush() return image
def __init__(self, x, y, radius, width, color): self.x = x self.y = y self.radius = radius self.extent = CountDownRing.MAX_EXTENT self.pen = Pen(color, width=width, opacity=190) self.shadow_pen = Pen(color, width=width, opacity=60)
def addCanvasLine(self, p1, p2, color=(0, 0, 0), color2=None, **kwargs): if color2 and color2 != color: mp = (p1[0] + p2[0]) / 2., (p1[1] + p2[1]) / 2. color = convertColor(color) self._doLine(p1, mp, Pen(color, kwargs.get('linewidth', 1)), **kwargs) color2 = convertColor(color2) self._doLine(mp, p2, Pen(color2, kwargs.get('linewidth', 1)), **kwargs) else: color = convertColor(color) self._doLine(p1, p2, Pen(color, kwargs.get('linewidth', 1)), **kwargs)
async def loading(self): image = Image.new("RGB", (self.width, self.height)) draw = Draw(image) colors = list(self.colors.values()) draw.ellipse((50, 59, 60, 69), Pen(colors[0]), Brush(colors[0])) draw.ellipse((75, 59, 85, 69), Pen(colors[1]), Brush(colors[1])) draw.ellipse((100, 59, 110, 69), Pen(colors[2]), Brush(colors[2])) draw.flush() self.display(image)
def stroke(self, style): if not style: self.stroke_width = 0 self.stroke_color = None self.stroke_alignment = STROKE_OUTER return self try: width, color, alignment = style except ValueError: width, color = style alignment = STROKE_OUTER if alignment in [STROKE_INNER, STROKE_CENTER, STROKE_OUTER]: self.stroke_alignment = alignment else: raise ValueError( "Invalid stroke alignment, see KLConstants for accepted values" ) color = list(color) if len(color) == 3: color += [255] self.stroke_color = color self.stroke_width = width self.__stroke = Pen(tuple(color[:3]), width, color[3]) if self.surface: # don't call this when initializing the Drawbject for the first time self._init_surface() return self
def __init__(self, size, thickness, fill, rotation=0, auto_draw=True): self.size = size self.thickness = thickness super(SquareAsterisk, self).__init__(size, size, None, fill, rotation) self._Drawbject__stroke = Pen((0, 0, 0), 0, 0) if auto_draw: self.draw()
def draw_lozenge(self, x, y, width, height, colorindex): lozenge_pen = Pen(self.pylink_colors[colorindex], 3, 255) if width > height: gap = width - height middle = x + width / 2.0 arc_left = (x, y, x + height, y + height) arc_right = (x + gap, y, x + width, y + height) line_top = (floor(middle - gap / 2.0), y, ceil(middle + gap / 2.0), y) line_bottom = (floor(middle - gap / 2.0), y + height, ceil(middle + gap / 2.0), y + height) self.drawer.arc(arc_left, 90, 270, lozenge_pen) self.drawer.arc(arc_right, -90, 90, lozenge_pen) self.drawer.line(line_top, lozenge_pen) self.drawer.line(line_bottom, lozenge_pen) elif height > width: gap = height - width middle = y + height / 2.0 arc_top = (x, y, x + width, y + width) arc_bottom = (x, y + gap, x + width, y + height) line_left = (x, floor(middle - gap / 2.0), x, ceil(middle + gap / 2.0)) line_right = (x + width, floor(middle - gap / 2.0), x + width, ceil(middle + gap / 2.0)) self.drawer.arc(arc_top, 0, 180, lozenge_pen) self.drawer.arc(arc_bottom, 180, 360, lozenge_pen) self.drawer.line(line_left, lozenge_pen) self.drawer.line(line_right, lozenge_pen) else: self.drawer.ellipse((x, y, x + width, y + height), lozenge_pen)
def draw(self): rotation = self.rotation center = self.surface_width / 2.0 r = self.radius + 1 for i in range(0, len(self.colors)): brush = Brush(rgb_to_rgba(self.colors[i])) vertices = [center, center] for i in range(0, 4): r_shift = -0.25 if i < 2 else 1.25 r_shift -= rotation func = cos if i % 2 else sin vertices.append(r + r * func(radians(r_shift + 180))) self.surface.polygon(vertices, brush) rotation += 360.0 / len(self.colors) self.surface.flush() # Create annulus mask and apply it to colour disc mask = Image.new('L', (self.surface_width, self.surface_height), 0) d = Draw(mask) xy_1 = center - (self.radius - self.thickness / 2.0) xy_2 = center + (self.radius - self.thickness / 2.0) path_pen = Pen(255, self.thickness) d.ellipse([xy_1, xy_1, xy_2, xy_2], path_pen, self.transparent_brush) d.flush() self.canvas.putalpha(mask) return self.canvas
def test_graphics3(): """See issue #22.""" from aggdraw import Draw, Pen from PIL import Image main = Image.new('RGB', (480, 1024), 'white') d = Draw(main) p = Pen((90,) * 3, 0.5)
def draw(self): self.dib.rectangle((0, 0, self.winfo_width(), self.winfo_height()), Pen(self.bg_color), Brush(self.bg_color)) self.galaxy.draw(self.dib) self.death_circle.draw(self.dib) self.time_ring.draw(self.dib) self.dib.expose(hwnd=self.winfo_id()) self.death_circle.update()
def test_pen(): from aggdraw import Pen Pen("black") Pen("black", 1) Pen("black", 1.5) Pen("black", 1, opacity=128) Pen(0) Pen((0,0,0)) Pen("rgb(0,0,0)") Pen("gold")
def addCanvasDashedWedge(self, p1, p2, p3, dash=(2, 2), color=(0, 0, 0), color2=None, **kwargs): pen = Pen(color, kwargs.get('linewidth', 1)) dash = (3, 3) pts1 = self._getLinePoints(p1, p2, dash) pts2 = self._getLinePoints(p1, p3, dash) if len(pts2) < len(pts1): pts2, pts1 = pts1, pts2 for i in range(len(pts1)): self.draw.line((pts1[i][0], pts1[i][1], pts2[i][0], pts2[i][1]), pen)
def test_graphics2(): """See issue #14.""" from aggdraw import Draw, Symbol, Pen from PIL import Image import numpy as np symbol = Symbol("M400 200 L400 400") pen = Pen("red") image = Image.fromarray(np.zeros((800, 600, 3)), mode="RGB") canvas = Draw(image) canvas.symbol((0, 0), symbol, pen) canvas.flush() assert np.asarray(image).sum() == 50800
def draw(self): surf_c = self.surface_width / 2.0 # center of the drawing surface if self.stroke: if self.stroke_alignment == STROKE_CENTER: stroke_pen = Pen(tuple(self.stroke_color), self.stroke_width / 2.0) # draw outer stroke ring xy_1 = surf_c - (self.radius + self.stroke_width / 4.0) xy_2 = surf_c + (self.radius + self.stroke_width / 4.0) self.surface.ellipse([xy_1, xy_1, xy_2, xy_2], stroke_pen, self.transparent_brush) # draw inner stroke ring xy_1 = surf_c - (self.radius - (self.thickness + self.stroke_width / 4.0)) xy_2 = surf_c + (self.radius - (self.thickness + self.stroke_width / 4.0)) self.surface.ellipse([xy_1, xy_1, xy_2, xy_2], stroke_pen, self.transparent_brush) else: if self.stroke_alignment == STROKE_OUTER: xy_1 = surf_c - (self.radius + self.stroke_width / 2.0) xy_2 = surf_c + (self.radius + self.stroke_width / 2.0) elif self.stroke_alignment == STROKE_INNER: xy_1 = surf_c - ( self.radius - (self.thickness + self.stroke_width / 2.0)) xy_2 = surf_c + ( self.radius - (self.thickness + self.stroke_width / 2.0)) stroke_pen = Pen(tuple(self.stroke_color), self.stroke_width) self.surface.ellipse([xy_1, xy_1, xy_2, xy_2], stroke_pen, self.transparent_brush) if self.fill: xy_1 = surf_c - (self.radius - self.thickness / 2.0) xy_2 = surf_c + (self.radius - self.thickness / 2.0) ring_pen = Pen(tuple(self.fill_color), self.thickness) self.surface.ellipse([xy_1, xy_1, xy_2, xy_2], ring_pen, self.transparent_brush) self.surface.flush() return self.canvas
def __init__(self, size, thickness, stroke=None, fill=None, rotation=0, auto_draw=True): self.thickness = thickness super(FixationCross, self).__init__(size, size, stroke, fill, rotation) if stroke == None: self._Drawbject__stroke = Pen((0, 0, 0), 0, 0) if auto_draw: self.draw()
def circleProgressLeft(self, percent, color): dia = 44 image = Image.new("RGB", (dia + 6, dia + 6)) draw = Draw(image) pen = Pen(self.colors[color], 6) radian = percent * 360 draw.arc((3, 3, dia + 3, dia + 3), 450 - radian, 90, pen) draw.flush() return image
def addCanvasPolygon(self, ps, color=(0, 0, 0), fill=True, stroke=False, **kwargs): if not fill and not stroke: return dps = [] for p in ps: dps.extend(p) color = convertColor(color) brush = None pen = None if fill: brush = Brush(color) if stroke: pen = Pen(color) self.draw.polygon(dps, pen, brush)
def __init__(self, x, y): self.x = x self.y = y self.get_radius = SinFunction(phase=random.uniform(0, math.pi), amplitude=random.uniform(0.5, 1.5), del_theta=random.uniform(0.03, 0.07), mean_value=random.uniform(1.2, 3.2)) self.status = Star.IS_HEALTHY self.radius = self.get_radius() self.color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) self.brush = Brush(self.color) self.pen = Pen(self.color, 0) self.glow_brush = Brush(self.color, opacity=random.randint(20, 40)) self.glow_offset = self.get_radius.max_value() * 1.7 self.explode_radius = self.get_radius.max_value() * 8
def __init__(self, tail_w, tail_h, head_w, head_h, rotation=0, stroke=None, fill=None): self.tail_w = tail_w self.tail_h = tail_h self.head_h = head_h self.head_w = head_w arrow_w = self.head_w + self.tail_w arrow_h = self.head_h if head_h > tail_h else tail_h super(Arrow, self).__init__(arrow_w, arrow_h, stroke, fill, rotation) # Set stroke to empty pen to avoid aggdraw anti-aliasing weirdness if stroke == None: self._Drawbject__stroke = Pen((0, 0, 0), 0, 0)
def _stroke_annotations(a: Annotation, label: str, color=0xFF, width=5, out=None): if out is None: out = Image.new(mode='L', size=a.image.size) pen = Pen(color, width) d = Draw(out) d.setantialias(False) # Should not antialias masks for o in a.iter_objects(label): xy = a.points(o) d.polygon(xy.flatten(), pen) return d.flush()
def make_marker(radius, fill_color, stroke_color, stroke_width, opacity=1.0): """ Creates a map marker and returns a PIL image. radius In pixels fill_color Any PIL-acceptable color representation, but standard hex string is best stroke_color See fill_color stroke_width In pixels opacity Float between 0.0 and 1.0 """ # Double all dimensions for drawing. We'll resize back to the original # radius for final output -- it makes for a higher-quality image, especially # around the edges radius, stroke_width = radius * 2, stroke_width * 2 diameter = radius * 2 im = Image.new('RGBA', (diameter, diameter)) draw = Draw(im) # Move in from edges half the stroke width, so that the stroke is not # clipped. half_stroke_w = (stroke_width / 2 * 1.0) + 1 min_x, min_y = half_stroke_w, half_stroke_w max_x = diameter - half_stroke_w max_y = max_x bbox = (min_x, min_y, max_x, max_y) # Translate opacity into aggdraw's reference (0-255) opacity = int(opacity * 255) draw.ellipse(bbox, Pen(stroke_color, stroke_width, opacity), Brush(fill_color, opacity)) draw.flush() # The key here is to resize using the ANTIALIAS filter, which is very # high-quality im = im.resize((diameter / 2, diameter / 2), Image.ANTIALIAS) return im
def cursor(color=None): dc = Draw("RGBA", [32, 32], (0, 0, 0, 0)) if color is not None: cursor_color = color[0:3] else: cursor_color = [] for c in P.default_fill_color: cursor_color.append(abs(c - 255)) cursor_color = cursor_color[0:3] # coordinate tuples are easier to read/modify but aggdraw needs a stupid x,y,x,y,x,y list cursor_coords = [(6, 0), (6, 27), (12, 21), (18, 32), (20, 30), (15, 20), (23, 20), (6, 0)] cursor_xy_list = [] for point in cursor_coords: cursor_xy_list.append(point[0]) cursor_xy_list.append(point[1]) brush = Brush(tuple(cursor_color), 255) pen = Pen((255, 255, 255), 1, 255) dc.polygon(cursor_xy_list, pen, brush) cursor_surface = aggdraw_to_array(dc) return cursor_surface
async def circleProgress(self, percent, text=""): image = Image.new("RGB", (self.width, self.height)) draw = Draw(image) pen = Pen("white", 7) radian = percent * 3.6 draw.arc((50, 34, 110, 94), 450 - radian, 90, pen) draw.flush() imDraw = ImageDraw.Draw(image) if len(text) > 0: text = text.capitalize() w, h = imDraw.textsize(text, font=self.fonts['prompt']) imDraw.text(((self.width - w) / 2, (self.height - h) / 2), text, font=self.fonts['prompt'], align="center", fill="#fff") image = invert(image) self.display(image)
def test_graphics(): from aggdraw import Draw, Pen, Brush draw = Draw("RGB", (500, 500)) pen = Pen("black") brush = Brush("black") draw.line((50, 50, 100, 100), pen) draw.rectangle((50, 150, 100, 200), pen) draw.rectangle((50, 220, 100, 270), brush) draw.rectangle((50, 290, 100, 340), brush, pen) draw.rectangle((50, 360, 100, 410), pen, brush) draw.ellipse((120, 150, 170, 200), pen) draw.ellipse((120, 220, 170, 270), brush) draw.ellipse((120, 290, 170, 340), brush, pen) draw.ellipse((120, 360, 170, 410), pen, brush) draw.polygon((190+25, 150, 190, 200, 190+50, 200), pen) draw.polygon((190+25, 220, 190, 270, 190+50, 270), brush) draw.polygon((190+25, 290, 190, 340, 190+50, 340), brush, pen) draw.polygon((190+25, 360, 190, 410, 190+50, 410), pen, brush)
def draw_line(self, x1, y1, x2, y2, colorindex): line_pen = Pen(self.pylink_colors[colorindex], 3, 255) self.drawer.line((x1, y1, x2, y2), line_pen)
def __init__(self): self.__is_growing = False self.__radius = 0 self.__center = (0, 0) self.__pen = Pen("red", 0) self.__brush = Brush("red", 50)
def draw(self, dib: Dib): dib.eclipse(self.bounds, Pen(self.color, self.width))
def draw(self): ''' Render the image. Assumes that set_data has already been called. Returns a Python Image Library (PIL) Image object. ''' img = Image.new('RGB', (self.width, self.height), self.bgcol) canvas = Draw(img) # create the projection. Here we use an equidistant cylindrical projection, # but others may work with tweaking of the parameters. proj = Proj(proj=self.proj, a=self.width/(2*pi), # set the radius of the earth such that our # projections work x_0=self.width/2, # center horizontally on the image y_0=self.height/2) # center verticallly on the image # two branches below will use the same sequence of commands to # draw a great-circle on the map, so the common elements are wrapped # up into a locally defined function. Given a matrix of points and # a pen, draw the path through the points. def draw_(pts, pen): lons, lats = pts.T x, y = proj(lons, lats) y = self.height - y path = reduce(operator.add, zip(x, y)) canvas.line(path, pen) # loop over every coordinate pair for i, (lon1, lat1, lon2, lat2) in enumerate(self.data[self.order]): # calculate the fraction of the paths already drawn, and use # it to create a pen of the appropriate color frac = i / float(self.data_size) pen = Pen(self.cols(frac), self.line_width) # find the intermediate coordinates along a line between the two # coordinates pts = self.geo.npts(lon1, lat1, lon2, lat2, self.gc_resolution) pts = np.array(pts) # if the longitudinal distance between the two points (travelling # through the prime meridian) is more than 180 degrees, it's faster # to *not* travel through the prime meridian, so we have to special- # case the drawing of the lines. if abs(lon1 - lon2) >= HALF_ROTATION: # find the index of the path where the line wraps around the image (cut_point,), = np.where(np.abs(np.diff(pts[:,0])) > HALF_ROTATION) # draw the two resultant lines separately pts1 = pts[:cut_point+1,:] pts2 = pts[cut_point+1:,:] # plot one point after the break on each sides so that the # paths go to the edge of the screen x1, y1 = pts[cut_point+2, :] x2, y2 = pts[cut_point+1, :] if x1 > 0: pts1 = np.vstack((pts1, [-HALF_ROTATION, y1])) pts2 = np.vstack(([HALF_ROTATION, y2], pts2)) else: pts1 = np.vstack((pts1, [HALF_ROTATION, y1])) pts2 = np.vstack(([-HALF_ROTATION, y2], pts2)) draw_(pts1, pen) draw_(pts2, pen) else: # the path does not wrap the image, so we can simply draw # it as-is draw_(pts, pen) canvas.flush() return img