def to_png(self): padding = self.pt_to_px(self.padding) left_length = self.pt_to_px(self.left_length) right_length = self.pt_to_px(self.right_length) width, height = (self.pt_to_px(self.width) + left_length + right_length, self.pt_to_px(self.height) + 2 * padding) thickness = self.pt_to_px(self.thickness) image = Image.new('RGBA', (width, height)) draw = ImageDraw.Draw(image) for bezier_points in self.get_bezier_points_lists(): bezier_points = map(lambda coordinates: tuple(map(self.pt_to_px, coordinates)), bezier_points) bezier = make_bezier(bezier_points) samples = height + 2.0 * padding half_thickness = thickness / 2.0 for point in bezier([t/samples for t in range(int(samples))]): draw.ellipse((point[0] - half_thickness, point[1] - half_thickness, point[0] + half_thickness, point[1] + half_thickness), fill = self.fill, outline = self.fill) for line_points in self.get_line_points_lists(): draw.line(map(self.pt_to_px, line_points), width=thickness, fill=self.fill) for polygon_points in self.get_polygons_points_lists(): # polygon_points is a list of tuple with coordinates draw.polygon(map(lambda coordinates: tuple(map(self.pt_to_px, coordinates)), polygon_points), fill=self.fill, ) return ImageWrapper(image, self.px_to_pt(width), self.px_to_pt(height), handlers=self.get_handlers(height))
def to_png(self): # values in pixels left_length = self.pt_to_px(self.left_length) right_length = self.pt_to_px(self.right_length) width, height = self.pt_to_px(self.size[0]) + left_length + right_length, self.pt_to_px(self.size[1]) thickness = self.pt_to_px(self.thickness) left_handler = self.pt_to_px(self.handlers['left']) right_handler = self.pt_to_px(self.handlers['right']) arrow_width = self.pt_to_px(self.arrow_width) marker = self.pt_to_px(self.marker) image = Image.new('RGBA', (width, height)) draw = ImageDraw.Draw(image) bezier_points = [(left_length, left_handler), (left_length + arrow_width, left_handler), (left_length, right_handler), (left_length + arrow_width, right_handler)] bezier = make_bezier(bezier_points) samples = float(max(left_length, right_length) + height) half_thickness = thickness / 2.0 for point in bezier([t/samples for t in range(int(samples))]): draw.ellipse((point[0] - half_thickness, point[1] - half_thickness, point[0] + half_thickness, point[1] + half_thickness), fill = self.fill, outline = self.fill) for line_points in [ (0, left_handler, left_length, left_handler), (left_length + arrow_width, right_handler, width - marker / 2, right_handler) ]: draw.line(line_points, width=thickness, fill=self.fill) if right_length > marker: for polygon_points in [[ (width, right_handler), (width - marker, right_handler - marker / 2), (width - marker, right_handler + marker / 2) ]]: # polygon_points is a list of tuple with coordinates draw.polygon(polygon_points, fill=self.fill) return ImageWrapper(image, self.px_to_pt(width), self.px_to_pt(height), handlers=self.handlers)