Exemplo n.º 1
0
 def __call__(self, ok):
     from PyQt5.Qt import QImage, QPainter, QByteArray, QBuffer
     try:
         if not ok:
             raise RuntimeError('Rendering of HTML failed.')
         de = self.page.mainFrame().documentElement()
         pe = de.findFirst('parsererror')
         if not pe.isNull():
             raise ParserError(pe.toPlainText())
         image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
         image.setDotsPerMeterX(96*(100/2.54))
         image.setDotsPerMeterY(96*(100/2.54))
         painter = QPainter(image)
         self.page.mainFrame().render(painter)
         painter.end()
         ba = QByteArray()
         buf = QBuffer(ba)
         buf.open(QBuffer.WriteOnly)
         image.save(buf, 'JPEG')
         self.data = bytes(ba.data())
     except Exception as e:
         self.exception = e
         self.traceback = traceback.format_exc()
     finally:
         self.loop.exit(0)
Exemplo n.º 2
0
 def __call__(self, ok):
     from PyQt5.Qt import QImage, QPainter, QByteArray, QBuffer
     try:
         if not ok:
             raise RuntimeError('Rendering of HTML failed.')
         de = self.page.mainFrame().documentElement()
         pe = de.findFirst('parsererror')
         if not pe.isNull():
             raise ParserError(pe.toPlainText())
         image = QImage(self.page.viewportSize(), QImage.Format_ARGB32)
         image.setDotsPerMeterX(96*(100/2.54))
         image.setDotsPerMeterY(96*(100/2.54))
         painter = QPainter(image)
         self.page.mainFrame().render(painter)
         painter.end()
         ba = QByteArray()
         buf = QBuffer(ba)
         buf.open(QBuffer.WriteOnly)
         image.save(buf, 'JPEG')
         self.data = str(ba.data())
     except Exception as e:
         self.exception = e
         self.traceback = traceback.format_exc()
     finally:
         self.loop.exit(0)
Exemplo n.º 3
0
    def createThumb (self,path_to_perso):

        list_pic = os.listdir (path_to_perso)
        if len(list_pic) > 0 : 
            find = False
            for pic in list_pic :
                self.success = True
                if self.thumb_ext not in pic : 
                    if "portrait" in pic :
                        find = True    
                        thumbnail = QImage(os.path.join(path_to_perso,pic))
                        if (thumbnail.isNull()):
                            print ('IMAGE NULL',os.path.join(path_to_perso,pic))
                        thumbnail = thumbnail.scaledToWidth(self.thumb_width)
                        pic_basename = pic.split(".")
                        result = thumbnail.save(os.path.join(path_to_perso,self.thumb_ext+".jpg"))
                            
                        try:
                            os.rename(os.path.join(path_to_perso,pic),os.path.join(path_to_perso,self.portrait_ext+".jpg"))
                            self.nb_rename[1] = self.nb_rename[1]+1
                        except FileExistsError :
                            self.nb_rename_failed[1] = self.nb_rename_failed[1]+1
                            pass
                        if result == False :
                            self.nb_echec_thumb_creation[1] = self.nb_echec_thumb_creation[1]+ 1
                            try : 
                                qDebug("Echec de la creation de la miniature :"+os.path.join(path_to_perso,self.thumb_ext+".jpg"))
                                self.echec_list.append("FAILED save"+str(os.path.join(path_to_perso,self.thumb_ext+".jpg")))
                                
                            except UnicodeEncodeError :
                                qDebug("echec sauvegarde ****")

                        else:
                            self.nb_success_thumb_creation[1] = self.nb_success_thumb_creation[1]+1
                    else :
                        pass 
            #une image principale n existe pas
            if find == False : 
                #on fait l hypothese que portrai_ext et thumb_ext contienne tous 2 le mot "portrait"
                thumbnail = QImage(os.path.join(path_to_perso,pic))
                thumbnail = thumbnail.scaledToWidth(self.thumb_width)
                pic_basename = pic.split(".")
                result = thumbnail.save(os.path.join(path_to_perso,self.thumb_ext+".jpg"))
                if result == False : 
                    try : 
                        qDebug("Echec de la creation de la miniature :"+str(os.path.join(path_to_perso,self.thumb_ext+".jpg")))
                        self.echec_list.append("FAILED save"+str(os.path.join(path_to_perso,self.thumb_ext+".jpg")))
                    except UnicodeEncodeError :
                        qDebug("echec sauvegarde ****")
                    self.nb_echec_thumb_creation[1] = self.nb_echec_thumb_creation[1]+1
                else:
                    self.nb_success_thumb_creation[1] = self.nb_success_thumb_creation[1] + 1
                try:
                    os.rename(os.path.join(path_to_perso,pic), os.path.join(path_to_perso,self.portrait_ext+".jpg"))
                    self.nb_rename[1] = self.nb_rename[1] + 1
                except FileExistsError :
                    self.nb_rename_failed[1] = self.nb_rename_failed[1] + 1
                    pass
            else: 
                self.echec_list.append("FAILED no portrait found"+str(path_to_perso))
