예제 #1
0
 def rotateR(self):
     # Load image for QImageWriter
     self.myImg = QImageWriter('testImage.png')
     # Rotate image with QImageWriter transformation function
     self.myImg.setTransformation(QImageIOHandler.TransformationRotate90)
     
     self.updateImage()
예제 #2
0
 def get_image_data(self, quality=90):
     if not self.is_modified:
         return self.original_image_data
     fmt = self.original_image_format or 'JPEG'
     if fmt.lower() not in set(map(lambda x:bytes(x).decode('ascii'), QImageWriter.supportedImageFormats())):
         return qimage_to_magick(self.current_image).export(fmt)
     return pixmap_to_data(self.current_image, format=fmt, quality=90)
예제 #3
0
 def get_image_data(self, quality=90):
     if not self.is_modified:
         return self.original_image_data
     fmt = self.original_image_format or 'JPEG'
     if fmt.lower() not in set(
             map(lambda x: bytes(x).decode('ascii'),
                 QImageWriter.supportedImageFormats())):
         return qimage_to_magick(self.current_image).export(fmt)
     return pixmap_to_data(self.current_image, format=fmt, quality=90)
예제 #4
0
 def get_image_data(self, quality=90):
     if not self.is_modified:
         return self.original_image_data
     fmt = self.original_image_format or 'JPEG'
     if fmt.lower() not in set(map(lambda x:bytes(x).decode('ascii'), QImageWriter.supportedImageFormats())):
         if fmt.lower() == 'gif':
             data = image_to_data(self.current_image, fmt='PNG', png_compression_level=0)
             from PIL import Image
             i = Image.open(BytesIO(data))
             buf = BytesIO()
             i.save(buf, 'gif')
             return buf.getvalue()
         else:
             raise ValueError('Cannot save %s format images' % fmt)
     return pixmap_to_data(self.current_image, format=fmt, quality=90)
예제 #5
0
파일: img.py 프로젝트: x007007007/calibre
def image_to_data(img, compression_quality=95, fmt='JPEG', png_compression_level=9, jpeg_optimized=True, jpeg_progressive=False):
    '''
    Serialize image to bytestring in the specified format.

    :param compression_quality: is for JPEG and goes from 0 to 100. 100 being lowest compression, highest image quality
    :param png_compression_level: is for PNG and goes from 0-9. 9 being highest compression.
    :param jpeg_optimized: Turns on the 'optimize' option for libjpeg which losslessly reduce file size
    :param jpeg_progressive: Turns on the 'progressive scan' option for libjpeg which allows JPEG images to be downloaded in streaming fashion
    '''
    fmt = fmt.upper()
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    if fmt == 'GIF':
        w = QImageWriter(buf, b'PNG')
        w.setQuality(90)
        if not w.write(img):
            raise ValueError('Failed to export image as ' + fmt + ' with error: ' + w.errorString())
        from PIL import Image
        im = Image.open(BytesIO(ba.data()))
        buf = BytesIO()
        im.save(buf, 'gif')
        return buf.getvalue()
    is_jpeg = fmt in ('JPG', 'JPEG')
    w = QImageWriter(buf, fmt.encode('ascii'))
    if is_jpeg:
        if img.hasAlphaChannel():
            img = blend_image(img)
        # QImageWriter only gained the following options in Qt 5.5
        if jpeg_optimized and hasattr(QImageWriter, 'setOptimizedWrite'):
            w.setOptimizedWrite(True)
        if jpeg_progressive and hasattr(QImageWriter, 'setProgressiveScanWrite'):
            w.setProgressiveScanWrite(True)
        w.setQuality(compression_quality)
    elif fmt == 'PNG':
        cl = min(9, max(0, png_compression_level))
        w.setQuality(10 * (9-cl))
    if not w.write(img):
        raise ValueError('Failed to export image as ' + fmt + ' with error: ' + w.errorString())
    return ba.data()
예제 #6
0
def image_to_data(img,
                  compression_quality=95,
                  fmt='JPEG',
                  png_compression_level=9,
                  jpeg_optimized=True,
                  jpeg_progressive=False):
    '''
    Serialize image to bytestring in the specified format.

    :param compression_quality: is for JPEG and goes from 0 to 100. 100 being lowest compression, highest image quality
    :param png_compression_level: is for PNG and goes from 0-9. 9 being highest compression.
    :param jpeg_optimized: Turns on the 'optimize' option for libjpeg which losslessly reduce file size
    :param jpeg_progressive: Turns on the 'progressive scan' option for libjpeg which allows JPEG images to be downloaded in streaming fashion
    '''
    fmt = fmt.upper()
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    if fmt == 'GIF':
        w = QImageWriter(buf, b'PNG')
        w.setQuality(90)
        if not w.write(img):
            raise ValueError('Failed to export image as ' + fmt +
                             ' with error: ' + w.errorString())
        from PIL import Image
        im = Image.open(BytesIO(ba.data()))
        buf = BytesIO()
        im.save(buf, 'gif')
        return buf.getvalue()
    is_jpeg = fmt in ('JPG', 'JPEG')
    w = QImageWriter(buf, fmt.encode('ascii'))
    if is_jpeg:
        if img.hasAlphaChannel():
            img = blend_image(img)
        # QImageWriter only gained the following options in Qt 5.5
        if jpeg_optimized and hasattr(QImageWriter, 'setOptimizedWrite'):
            w.setOptimizedWrite(True)
        if jpeg_progressive and hasattr(QImageWriter,
                                        'setProgressiveScanWrite'):
            w.setProgressiveScanWrite(True)
        w.setQuality(compression_quality)
    elif fmt == 'PNG':
        cl = min(9, max(0, png_compression_level))
        w.setQuality(10 * (9 - cl))
    if not w.write(img):
        raise ValueError('Failed to export image as ' + fmt + ' with error: ' +
                         w.errorString())
    return ba.data()
