Ejemplo n.º 1
0
def open(url, mode='Pillow'):
    if hasattr(url, 'read') or isinstance(url, Image.Image):
        stream = url
    elif not urlutil.isurl(url):
        stream = io.open(url, 'rb')
    else:
        try:
            # wrap BytesIO for rewind stream
            stream = io.BytesIO(urlopen(url).read())
        except:
            warning(u("Could not retrieve: %s"), url)
            raise IOError

    image = pillow_open(url, stream)
    if mode.lower() == 'pillow':
        # stream will be closed by GC
        return image
    else:  # mode == 'png'
        try:
            png_image = io.BytesIO()
            image.save(png_image, 'PNG')
            if hasattr(stream, 'close'):  # close() is implemented on Pillow
                stream.close()
        except:
            warning(u("Could not convert image: %s"), url)
            raise IOError

        png_image.seek(0)
        return png_image
Ejemplo n.º 2
0
    def image(self, box, url):
        if not urlutil.isurl(url):
            string = open(url, 'rb').read()
            url = "data:;base64," + str(base64.b64encode(string))

        im = image(url, box.x1, box.y1, box.width, box.height)
        self.svg.addElement(im)
Ejemplo n.º 3
0
    def image(self, box, url):
        if urlutil.isurl(url):
            from reportlab.lib.utils import ImageReader
            try:
                url = ImageReader(url)
            except:
                msg = u("WARNING: Could not retrieve: %s\n") % url
                sys.stderr.write(msg)
                return

        y = self.size[1] - box[3]
        self.canvas.drawImage(url, box.x1, y, box.width, box.height,
                              mask='auto', preserveAspectRatio=True)
Ejemplo n.º 4
0
    def image(self, box, url):
        if urlutil.isurl(url):
            try:
                from io import BytesIO as StringIO
            except ImportError:
                from cStringIO import StringIO
            try:
                from urllib.request import urlopen
            except ImportError:
                from urllib import urlopen

            try:
                url = StringIO(urlopen(url).read())
            except:
                msg = u("WARNING: Could not retrieve: %s\n") % url
                sys.stderr.write(msg)
                return
        image = Image.open(url)

        # 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]

        self.paste(image, (x, y))
Ejemplo n.º 5
0
def get_image_size(filename):
    if filename not in _image_size_cache:
        uri = filename
        if urlutil.isurl(filename):
            try:
                from io import BytesIO as StringIO
            except ImportError:
                from cStringIO import StringIO
            try:
                from urllib.request import urlopen
            except ImportError:
                from urllib import urlopen

            try:
                uri = StringIO(urlopen(filename).read())
            except IOError:
                raise RuntimeError('Could not retrieve: %s' % filename)

        try:
            _image_size_cache[filename] = Image.open(uri).size
        except:
            raise RuntimeError('Colud not get size of image: %s' % filename)

    return _image_size_cache[filename]
Ejemplo n.º 6
0
 def set_background(self, value):
     if urlutil.isurl(value) or os.path.isfile(value):
         self.background = value
     else:
         warning("background image not found: %s", value)
Ejemplo n.º 7
0
 def set_icon(self, value):
     if urlutil.isurl(value) or os.path.isfile(value):
         self.icon = value
     else:
         warning("icon image not found: %s", value)
Ejemplo n.º 8
0
 def set_background(self, value):
     if urlutil.isurl(value) or os.path.isfile(value):
         self.background = value
     else:
         warning("background image not found: %s", value)
Ejemplo n.º 9
0
 def set_icon(self, value):
     if urlutil.isurl(value) or os.path.isfile(value):
         self.icon = value
     else:
         warning("icon image not found: %s", value)
Ejemplo n.º 10
0
 def set_background(self, value):
     if urlutil.isurl(value) or os.path.isfile(value):
         self.background = value
     else:
         msg = u("WARNING: background image not found: %s\n") % value
         sys.stderr.write(msg)
Ejemplo n.º 11
0
 def set_icon(self, value):
     if urlutil.isurl(value) or os.path.isfile(value):
         self.icon = value
     else:
         msg = u("WARNING: icon image not found: %s\n") % value
         sys.stderr.write(msg)