def display_boxes(self, pdf_file, boxes, alternate_colors=False): """ Displays each of the bounding boxes passed in 'boxes' on images of the pdf pointed to by pdf_file boxes is a list of 5-tuples (page, top, left, bottom, right) """ imgs = [] colors = [Color('blue'), Color('red')] boxes_per_page = defaultdict(int) boxes_by_page = defaultdict(list) for i, (page, top, left, bottom, right) in enumerate(boxes): boxes_per_page[page] += 1 boxes_by_page[page].append((top, left, bottom, right)) for i, page_num in enumerate(boxes_per_page.keys()): img = pdf_to_img(pdf_file, page_num) draw = Drawing() draw.fill_color = Color('rgba(0, 0, 0, 0.0)') for j, (top, left, bottom, right) in enumerate(boxes_by_page[page_num]): draw.stroke_color = colors[ j % 2] if alternate_colors else colors[0] draw.rectangle(left=left, top=top, right=right, bottom=bottom) draw(img) imgs.append(img) return imgs
def display_bounding_boxes(self, page_num, bboxes, alternate_colors=True): elems = self.elems[page_num] page_width, page_height = int(elems.layout.width), int(elems.layout.height) img = pdf_to_img(self.pdf_file, page_num, page_width, page_height) draw = Drawing() draw.fill_color = Color("rgba(0, 0, 0, 0)") color = Color("blue") draw.stroke_color = color for block in bboxes: top, left, bottom, right = block[-4:] draw.stroke_color = Color( "rgba({},{},{}, 1)".format( str(np.random.randint(255)), str(np.random.randint(255)), str(np.random.randint(255)), ) ) draw.rectangle( left=float(left), top=float(top), right=float(right), bottom=float(bottom), ) draw(img) return img
def draw_strip(self, filename): led_width = 20 led_height = 20 color_width = int(led_width * 0.8) color_height = int(led_height * 0.8) img = Image(width=(led_width * self.led_count), height=led_height, background=Color('#000000')) draw = Drawing() draw.stroke_width = 3 for n in range(0, self.led_count): draw.fill_color = Color('#%02x%02x%02x' % (self.led_list[n][0], self.led_list[n][1], self.led_list[n][2])) draw.rectangle(left=int(led_width * n + 0.5 * (led_width - color_width)), top=int(0.5 * (led_height - color_height)), width=color_width, height=color_height) draw(img) img.save(filename=filename)
def wand1(): """This is Python Wand example 1""" the_time = t.asctime() print "Importing image ", IFILE img_1 = Image(filename=IFILE) print "Cropping and resizing the image" img_1.crop(300, 0, width=300, height=282) img_1.resize(width=600, height=564) print "Creating a drawing and overlaying on it" draw = Drawing() draw.circle((100, 100), (120, 120)) draw.rectangle(left=img_1.width-300, top=img_1.height-45, width=230, height=40, radius=5) draw.font_size = 17 draw.fill_color = Color('white') draw.text_color = Color('white') draw.text(img_1.width-290, img_1.height-20, the_time) draw(img_1) print "Displaying, close the XTERM when done" display(img_1)
def display_bounding_boxes_within_notebook(page_num, extractor, blocks, alternatecolors=False, color=Color('blue')): """ Displays each of the bounding boxes passed in 'boxes' on an image of the pdf pointed to by pdf_file boxes is a list of 5-tuples (page, top, left, bottom, right) """ elems = extractor.elems[page_num] page_width, page_height = int(elems.layout.width), int(elems.layout.height) img = pdf_to_img(extractor.pdf_file, page_num, page_width, page_height) draw = Drawing() draw.fill_color = Color('rgba(0, 0, 0, 0)') draw.stroke_color = color for block in blocks: top, left, bottom, right = block[-4:] if alternatecolors: draw.stroke_color = Color('rgba({},{},{}, 1)'.format( str(np.random.randint(255)), str(np.random.randint(255)), str(np.random.randint(255)))) draw.rectangle(left=float(left), top=float(top), right=float(right), bottom=float(bottom)) draw(img) return img
def display_bounding_boxes(img, blocks, alternatecolors=False, color=Color("blue")): """ Displays each of the bounding boxes passed in 'boxes' on an image of the pdf pointed to by pdf_file boxes is a list of 5-tuples (page, top, left, bottom, right) """ draw = Drawing() draw.fill_color = Color("rgba(0, 0, 0, 0)") draw.stroke_color = color for block in blocks: top, left, bottom, right = block[-4:] if alternatecolors: draw.stroke_color = Color("rgba({},{},{}, 1)".format( str(np.random.randint(255)), str(np.random.randint(255)), str(np.random.randint(255)), )) draw.rectangle(left=float(left), top=float(top), right=float(right), bottom=float(bottom)) draw(img) display(img)
def create_kernel(w=3, h=3): k = Image(width=w * kernel_size + 2, height=h * kernel_size + 2) draw = Drawing() draw.fill_color = Color("white") draw.stroke_color = Color("black") for y in xrange(0,h): for x in xrange(0,w): draw.rectangle(left=x*kernel_size, top=y*kernel_size, width=kernel_size, height=kernel_size) draw(k) return k
def create_kernel(w=3, h=3): k = Image(width=w * kernel_size + 2, height=h * kernel_size + 2) draw = Drawing() draw.fill_color = Color("white") draw.stroke_color = Color("black") for y in xrange(0, h): for x in xrange(0, w): draw.rectangle(left=x * kernel_size, top=y * kernel_size, width=kernel_size, height=kernel_size) draw(k) return k
def display_boxes(self, tree, html_path, filename_prefix, alternate_colors=False): """ Displays each of the bounding boxes passed in 'boxes' on images of the pdf pointed to by pdf_file boxes is a list of 5-tuples (page, top, left, bottom, right) """ imgs = [] colors = { "section_header": Color('blue'), "figure": Color('green'), "figure_caption": Color('green'), "table_caption": Color('red'), "list": Color('yellow'), "paragraph": Color('gray'), "table": Color('red'), "header": Color("brown") } for i, page_num in enumerate(tree.keys()): img = self.pdf_to_img(page_num) draw = Drawing() draw.fill_color = Color('rgba(0, 0, 0, 0.0)') for clust in tree[page_num]: for (pnum, pwidth, pheight, top, left, bottom, right) in tree[page_num][clust]: draw.stroke_color = colors[clust] draw.rectangle(left=left, top=top, right=right, bottom=bottom) draw.push() draw.font_size = 20 draw.font_weight = 10 draw.fill_color = colors[clust] if int(left) > 0 and int(top) > 0: draw.text(x=int(left), y=int(top), body=clust) draw.pop() draw(img) img.save(filename=html_path + filename_prefix + "_page_" + str(i) + '.png') imgs.append(img) return imgs
def blur_edges(image, border): rect = Image(width=image.width, height=image.height, background=Color('white')) drawing = Drawing() drawing.rectangle(left=border, top=border, width=image.width - 2 * border, height=image.height - 2 * border, radius=100) drawing(rect) rect.negate() rect.gaussian_blur(radius=10, sigma=15) with Drawing() as draw: draw.composite('multiply', 0, 0, image.width, image.height, rect) draw(image)
def combine_images(*images: List[Union[str, bytes, WandImage]]) -> Image: with ThreadPool() as pool: images = pool.map(normalize_image, images) max_width = round(mean(img.width for img in images)) max_height = round(mean(img.height for img in images)) sum_width = max_width * len(images) canvas = WandImage(width=sum_width, height=max_height) resizer = ImageResizer(max_width, max_height) left = 0 draw = Drawing() draw.fill_color = Color('white') draw.rectangle(left=0, top=0, right=canvas.width, bottom=canvas.height) draw(canvas) for img in images: if img.height < max_height: img = resizer.resize(img, max_height / img.height) if img.width < max_width: img = resizer.resize(img, max_width / img.width) elif img.width > max_width: # Do a bit of cropping so it's centered crop_left = round((img.width - max_width) / 2) crop_right = crop_left + max_width img.crop(crop_left, 0, crop_right, img.height) draw.composite(operator='over', top=0, left=left, width=img.width, height=img.height, image=img) draw(canvas) left += img.width if DEBUG_IMAGE: # pragma: no cover server_name = os.environ.get('DISPLAY', ':0') display(canvas, server_name=server_name) return Image(content=canvas.make_blob(format='png'), content_type='image/png')
class WandPanelDisplay(PanelDisplay): def __init__(self, width=600, height=400): super().__init__("Graphics Panel Display", width, height) self.draw = Drawing() def drawRectangle(self, r, outline_colour="black", outline_width=1, fill=None): self.draw.stroke_color = Color(outline_colour) self.draw.stroke_width = outline_width self.draw.rectangle(left=r.tl.x, top=r.tl.y, right=r.br.x, bottom=r.br.y) def show(self): with Image(width=self.width, height=self.height, background=Color('lightblue')) as img: self.draw.draw(img) img.save(filename='draw-panel.svg')
# syslog.syslog(syslog.LOG_INFO, 'Setting Night Parameters') F.SetCamera(cam,NightSettings) # syslog.syslog(syslog.LOG_INFO, 'Taking Photo') cam.capture(Settings['FTPFile']) img = Image(filename=Settings['FTPFile']) OverImg = Image(filename='/boot/TMLogo.png') draw = Drawing() draw.composite(operator='over',left=img.width - OverImg.width - 5,top=5,width=OverImg.width,height=OverImg.height,image=OverImg) draw(img) draw = Drawing() draw.fill_color = Color('blue') draw.fill_opacity = 0.5 draw.rectangle(0,img.height - 30,img.width,img.height) draw(img) draw = Drawing() draw.font = 'wandtests/assets/League_Gothic.otf' draw.font_size = 20 draw.fill_color = Color('white') draw.text_alignment = 'left' draw.text(5, img.height - 5, Settings['Description']) draw(img) draw = Drawing() draw.font = 'wandtests/assets/League_Gothic.otf' draw.font_size = 20 draw.fill_color = Color('white') draw.text_alignment = 'right'
def draw_faces(self, image, progress_tracker, with_back=True, only_back=False): margin_height = (self.paper['height'] - self.pattern_height()) / 2 margin_width = (self.paper['width'] - self.pattern_width()) / 2 face_sizes = { "front": (math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)), "back": (math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)), "left": (math.ceil(self.tuckbox['depth'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)), "right": (math.ceil(self.tuckbox['depth'] * POINT_PER_MM), math.ceil(self.tuckbox['height'] * POINT_PER_MM)), "top": (math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['depth'] * POINT_PER_MM)), "bottom": (math.ceil(self.tuckbox['width'] * POINT_PER_MM), math.ceil(self.tuckbox['depth'] * POINT_PER_MM)), } face_positions = { "front": (math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)), "back": (math.floor((margin_width + self.tuckbox['depth']*2 + self.tuckbox['width']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)), "left": (math.floor(margin_width * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)), "right": (math.floor((margin_width + self.tuckbox['depth'] + self.tuckbox['width']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)), "top": (math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.lip_size()) * POINT_PER_MM)), "bottom": (math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth'] + self.tuckbox['height']) * POINT_PER_MM)), } if only_back: faces = ["back"] # We are drawing the second page with the back face only, making some assumptions on its position face_positions["back"] = (math.floor((margin_width + self.tuckbox['depth']) * POINT_PER_MM), math.floor((margin_height + self.lip_size() + self.tuckbox['depth']) * POINT_PER_MM)) else: faces = list(face_sizes.keys()) if not with_back: faces.remove("back") face_angles = {} for face in faces: face_angles[face] = self.options[face + "_angle"] if face+"_angle" in self.options else 0 # Apply those face pictures for counter, side in enumerate(faces): if side in self.faces: if self.faces[side][:1] == "#": # we are filling with color color_face_draw = Drawing() color_face_draw.stroke_width = 0 color_face_draw.fill_color = Color(self.faces[side]) color_face_draw.rectangle(left = face_positions[side][0], top = face_positions[side][1], width = face_sizes[side][0], height = face_sizes[side][1]) color_face_draw.draw(image) else: _, file_extension = os.path.splitext(os.path.basename(self.faces[side])) tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) self.resize_rotate_image(self.faces[side], tmp_file.name, face_angles[side] * 90, *face_sizes[side]) with Image(filename=tmp_file.name) as i: image.composite(i, *face_positions[side]) if progress_tracker is not None: progress_tracker(10*(counter+2))
def make_namecard(user_info_list, team_info, template): # check team directory exist(if not, make) path_team_dir = '/'.join((os.path.abspath(os.path.dirname(__file__)), 'deliverable', team_info['name'])) if not os.path.isdir(path_team_dir): os.makedirs(path_team_dir) # check icon directory exist(if not, make) path_icon_dir = '/'.join((path_team_dir, 'icon')) if not os.path.isdir(path_icon_dir): os.makedirs(path_icon_dir) # check namecard directory exist(if not, make) path_card_dir = '/'.join((path_team_dir, 'namecard')) if not os.path.isdir(path_card_dir): os.makedirs(path_card_dir) # make plain card base = Image(width=template['width'], height=template['height'], background=Color('rgb(255, 255, 255)')) frame = Drawing() frame.stroke_color = Color('rgb(0, 0, 0)') frame.fill_color = Color('rgba(255, 255, 255, 0)') frame.rectangle(left=0, top=0, width=base.width - 1, height=base.height - 1) frame(base) # draw team logo if template['logo']['display']: # check team icon exist offline(if not, get) path_logo_file = ''.join((path_team_dir, '/logo.jpg')) if not os.path.isfile(path_logo_file): icon_collector.get_image(team_info['icon'], path_logo_file) logo_draw = Image(filename=path_logo_file) logo_draw.resize(template['logo']['size'], template['logo']['size']) if template['logo']['align'] == 'left': logo_left = template['logo']['x'] elif template['logo']['align'] == 'center': logo_left = template['logo']['x'] - int( template['logo']['size'] / 2) elif template['logo']['align'] == 'right': logo_left = template['logo']['x'] - template['logo']['size'] logo_top = template['logo']['y'] base.composite(logo_draw, logo_left, logo_top) # draw team name if template['team']['display']: team_draw = Drawing() if template['team']['font'] != 'default': team_draw.font = template['team']['font'] if template['team']['size'] != 'default': team_draw.font_size = template['team']['size'] mtr_team = team_draw.get_font_metrics(base, team_info['name']) if template['team']['align'] == 'left': team_left = template['team']['x'] elif template['team']['align'] == 'center': team_left = template['team']['x'] - int(mtr_team.text_width / 2) elif template['team']['align'] == 'center': team_left = template['team']['x'] - int(mtr_team.text_width) team_top = template['team']['y'] + int(mtr_team.ascender) team_draw.text(team_left, team_top, team_info['name']) team_draw(base) #save dummy card base.save(filename=''.join((path_team_dir, '/dummy.png'))) for user in user_info_list.values(): base_clone = base.clone() # draw user icon if template['icon']['display']: # check user icon exist offline(if not, get) path_icon_file = ''.join( (path_icon_dir, '/', user['name'], '.png')) if not os.path.isfile(path_icon_file): icon_collector.get_image(user['icon'], path_icon_file) icon_draw = Image(filename=path_icon_file) icon_draw.resize(template['icon']['size'], template['icon']['size']) if template['icon']['align'] == 'left': icon_left = template['icon']['x'] elif template['icon']['align'] == 'center': icon_left = template['icon']['x'] - int( template['icon']['size'] / 2) elif template['icon']['align'] == 'right': icon_left = template['icon']['x'] - template['icon']['size'] icon_top = template['icon']['y'] base_clone.composite(icon_draw, icon_left, icon_top) # draw real name if template['real']['display']: real_draw = Drawing() if template['real']['font'] != 'default': real_draw.font = template['real']['font'] if template['real']['size'] != 'default': real_draw.font_size = template['real']['size'] user_real_name = user['real_name'] mtr_real = real_draw.get_font_metrics(base_clone, user_real_name) if mtr_real.text_width > template['real']['width']: user_real_name = user['real_name'].replace(' ', '\n') mtr_real = real_draw.get_font_metrics(base_clone, user_real_name) if template['real']['align'] == 'left': real_left = template['real']['x'] elif template['real']['align'] == 'center': real_left = template['real']['x'] - int( mtr_real.text_width / 2) elif template['real']['align'] == 'center': real_left = template['real']['x'] - int(mtr_real.text_width) real_top = template['real']['y'] + int(mtr_real.ascender) real_draw.text(real_left, real_top, user_real_name) real_draw(base_clone) # draw name if template['name']['display']: name_draw = Drawing() if template['name']['font'] != 'default': name_draw.font = template['name']['font'] if template['name']['size'] != 'default': name_draw.font_size = template['name']['size'] user_name = ''.join(('@', user['name'])) mtr_name = name_draw.get_font_metrics(base_clone, user_name) if template['name']['align'] == 'left': name_left = template['name']['x'] elif template['name']['align'] == 'center': name_left = template['name']['x'] - int( mtr_name.text_width / 2) elif template['name']['align'] == 'center': name_left = template['name']['x'] - int(mtr_name.text_width) name_top = template['name']['y'] + int(mtr_name.ascender) name_draw.text(name_left, name_top, user_name) name_draw(base_clone) # draw title if template['title']['display']: title_draw = Drawing() if template['title']['font'] != 'default': title_draw.font = template['title']['font'] if template['title']['size'] != 'default': title_draw.font_size = template['title']['size'] user_title = user['title'] mtr_title = title_draw.get_font_metrics(base_clone, user_title) if template['title']['align'] == 'left': title_left = template['title']['x'] elif template['title']['align'] == 'center': title_left = template['title']['x'] - int( mtr_title.text_width / 2) elif template['title']['align'] == 'center': title_left = template['title']['x'] - int(mtr_title.text_width) title_top = template['title']['y'] + int(mtr_title.ascender) title_draw.text(title_left, title_top, user_title) title_draw(base_clone) base_clone.save(filename=''.join((path_card_dir, '/', user['name'], '.png')))