Exemplo n.º 4
0
    def add_image(self, img, cache_key):
        ref = self.get_image(cache_key)
        if ref is not None:
            return ref

        fmt = img.format()
        image = QImage(img)
        if (image.depth() == 1 and img.colorTable().size() == 2 and
            img.colorTable().at(0) == QColor(Qt.black).rgba() and
            img.colorTable().at(1) == QColor(Qt.white).rgba()):
            if fmt == QImage.Format_MonoLSB:
                image = image.convertToFormat(QImage.Format_Mono)
            fmt = QImage.Format_Mono
        else:
            if (fmt != QImage.Format_RGB32 and fmt != QImage.Format_ARGB32):
                image = image.convertToFormat(QImage.Format_ARGB32)
                fmt = QImage.Format_ARGB32

        w = image.width()
        h = image.height()
        d = image.depth()

        if fmt == QImage.Format_Mono:
            bytes_per_line = (w + 7) >> 3
            data = image.constBits().asstring(bytes_per_line * h)
            return self.write_image(data, w, h, d, cache_key=cache_key)

        has_alpha = False
        soft_mask = None

        if fmt == QImage.Format_ARGB32:
            tmask = image.constBits().asstring(4*w*h)[self.alpha_bit::4]
            sdata = bytearray(tmask)
            vals = set(sdata)
            vals.discard(255)  # discard opaque pixels
            has_alpha = bool(vals)
            if has_alpha:
                # Blend image onto a white background as otherwise Qt will render
                # transparent pixels as black
                background = QImage(image.size(), QImage.Format_ARGB32_Premultiplied)
                background.fill(Qt.white)
                painter = QPainter(background)
                painter.drawImage(0, 0, image)
                painter.end()
                image = background

        ba = QByteArray()
        buf = QBuffer(ba)
        image.save(buf, 'jpeg', 94)
        data = ba.data()

        if has_alpha:
            soft_mask = self.write_image(tmask, w, h, 8)

        return self.write_image(data, w, h, 32, dct=True,
                                soft_mask=soft_mask, cache_key=cache_key)
Exemplo n.º 5
0
    def add_image(self, img, cache_key):
        ref = self.get_image(cache_key)
        if ref is not None:
            return ref

        fmt = img.format()
        image = QImage(img)
        if (image.depth() == 1 and img.colorTable().size() == 2 and
            img.colorTable().at(0) == QColor(Qt.black).rgba() and
            img.colorTable().at(1) == QColor(Qt.white).rgba()):
            if fmt == QImage.Format_MonoLSB:
                image = image.convertToFormat(QImage.Format_Mono)
            fmt = QImage.Format_Mono
        else:
            if (fmt != QImage.Format_RGB32 and fmt != QImage.Format_ARGB32):
                image = image.convertToFormat(QImage.Format_ARGB32)
                fmt = QImage.Format_ARGB32

        w = image.width()
        h = image.height()
        d = image.depth()

        if fmt == QImage.Format_Mono:
            bytes_per_line = (w + 7) >> 3
            data = image.constBits().asstring(bytes_per_line * h)
            return self.write_image(data, w, h, d, cache_key=cache_key)

        has_alpha = False
        soft_mask = None

        if fmt == QImage.Format_ARGB32:
            tmask = image.constBits().asstring(4*w*h)[self.alpha_bit::4]
            sdata = bytearray(tmask)
            vals = set(sdata)
            vals.discard(255)  # discard opaque pixels
            has_alpha = bool(vals)
            if has_alpha:
                # Blend image onto a white background as otherwise Qt will render
                # transparent pixels as black
                background = QImage(image.size(), QImage.Format_ARGB32_Premultiplied)
                background.fill(Qt.white)
                painter = QPainter(background)
                painter.drawImage(0, 0, image)
                painter.end()
                image = background

        ba = QByteArray()
        buf = QBuffer(ba)
        image.save(buf, 'jpeg', 94)
        data = bytes(ba.data())

        if has_alpha:
            soft_mask = self.write_image(tmask, w, h, 8)

        return self.write_image(data, w, h, 32, dct=True,
                                soft_mask=soft_mask, cache_key=cache_key)
Exemplo n.º 6
0
 def processPendingDatagrams(self):
     while self.udpSocket.hasPendingDatagrams():
         datagram, host, port = self.udpSocket.readDatagram(
             self.udpSocket.pendingDatagramSize())
         buf = QBuffer()
         b = buf.write(self.udpSocket.readAll())
         buf.seek(buf.pos() - b)
         image = QImage()
         image.loadFromData(buf.buffer())
         image.save(r"D:\test.png")
Exemplo n.º 7
0
def to_png(bmp):
    from PyQt5.Qt import QImage, QByteArray, QBuffer
    i = QImage()
    if not i.loadFromData(bmp):
        raise ValueError('Invalid image data')
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    i.save(buf, 'png')
    return bytes(ba.data())
Exemplo n.º 8
0
def to_png(bmp):
    from PyQt5.Qt import QImage, QByteArray, QBuffer
    i = QImage()
    if not i.loadFromData(bmp):
        raise ValueError('Invalid image data')
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    i.save(buf, 'png')
    return ba.data()
Exemplo n.º 9
0
    def screenshot(self, file_name):
        # while self.load_status == "started":
        #    time.sleep(0.15)

        self.page().setViewportSize(self.page().mainFrame().contentsSize())
        image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
        painter = QPainter(image)
        self.page().mainFrame().render(painter)

        painter.end()
        image.save(file_name)
Exemplo n.º 10
0
    def rasterize_svg(self, elem, width=0, height=0, format='PNG'):
        view_box = elem.get('viewBox', elem.get('viewbox', None))
        sizes = None
        logger = self.oeb.logger

        if view_box is not None:
            try:
                box = [
                    float(x)
                    for x in [_f for _f in re.split('[, ]', view_box) if _f]
                ]
                sizes = [box[2] - box[0], box[3] - box[1]]
            except (TypeError, ValueError, IndexError):
                logger.warn(
                    'SVG image has invalid viewBox="%s", ignoring the viewBox'
                    % view_box)
            else:
                for image in elem.xpath(
                        'descendant::*[local-name()="image" and '
                        '@height and contains(@height, "%")]'):
                    logger.info(
                        'Found SVG image height in %, trying to convert...')
                    try:
                        h = float(image.get('height').replace('%', '')) / 100.
                        image.set('height', str(h * sizes[1]))
                    except:
                        logger.exception(
                            'Failed to convert percentage height:',
                            image.get('height'))

        data = QByteArray(xml2str(elem, with_tail=False))
        svg = QSvgRenderer(data)
        size = svg.defaultSize()
        if size.width() == 100 and size.height() == 100 and sizes:
            size.setWidth(sizes[0])
            size.setHeight(sizes[1])
        if width or height:
            size.scale(width, height, Qt.KeepAspectRatio)
        logger.info('Rasterizing %r to %dx%d' %
                    (elem, size.width(), size.height()))
        image = QImage(size, QImage.Format_ARGB32_Premultiplied)
        image.fill(QColor("white").rgb())
        painter = QPainter(image)
        svg.render(painter)
        painter.end()
        array = QByteArray()
        buffer = QBuffer(array)
        buffer.open(QIODevice.WriteOnly)
        image.save(buffer, format)
        return str(array)
Exemplo n.º 11
0
def scale_image(data, width=60, height=80, compression_quality=70, as_png=False, preserve_aspect_ratio=True):
    ''' Scale an image, returning it as either JPEG or PNG data (bytestring).
    Transparency is alpha blended with white when converting to JPEG. Is thread
    safe and does not require a QApplication. '''
    # We use Qt instead of ImageMagick here because ImageMagick seems to use
    # some kind of memory pool, causing memory consumption to sky rocket.
    if isinstance(data, QImage):
        img = data
    else:
        img = QImage()
        if not img.loadFromData(data):
            raise ValueError('Could not load image for thumbnail generation')
    if preserve_aspect_ratio:
        scaled, nwidth, nheight = fit_image(img.width(), img.height(), width, height)
        if scaled:
            img = img.scaled(nwidth, nheight, Qt.KeepAspectRatio, Qt.SmoothTransformation)
    else:
        if img.width() != width or img.height() != height:
            img = img.scaled(width, height, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
    if not as_png and img.hasAlphaChannel():
        nimg = QImage(img.size(), QImage.Format_RGB32)
        nimg.fill(Qt.white)
        p = QPainter(nimg)
        p.drawImage(0, 0, img)
        p.end()
        img = nimg
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    fmt = 'PNG' if as_png else 'JPEG'
    if not img.save(buf, fmt, quality=compression_quality):
        raise ValueError('Failed to export thumbnail image to: ' + fmt)
    return img.width(), img.height(), ba.data()
Exemplo n.º 12
0
 def capture(self, fullScreen = False, filename = ''):
     if fullScreen:
         image = QApplication.primaryScreen().grabWindow(0)
     else:
         image = QImage(self.webView.mainFrame.contentsSize(), QImage.Format_ARGB32)
         painter = QPainter(image)
         self.webView.mainFrame.render(painter)
         painter.end()
     if filename:
         return image.save(filename)
     else:
         data = QByteArray()
         buffer = QBuffer(data)
         buffer.open(QBuffer.WriteOnly)
         image.save(buffer, 'PNG')
         return bytes(data.toBase64()).decode()
Exemplo n.º 13
0
    def save(self, filename, chart_size, legend_width=None):
        """ save graph as file
        """
        if not filename:
            """ if filename is not specified, open a save file dialog
            """
            filename = QFileDialog.getSaveFileName(None, 'Save File')[0]
            if not filename[-4:] in ['.png', '.jpg',]:
                """ TODO : cleaner extension manager
                """
                filename += ".png"
                
        image_size = chart_size
        
        if legend_width:
            image_size = image_size + QSize(legend_width, 0)

        image = QImage(image_size, QImage.Format_ARGB32_Premultiplied)
        painter = QPainter(image)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.fillRect(image.rect(), Qt.white)
        self.draw(painter, QRect(QPoint(0, 0), chart_size))
        
        if legend_width:
            legendRect = QRect(QPoint(chart_size.width(), 10),
                               QSize(legend_width, chart_size.height()))
            self.draw_legend(painter, legendRect)
        painter.end()
        return image.save(filename)
Exemplo n.º 14
0
 def capture(self, fullScreen = False, filename = ''):
     if fullScreen:
         image = QApplication.primaryScreen().grabWindow(0)
     else:
         image = QImage(self.webView.mainFrame.contentsSize(), QImage.Format_ARGB32)
         painter = QPainter(image)
         self.webView.mainFrame.render(painter)
         painter.end()
     if filename:
         return image.save(filename)
     else:
         data = QByteArray()
         buffer = QBuffer(data)
         buffer.open(QBuffer.WriteOnly)
         image.save(buffer, 'PNG')
         return bytes(data.toBase64()).decode()
Exemplo n.º 15
0
def to_png(bmp):
    # ImageMagick does not convert some bmp files correctly, while Qt does,
    # so try Qt first. See for instance:
    # https://bugs.launchpad.net/calibre/+bug/934167
    # ImageMagick bug report:
    # http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=20350
    from PyQt5.Qt import QImage, QByteArray, QBuffer
    i = QImage()
    if i.loadFromData(bmp):
        ba = QByteArray()
        buf = QBuffer(ba)
        buf.open(QBuffer.WriteOnly)
        i.save(buf, 'png')
        return bytes(ba.data())

    from calibre.utils.magick import Image
    img = Image()
    img.load(bmp)
    return img.export('png')
Exemplo n.º 16
0
def to_png(bmp):
    # ImageMagick does not convert some bmp files correctly, while Qt does,
    # so try Qt first. See for instance:
    # https://bugs.launchpad.net/calibre/+bug/934167
    # ImageMagick bug report:
    # http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=20350
    from PyQt5.Qt import QImage, QByteArray, QBuffer
    i = QImage()
    if i.loadFromData(bmp):
        ba = QByteArray()
        buf = QBuffer(ba)
        buf.open(QBuffer.WriteOnly)
        i.save(buf, 'png')
        return bytes(ba.data())

    from calibre.utils.magick import Image
    img = Image()
    img.load(bmp)
    return img.export('png')
Exemplo n.º 17
0
    def rasterize_svg(self, elem, width=0, height=0, format='PNG'):
        view_box = elem.get('viewBox', elem.get('viewbox', None))
        sizes = None
        logger = self.oeb.logger

        if view_box is not None:
            try:
                box = [float(x) for x in filter(None, re.split('[, ]', view_box))]
                sizes = [box[2]-box[0], box[3] - box[1]]
            except (TypeError, ValueError, IndexError):
                logger.warn('SVG image has invalid viewBox="%s", ignoring the viewBox' % view_box)
            else:
                for image in elem.xpath('descendant::*[local-name()="image" and '
                        '@height and contains(@height, "%")]'):
                    logger.info('Found SVG image height in %, trying to convert...')
                    try:
                        h = float(image.get('height').replace('%', ''))/100.
                        image.set('height', str(h*sizes[1]))
                    except:
                        logger.exception('Failed to convert percentage height:',
                                image.get('height'))

        data = QByteArray(xml2str(elem, with_tail=False))
        svg = QSvgRenderer(data)
        size = svg.defaultSize()
        if size.width() == 100 and size.height() == 100 and sizes:
            size.setWidth(sizes[0])
            size.setHeight(sizes[1])
        if width or height:
            size.scale(width, height, Qt.KeepAspectRatio)
        logger.info('Rasterizing %r to %dx%d'
                    % (elem, size.width(), size.height()))
        image = QImage(size, QImage.Format_ARGB32_Premultiplied)
        image.fill(QColor("white").rgb())
        painter = QPainter(image)
        svg.render(painter)
        painter.end()
        array = QByteArray()
        buffer = QBuffer(array)
        buffer.open(QIODevice.WriteOnly)
        image.save(buffer, format)
        return str(array)
Exemplo n.º 18
0
def main():
    app = QApplication([])
    app
    tdir = os.path.abspath('.')
    pdf = os.path.join(tdir, 'painter.pdf')
    func = full
    dpi = 100
    with open(pdf, 'wb') as f:
        dev = PdfDevice(f, xdpi=dpi, ydpi=dpi, compress=False)
        img = QImage(dev.width(), dev.height(),
                     QImage.Format_ARGB32_Premultiplied)
        img.setDotsPerMeterX(dpi*39.37)
        img.setDotsPerMeterY(dpi*39.37)
        img.fill(Qt.white)
        run(dev, func)
    run(img, func)
    path = os.path.join(tdir, 'painter.png')
    img.save(path)
    print('PDF written to:', pdf)
    print('Image written to:', path)
Exemplo n.º 19
0
 def rasterize_external(self, elem, style, item, svgitem):
     width = style['width']
     height = style['height']
     width = (width / 72) * self.profile.dpi
     height = (height / 72) * self.profile.dpi
     data = QByteArray(str(svgitem))
     svg = QSvgRenderer(data)
     size = svg.defaultSize()
     size.scale(width, height, Qt.KeepAspectRatio)
     key = (svgitem.href, size.width(), size.height())
     if key in self.images:
         href = self.images[key]
     else:
         logger = self.oeb.logger
         logger.info('Rasterizing %r to %dx%d'
                     % (svgitem.href, size.width(), size.height()))
         image = QImage(size, QImage.Format_ARGB32_Premultiplied)
         image.fill(QColor("white").rgb())
         painter = QPainter(image)
         svg.render(painter)
         painter.end()
         array = QByteArray()
         buffer = QBuffer(array)
         buffer.open(QIODevice.WriteOnly)
         image.save(buffer, 'PNG')
         data = str(array)
         manifest = self.oeb.manifest
         href = os.path.splitext(svgitem.href)[0] + '.png'
         id, href = manifest.generate(svgitem.id, href)
         manifest.add(id, href, PNG_MIME, data=data)
         self.images[key] = href
     elem.tag = XHTML('img')
     for attr in elem.attrib:
         if attr not in KEEP_ATTRS:
             del elem.attrib[attr]
     elem.attrib['src'] = item.relhref(href)
     if elem.text:
         elem.attrib['alt'] = elem.text
         elem.text = None
     for child in elem:
         elem.remove(child)
Exemplo n.º 20
0
 def rasterize_external(self, elem, style, item, svgitem):
     width = style['width']
     height = style['height']
     width = (width / 72) * self.profile.dpi
     height = (height / 72) * self.profile.dpi
     data = QByteArray(str(svgitem))
     svg = QSvgRenderer(data)
     size = svg.defaultSize()
     size.scale(width, height, Qt.KeepAspectRatio)
     key = (svgitem.href, size.width(), size.height())
     if key in self.images:
         href = self.images[key]
     else:
         logger = self.oeb.logger
         logger.info('Rasterizing %r to %dx%d' %
                     (svgitem.href, size.width(), size.height()))
         image = QImage(size, QImage.Format_ARGB32_Premultiplied)
         image.fill(QColor("white").rgb())
         painter = QPainter(image)
         svg.render(painter)
         painter.end()
         array = QByteArray()
         buffer = QBuffer(array)
         buffer.open(QIODevice.WriteOnly)
         image.save(buffer, 'PNG')
         data = str(array)
         manifest = self.oeb.manifest
         href = os.path.splitext(svgitem.href)[0] + '.png'
         id, href = manifest.generate(svgitem.id, href)
         manifest.add(id, href, PNG_MIME, data=data)
         self.images[key] = href
     elem.tag = XHTML('img')
     for attr in elem.attrib:
         if attr not in KEEP_ATTRS:
             del elem.attrib[attr]
     elem.attrib['src'] = item.relhref(href)
     if elem.text:
         elem.attrib['alt'] = elem.text
         elem.text = None
     for child in elem:
         elem.remove(child)
Exemplo n.º 21
0
def encode_jpeg(file_path, quality=80):
    from calibre.utils.speedups import ReadOnlyFileBuffer
    quality = max(0, min(100, int(quality)))
    exe = get_exe_path('cjpeg')
    cmd = [exe] + '-optimize -progressive -maxmemory 100M -quality'.split() + [str(quality)]
    img = QImage()
    if not img.load(file_path):
        raise ValueError('%s is not a valid image file' % file_path)
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    if not img.save(buf, 'PPM'):
        raise ValueError('Failed to export image to PPM')
    return run_optimizer(file_path, cmd, as_filter=True, input_data=ReadOnlyFileBuffer(ba.data()))
Exemplo n.º 22
0
def encode_jpeg(file_path, quality=80):
    from calibre.utils.speedups import ReadOnlyFileBuffer
    quality = max(0, min(100, int(quality)))
    exe = get_exe_path('cjpeg')
    cmd = [exe] + '-optimize -progressive -maxmemory 100M -quality'.split() + [str(quality)]
    img = QImage()
    if not img.load(file_path):
        raise ValueError('%s is not a valid image file' % file_path)
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    if not img.save(buf, 'PPM'):
        raise ValueError('Failed to export image to PPM')
    return run_optimizer(file_path, cmd, as_filter=True, input_data=ReadOnlyFileBuffer(ba.data()))
Exemplo n.º 23
0
def scale_image(data,
                width=60,
                height=80,
                compression_quality=70,
                as_png=False,
                preserve_aspect_ratio=True):
    ''' Scale an image, returning it as either JPEG or PNG data (bytestring).
    Transparency is alpha blended with white when converting to JPEG. Is thread
    safe and does not require a QApplication. '''
    # We use Qt instead of ImageMagick here because ImageMagick seems to use
    # some kind of memory pool, causing memory consumption to sky rocket.
    if isinstance(data, QImage):
        img = data
    else:
        img = QImage()
        if not img.loadFromData(data):
            raise ValueError('Could not load image for thumbnail generation')
    if preserve_aspect_ratio:
        scaled, nwidth, nheight = fit_image(img.width(), img.height(), width,
                                            height)
        if scaled:
            img = img.scaled(nwidth, nheight, Qt.KeepAspectRatio,
                             Qt.SmoothTransformation)
    else:
        if img.width() != width or img.height() != height:
            img = img.scaled(width, height, Qt.IgnoreAspectRatio,
                             Qt.SmoothTransformation)
    if not as_png and img.hasAlphaChannel():
        nimg = QImage(img.size(), QImage.Format_RGB32)
        nimg.fill(Qt.white)
        p = QPainter(nimg)
        p.drawImage(0, 0, img)
        p.end()
        img = nimg
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    fmt = 'PNG' if as_png else 'JPEG'
    if not img.save(buf, fmt, quality=compression_quality):
        raise ValueError('Failed to export thumbnail image to: ' + fmt)
    return img.width(), img.height(), ba.data()
Exemplo n.º 24
0
def render_img(image, dest, width=128, height=128):
    from PyQt5.Qt import QImage, Qt
    img = QImage(I(image)).scaled(width, height, Qt.IgnoreAspectRatio,
                                  Qt.SmoothTransformation)
    img.save(dest)
Exemplo n.º 25
0
def render_svg(filepath):
    must_use_qt(headless=False)
    pngpath = filepath[:-4] + '.png'
    i = QImage(filepath)
    i.save(pngpath)
Exemplo n.º 26
0
def render_img(image, dest, width=128, height=128):
    from PyQt5.Qt import QImage, Qt
    img = QImage(I(image)).scaled(width, height, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
    img.save(dest)
Exemplo n.º 27
0
def render_svg(filepath):
    must_use_qt(headless=False)
    pngpath = filepath[:-4] + '.png'
    i = QImage(filepath)
    i.save(pngpath)
Exemplo n.º 28
0
# -*- coding: utf-8 -*-

__author__ = 'ipetrash'

# SOURCE: https://stackoverflow.com/a/8551810/5909792

from PyQt5.Qt import QApplication, QSvgRenderer, QImage, Qt, QPainter

# A QApplication instance is necessary if fonts are used in the SVG
app = QApplication([])

# Load your SVG
renderer = QSvgRenderer("input.svg")

for width, height in [(32, 32), (64, 64), (512, 512), (4096, 4096)]:
    # Prepare a QImage with desired characteritisc
    image = QImage(width, height, QImage.Format_ARGB32)

    # Partly transparent red-ish background
    image.fill(Qt.transparent)

    # Get QPainter that paints to the image
    painter = QPainter(image)
    renderer.render(painter)

    # Save, image format based on file extension
    image.save("output_{}x{}.png".format(width, height))

    # FIX error: "Process finished with exit code -1073741819 (0xC0000005)"
    painter.end()