Created
July 12, 2022 00:02
-
-
Save adithya-badidey/afc1070007e8051fb17da66855291b6d to your computer and use it in GitHub Desktop.
A simple wrapper for aggdraw utils.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw | |
import aggdraw | |
class Drawing: | |
def __init__(self, image): | |
self.d = aggdraw.Draw(image) | |
self.img_height = image.height | |
self.img_width = image.width | |
def transform(self, arr): | |
return [j[i]*self.img_width if i%2==0 else j[i]*self.img_height for j in arr for i in range(len(j))] | |
def __enter__(self): | |
return self | |
def __exit__(self, exc_type, exc_value, traceback): | |
self.d.flush() | |
def rectangle(self, points, pen=None, brush=None): | |
self.d.rectangle(self.transform(points), pen, brush) | |
def line(self, *points, pen=None): | |
self.d.line(self.transform(points), pen) | |
def arc(self, points, start, end, pen=None): | |
self.d.arc(self.transform(points), start, end, pen) | |
def chord(self, points, start, end, pen=None, brush=None): | |
self.d.chord(self.transform(points), start, end, pen, brush) | |
def pieslice(self, points, start, end, pen=None, brush=None): | |
self.d.pieslice(self.transform(points), start, end, pen, brush) | |
def ellipse(self, points, pen=None, brush=None): | |
self.d.ellipse(self.transform(points), pen, brush) | |
def path(self, path, pen=None, brush=None): | |
self.d.path(path.p, pen, brush) | |
class Path: | |
def __init__(self, drawing, x, y): | |
self.p = aggdraw.Path() | |
self.img_height = drawing.img_height | |
self.img_width = drawing.img_width | |
self.p.lineto(self.transform(x, y)) | |
def transform(self, *arr): | |
return [j[i]*self.img_width if i%2==0 else j[i]*self.img_height for j in arr for i in range(len(j))] | |
def lineto(self, x, y): | |
self.p.lineto(*self.transform(x,y)) | |
def rlineto(self, x, y): | |
self.p.rlineto(*self.transform(x,y)) | |
def curveto(self, x1, y1, x2, y2, x, y): | |
self.p.curveto(*self.transform(x1, y1, x2, y2, x, y)) | |
def rcurveto(self, x1, y1, x2, y2, x, y): | |
self.p.rcurveto(*self.transform(x1, y1, x2, y2, x, y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment