def sun(self, interpolation_scheme="simple"): pil_img = PILImage.open('doubleprom_soho_full.jpg') img = fromstring(piltostring(pil_img), UInt8) img.resize((pil_img.size[1], pil_img.size[0], 3)) alpha = ones(pil_img.size, UInt8) * 255 img = concatenate((img[:, :, ::-1], alpha[:, :, newaxis]), -1).copy() return GraphicsContext(img, "bgra32", interpolation_scheme)
def sun(self, interpolation_scheme="simple"): pil_img = PILImage.open('doubleprom_soho_full.jpg') img = frombuffer(piltostring(pil_img), UInt8) img.resize((pil_img.size[1], pil_img.size[0], 3)) alpha = ones(pil_img.size, UInt8) * 255 img = concatenate((img[:, :, ::-1], alpha[:, :, newaxis]), -1).copy() return GraphicsContext(img, "bgra32", interpolation_scheme)
def sun(interpolation_scheme="simple"): path = os.path.join(os.path.dirname(__file__), 'doubleprom_soho_full.jpg') pil_img = Image.open(path) img = frombuffer(piltostring(pil_img), UInt8) img.resize((pil_img.size[1], pil_img.size[0], 3)) alpha = ones(pil_img.size, UInt8) * 255 img = concatenate((img[:, :, ::-1], alpha[:, :, newaxis]), -1).copy() return agg.GraphicsContextArray(img, "bgra32", interpolation_scheme)
def sun(interpolation_scheme="simple"): path = os.path.join(os.path.dirname(__file__), "doubleprom_soho_full.jpg") pil_img = Image.open(path) img = fromstring(piltostring(pil_img), UInt8) img.resize((pil_img.size[1], pil_img.size[0], 3)) alpha = ones(pil_img.size, UInt8) * 255 img = concatenate((img[:, :, ::-1], alpha[:, :, newaxis]), -1).copy() return agg.GraphicsContextArray(img, "bgra32", interpolation_scheme)
def draw_image(self, img, rect=None): """ draw_image(img_gc, rect=(x, y, w, h)) Draws another gc into this one. If 'rect' is not provided, then the image gc is drawn into this one, rooted at (0, 0) and at full pixel size. If 'rect' is provided, then the image is resized into the (w, h) given and drawn into this GC at point (x, y). img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's Agg backend (kiva.agg.GraphicsContextArray). Requires the Python Imaging Library (PIL). """ # We turn img into a PIL object, since that is what ReportLab # requires. To do this, we first determine if the input image # GC needs to be converted to RGBA/RGB. If so, we see if we can # do it nicely (using convert_pixel_format), and if not, we do # it brute-force using Agg. from reportlab.lib.utils import ImageReader from kiva import agg from kiva.compat import pilfromstring, piltostring if type(img) == type(array([])): # Numeric array converted_img = agg.GraphicsContextArray(img, pix_format='rgba32') format = 'RGBA' elif isinstance(img, agg.GraphicsContextArray): if img.format().startswith('RGBA'): format = 'RGBA' elif img.format().startswith('RGB'): format = 'RGB' else: converted_img = img.convert_pixel_format('rgba32', inplace=0) format = 'RGBA' else: warnings.warn("Cannot render image of type %r into PDF context." % type(img)) return # converted_img now holds an Agg graphics context with the image pil_img = pilfromstring(format, (converted_img.width(), converted_img.height()), piltostring(converted_img.bmp_array)) if rect is None: rect = (0, 0, img.width(), img.height()) # Draw the actual image. # Wrap it in an ImageReader object, because that's what reportlab # actually needs. self.gc.drawImage(ImageReader(pil_img), rect[0], rect[1], rect[2], rect[3])
def open_image(self, path): """ Resolve and read an image into an appropriate object for the renderer. """ path, open = self.resolve(path) fin = open(path) from PIL import Image from kiva.compat import piltostring import numpy pil_img = Image.open(fin) if pil_img.mode not in ('RGB', 'RGBA'): pil_img = pil_img.convert('RGBA') img = numpy.fromstring(piltostring(pil_img), numpy.uint8) shape = (pil_img.size[1],pil_img.size[0],len(pil_img.mode)) img.shape = shape return img
def open_image(self, path): """ Resolve and read an image into an appropriate object for the renderer. """ path, open = self.resolve(path) fin = open(path) from PIL import Image from kiva.compat import piltostring import numpy pil_img = Image.open(fin) if pil_img.mode not in ('RGB', 'RGBA'): pil_img = pil_img.convert('RGBA') img = numpy.fromstring(piltostring(pil_img), numpy.uint8) shape = (pil_img.size[1], pil_img.size[0], len(pil_img.mode)) img.shape = shape return img
def device_draw_image(self, img, rect): """ draw_image(img_gc, rect=(x,y,w,h)) Draws another gc into this one. If 'rect' is not provided, then the image gc is drawn into this one, rooted at (0,0) and at full pixel size. If 'rect' is provided, then the image is resized into the (w,h) given and drawn into this GC at point (x,y). img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's Agg backend (kiva.agg.GraphicsContextArray). Requires the Python Imaging Library (PIL). """ from kiva.compat import pilfromstring, piltostring, Image as PilImage # We turn img into a PIL object, since that is what ReportLab # requires. To do this, we first determine if the input image # GC needs to be converted to RGBA/RGB. If so, we see if we can # do it nicely (using convert_pixel_format), and if not, we do # it brute-force using Agg. if type(img) == type(array([])): # Numeric array converted_img = agg.GraphicsContextArray(img, pix_format='rgba32') format = 'RGBA' elif isinstance(img, agg.GraphicsContextArray): if img.format().startswith('RGBA'): format = 'RGBA' elif img.format().startswith('RGB'): format = 'RGB' else: converted_img = img.convert_pixel_format('rgba32', inplace=0) format = 'RGBA' # Should probably take this into account # interp = img.get_image_interpolation() else: warnings.warn("Cannot render image of type %r into SVG context." % type(img)) return # converted_img now holds an Agg graphics context with the image pil_img = pilfromstring(format, (converted_img.width(), converted_img.height()), piltostring(converted_img.bmp_array)) if rect == None: rect = (0, 0, img.width(), img.height()) left, top, width, height = rect if width != img.width() or height != img.height(): # This is not strictly required. pil_img = pil_img.resize((int(width), int(height)), PilImage.NEAREST) png_buffer = six.StringIO() pil_img.save(png_buffer, 'png') b64_img_data = b64encode(png_buffer.getvalue()) png_buffer.close() # Draw the actual image. m = self.get_ctm() # Place the image on the page. # Using bottom instead of top here to account for the y-flip. m = affine.translate(m, left, height + top) transform = 'matrix(%f,%f,%f,%f,%f,%f) scale(1,-1)' % affine.affine_params(m) # Flip y to reverse the flip at the start of the document. image_data = 'data:image/png;base64,' + b64_img_data self._emit('image', transform=transform, width=str(width), height=str(height), preserveAspectRatio='none', kw={ 'xlink:href': image_data })
def device_draw_image(self, img, rect): """ draw_image(img_gc, rect=(x,y,w,h)) Draws another gc into this one. If 'rect' is not provided, then the image gc is drawn into this one, rooted at (0,0) and at full pixel size. If 'rect' is provided, then the image is resized into the (w,h) given and drawn into this GC at point (x,y). img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's Agg backend (kiva.agg.GraphicsContextArray). Requires the Python Imaging Library (PIL). """ from kiva.compat import pilfromstring, piltostring if type(img) == type(array([])): # Numeric array converted_img = agg.GraphicsContextArray(img, pix_format='rgba32') format = 'RGBA' elif isinstance(img, agg.GraphicsContextArray): if img.format().startswith('RGBA'): format = 'RGBA' elif img.format().startswith('RGB'): format = 'RGB' else: converted_img = img.convert_pixel_format('rgba32', inplace=0) format = 'RGBA' # Should probably take this into account # interp = img.get_image_interpolation() else: warnings.warn("Cannot render image of type %r into EPS context." % type(img)) return # converted_img now holds an Agg graphics context with the image pil_img = pilfromstring( format, (converted_img.width(), converted_img.height()), piltostring(converted_img.bmp_array)) if rect is None: rect = (0, 0, img.width(), img.height()) # PIL PS output doesn't support alpha. if format != 'RGB': pil_img = pil_img.convert('RGB') left, top, width, height = rect if width != img.width() or height != img.height(): # This is not strictly required. pil_img = pil_img.resize((int(width), int(height)), PilImage.NEAREST) self.contents.write('gsave\n') self.contents.write('initmatrix\n') m = self.get_ctm() self.contents.write('[%.3f %.3f %.3f %.3f %.3f %.3f] concat\n' % \ affine.affine_params(m)) self.contents.write('%.3f %.3f translate\n' % (left, top)) # Rely on PIL's EpsImagePlugin to do the hard work here. pil_img.save(self.contents, 'eps', eps=0) self.contents.write('grestore\n')
def device_draw_image(self, img, rect): """ draw_image(img_gc, rect=(x,y,w,h)) Draws another gc into this one. If 'rect' is not provided, then the image gc is drawn into this one, rooted at (0,0) and at full pixel size. If 'rect' is provided, then the image is resized into the (w,h) given and drawn into this GC at point (x,y). img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's Agg backend (kiva.agg.GraphicsContextArray). Requires the Python Imaging Library (PIL). """ from kiva.compat import pilfromstring, piltostring if type(img) == type(array([])): # Numeric array converted_img = agg.GraphicsContextArray(img, pix_format='rgba32') format = 'RGBA' elif isinstance(img, agg.GraphicsContextArray): if img.format().startswith('RGBA'): format = 'RGBA' elif img.format().startswith('RGB'): format = 'RGB' else: converted_img = img.convert_pixel_format('rgba32', inplace=0) format = 'RGBA' # Should probably take this into account # interp = img.get_image_interpolation() else: warnings.warn("Cannot render image of type %r into EPS context." % type(img)) return # converted_img now holds an Agg graphics context with the image pil_img = pilfromstring(format, (converted_img.width(), converted_img.height()), piltostring(converted_img.bmp_array)) if rect == None: rect = (0, 0, img.width(), img.height()) # PIL PS output doesn't support alpha. if format != 'RGB': pil_img = pil_img.convert('RGB') left, top, width, height = rect if width != img.width() or height != img.height(): # This is not strictly required. pil_img = pil_img.resize((int(width), int(height)), PilImage.NEAREST) self.contents.write('gsave\n') self.contents.write('initmatrix\n') m = self.get_ctm() self.contents.write('[%.3f %.3f %.3f %.3f %.3f %.3f] concat\n' % \ affine.affine_params(m)) self.contents.write('%.3f %.3f translate\n' % (left, top)) # Rely on PIL's EpsImagePlugin to do the hard work here. pil_img.save(self.contents, 'eps', eps=0) self.contents.write('grestore\n')
def device_draw_image(self, img, rect): """ draw_image(img_gc, rect=(x,y,w,h)) Draws another gc into this one. If 'rect' is not provided, then the image gc is drawn into this one, rooted at (0,0) and at full pixel size. If 'rect' is provided, then the image is resized into the (w,h) given and drawn into this GC at point (x,y). img_gc is either a Numeric array (WxHx3 or WxHx4) or a GC from Kiva's Agg backend (kiva.agg.GraphicsContextArray). Requires the Python Imaging Library (PIL). """ from kiva.compat import pilfromstring, piltostring, Image as PilImage # We turn img into a PIL object, since that is what ReportLab # requires. To do this, we first determine if the input image # GC needs to be converted to RGBA/RGB. If so, we see if we can # do it nicely (using convert_pixel_format), and if not, we do # it brute-force using Agg. if type(img) == type(array([])): # Numeric array converted_img = agg.GraphicsContextArray(img, pix_format='rgba32') format = 'RGBA' elif isinstance(img, agg.GraphicsContextArray): if img.format().startswith('RGBA'): format = 'RGBA' elif img.format().startswith('RGB'): format = 'RGB' else: converted_img = img.convert_pixel_format('rgba32', inplace=0) format = 'RGBA' # Should probably take this into account # interp = img.get_image_interpolation() else: warnings.warn("Cannot render image of type %r into SVG context." % type(img)) return # converted_img now holds an Agg graphics context with the image pil_img = pilfromstring(format, (converted_img.width(), converted_img.height()), piltostring(converted_img.bmp_array)) if rect == None: rect = (0, 0, img.width(), img.height()) left, top, width, height = rect if width != img.width() or height != img.height(): # This is not strictly required. pil_img = pil_img.resize((int(width), int(height)), PilImage.NEAREST) png_buffer = StringIO() pil_img.save(png_buffer, 'png') b64_img_data = b64encode(png_buffer.getvalue()) png_buffer.close() # Draw the actual image. m = self.get_ctm() # Place the image on the page. # Using bottom instead of top here to account for the y-flip. m = affine.translate(m, left, height + top) transform = 'matrix(%f,%f,%f,%f,%f,%f) scale(1,-1)' % affine.affine_params(m) # Flip y to reverse the flip at the start of the document. image_data = 'data:image/png;base64,' + b64_img_data self._emit('image', transform=transform, width=str(width), height=str(height), preserveAspectRatio='none', kw={ 'xlink:href': image_data })