Skip to content

Instantly share code, notes, and snippets.

@Ahanmr
Created March 30, 2020 12:33
Show Gist options
  • Save Ahanmr/eae72a803abab6f14a680d7a1e945372 to your computer and use it in GitHub Desktop.
Save Ahanmr/eae72a803abab6f14a680d7a1e945372 to your computer and use it in GitHub Desktop.
import PIL.Image
import PIL.ImageDraw
import wand.image
import sys, os
from io import BytesIO
def draw_rect(self, bbox_or_obj,
fill=DEFAULT_FILL,
stroke=DEFAULT_STROKE,
stroke_width=DEFAULT_STROKE_WIDTH):
if isinstance(bbox_or_obj, (tuple, list)):
bbox = bbox_or_obj
else:
obj = bbox_or_obj
bbox = (obj["x0"], obj["top"], obj["x1"], obj["bottom"])
x0, top, x1, bottom = bbox
half = self.decimalize(stroke_width / 2)
x0 += half
top += half
x1 -= half
bottom -= half
self.draw.rectangle(
self._reproject_bbox((x0, top, x1, bottom)),
fill,
COLORS.TRANSPARENT
)
if stroke_width > 0:
segments = [
((x0, top), (x1, top)), # top
((x0, bottom), (x1, bottom)), # bottom
((x0, top), (x0, bottom)), # left
((x1, top), (x1, bottom)), # right
]
self.draw_lines(
segments,
stroke=stroke,
stroke_width=stroke_width
)
return self
def draw_rects(self, list_of_rects, **kwargs):
for x in utils.to_list(list_of_rects):
self.draw_rect(x, **kwargs)
return self
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment