aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMuriithi Frederick Muriuki2019-03-08 10:39:53 +0300
committerBonfaceKilz2020-08-08 03:26:09 +0300
commitdcd053e0249b14c938d94eb749a8b4095c80be29 (patch)
tree8fe55bd1e8aa2b13146d380baecd84f38d322870
parent95dbb4a18a205b475ca8eb72210d7d2193062295 (diff)
downloadgenenetwork2-dcd053e0249b14c938d94eb749a8b4095c80be29.tar.gz
Create new utility module for drawing
* wqflask/utility/pillow_utils.py: Create a module to hold some utility functions for drawing with Pillow. Initialise the module with a function to draw rotated text.
-rw-r--r--wqflask/utility/pillow_utils.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/wqflask/utility/pillow_utils.py b/wqflask/utility/pillow_utils.py
new file mode 100644
index 00000000..4a666e4b
--- /dev/null
+++ b/wqflask/utility/pillow_utils.py
@@ -0,0 +1,16 @@
+from PIL import Image, ImageColor, ImageDraw, ImageFont
+
+import utility.logger
+logger = utility.logger.getLogger(__name__ )
+
+BLACK = ImageColor.getrgb("black")
+# def draw_rotated_text(canvas: Image, text: str, font: ImageFont, xy: tuple, fill: ImageColor=BLACK, angle: int=-90):
+def draw_rotated_text(canvas, text, font, xy, fill=BLACK, angle=-90):
+ # type: (Image, str, ImageFont, tuple, ImageColor, int)
+ """Utility function draw rotated text"""
+ tmp_img = Image.new("RGBA", font.getsize(text), color=(0,0,0,0))
+ draw_text = ImageDraw.Draw(tmp_img)
+ draw_text.text(text=text, xy=(0,0), font=font, fill=fill)
+ tmp_img2 = tmp_img.rotate(angle, expand=1)
+ tmp_img2.save("/tmp/{}.png".format(text), format="png")
+ canvas.paste(im=tmp_img2, box=tuple([int(i) for i in xy]))