def image(self, box, url): try: image = images.open(url, mode='pillow') # resize image. w = min([box.width, image.size[0] * self.scale_ratio]) h = min([box.height, image.size[1] * self.scale_ratio]) image.thumbnail((w, h), Image.ANTIALIAS) # centering image. w, h = image.size if box.width > w: x = box[0] + (box.width - w) // 2 else: x = box[0] if box.height > h: y = box[1] + (box.height - h) // 2 else: y = box[1] if image.mode == 'P': # convert P to RGBA to masking transparent pixels image = image.convert('RGBA') if image.mode == 'RGBA': self.paste(image, (x, y), mask=image) else: self.paste(image, (x, y)) except IOError: pass
def image(self, box, url): box = self.remap(box) try: image = images.open(url) # resize image. w = min([box.width, image.size[0]]) h = min([box.height, image.size[1]]) image.thumbnail((w, h), Image.ANTIALIAS) image = image.convert('RGB') # centering image. w, h = image.size if box.width > w: x = box[0] + (box.width - w) // 2 else: x = box[0] if box.height > h: y = box[1] + (box.height - h) // 2 else: y = box[1] size = w * h * 3 # size of bitmap (24bit color) self.write("%d %d translate", x, y + h) self.write("/imgdata %d string def", size) self.write("%d %d 8 [1 0 0 -1 0 1]", w, h) self.write("{ currentfile imgdata readhexstring pop }") self.write("false 3 colorimage") for i, rgb in enumerate(image.getdata()): self.write("%02x%02x%02x", *rgb) except IOError: pass
def image(self, box, url): try: image = images.open(url, mode='pillow') if image.mode not in ('RGBA', 'L', 'RGB', 'CYMYK'): # convert to format that reportlab can recognize image = image.convert('RGBA') y = self.size[1] - box[3] data = ImageReader(image) self.canvas.drawImage(data, box.x1, y, box.width, box.height, mask='auto', preserveAspectRatio=True) except IOError: pass
def image(self, box, url): if hasattr(url, 'read'): url = "data:;base64," + str(b64encode(url.read())) else: ext = os.path.splitext(url)[1].lower() if ext not in ('.jpg', '.png', '.gif'): stream = None try: stream = images.open(url, mode='png') url = "data:;base64," + str(b64encode(stream.read())) except IOError: pass finally: if stream: stream.close() im = image(url, box.x1, box.y1, box.width, box.height) self.svg.addElement(im)
def image(self, box, url): if hasattr(url, 'read'): url = "data:;base64," + str(b64encode(url.read())) else: if isinstance(url, Image): ext = None else: ext = os.path.splitext(url)[1].lower() if ext not in ('.jpg', '.png', '.gif'): stream = None try: stream = images.open(url, mode='png') url = "data:;base64," + str(b64encode(stream.read())) except IOError: pass finally: if stream: stream.close() im = image(url, box.x1, box.y1, box.width, box.height) self.svg.addElement(im)
def image(self, box, url): if hasattr(url, "read"): url = "data:;base64," + str(b64encode(url.read())) else: if isinstance(url, Image): ext = None else: ext = os.path.splitext(url)[1].lower() if ext not in (".jpg", ".png", ".gif"): stream = None try: stream = images.open(url, mode="png") url = "data:;base64," + str(b64encode(stream.read())) except IOError: pass finally: if stream: stream.close() im = image(url, box.x1, box.y1, box.width, box.height) self.svg.addElement(im)