예제 #7
0
class ImageWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Image Window')

        # Create label and add pixmap to it
        self.myLabel = QLabel(self)
        myPixmap = QPixmap('testImage.png')
        self.myLabel.setPixmap(myPixmap)
        
        # Create pushbuttons
        #-------------------
        # Create pushbutton to rotate image left
        btnRotateL = QPushButton('Rotate 90° left', self)
        btnRotateL.clicked.connect(self.rotateL)
        
        # Create pushbutton to rotate image left
        btnRotateR = QPushButton('Rotate 90° right', self)
        btnRotateR.clicked.connect(self.rotateR)
        
        # Create pushbutton to mirror image hirizontally
        btnMirror = QPushButton('Mirror horizontally', self)
        btnMirror.clicked.connect(self.mirror)
                
        # Create box layout and add elements to it
        vbox = QVBoxLayout()
        vbox.addWidget(btnRotateL)
        vbox.addWidget(btnRotateR)
        vbox.addWidget(btnMirror)
        vbox.addWidget(self.myLabel)
        self.setLayout(vbox)
        
        # Set window size to match size of pixmap
        self.resize(myPixmap.width(),myPixmap.height())

        print('Image window initialized')

    def rotateL(self):
        # Load image for QImageWriter
        self.myImg = QImageWriter('testImage.png')
        # Rotate image with QImageWriter transformation function
        self.myImg.setTransformation(QImageIOHandler.TransformationRotate270)
               
        self.updateImage()
    
    def rotateR(self):
        # Load image for QImageWriter
        self.myImg = QImageWriter('testImage.png')
        # Rotate image with QImageWriter transformation function
        self.myImg.setTransformation(QImageIOHandler.TransformationRotate90)
        
        self.updateImage()

    def mirror(self):
        # Load image for QImageWriter
        self.myImg = QImageWriter('testImage.png')
        # Rotate image with QImageWriter transformation function
        self.myImg.setTransformation(QImageIOHandler.TransformationMirror)
        
        self.updateImage()
          
    def updateImage(self):
        newImage = QImage('testImage.png')
        errorValue = self.myImg.write(newImage)
        if errorValue == 0:
            print('Error, image was not saved!') 
        myPixmap = QPixmap('testImage.png')
        self.myLabel.setPixmap(myPixmap)
예제 #8
0
파일: img.py 프로젝트: timpalpant/calibre
def image_to_data(
    img, compression_quality=95, fmt="JPEG", png_compression_level=9, jpeg_optimized=True, jpeg_progressive=False
):
    """
    Serialize image to bytestring in the specified format.

    :param compression_quality: is for JPEG and goes from 0 to 100. 100 being lowest compression, highest image quality
    :param png_compression_level: is for PNG and goes from 0-9. 9 being highest compression.
    :param jpeg_optimized: Turns on the 'optimize' option for libjpeg which losslessly reduce file size
    :param jpeg_progressive: Turns on the 'progressive scan' option for libjpeg which allows JPEG images to be downloaded in streaming fashion
    """
    ba = QByteArray()
    buf = QBuffer(ba)
    buf.open(QBuffer.WriteOnly)
    fmt = fmt.upper()
    is_jpeg = fmt in ("JPG", "JPEG")
    w = QImageWriter(buf, fmt.encode("ascii"))
    if is_jpeg:
        if img.hasAlphaChannel():
            img = blend_image(img)
        # QImageWriter only gained the following options in Qt 5.5
        if jpeg_optimized and hasattr(QImageWriter, "setOptimizedWrite"):
            w.setOptimizedWrite(True)
        if jpeg_progressive and hasattr(QImageWriter, "setProgressiveScanWrite"):
            w.setProgressiveScanWrite(True)
        w.setQuality(compression_quality)
    elif fmt == "PNG":
        cl = min(9, max(0, png_compression_level))
        w.setQuality(10 * (9 - cl))
    if not w.write(img):
        raise ValueError("Failed to export image as " + fmt + " with error: " + w.errorString())
    return ba.data()