[Solved] QImage.save() generates strange image files



  • I'm trying to make a very simple image creator, using PySide.
    For now, my app can draw things. But it fails to save a image file.
    According to the PySide documentation, using QImage.save() would result in the desired result. So, I wrote this script. It generates a 100x100 image with red background:

    @
    #!/usr/bin/python

    -- coding: utf-8 --

    import sys
    from PySide.QtCore import *
    from PySide.QtGui import *

    app = QApplication(sys.argv)

    size = QSize(100,100)
    picture = QImage(size, QImage.Format_RGB32)
    painter = QPainter()
    painter.begin(picture)
    brush = QBrush()
    color = QColor(255,0,0)
    brush.setColor(color)
    painter.setBackground(brush)
    painter.end()
    picture.save("image.png")

    exit()

    app.exec_()
    sys.exit()
    @

    However, it fails to create the image.
    Actually, it generates a image file. But the image file is not the one expected; it generates a black background, and some "blue lines" on the top of the image, instead.
    Does someone know what could be causing this?



  • Found the solution!
    The script isn't generating a image file properly because It doesn't has a QImage.fill(). Upon declaring one, the script generates the image correctly.
    The following script (a modified version of the first script I posted) does what I needed:

    @
    #!/usr/bin/python

    -- coding: utf-8 --

    import sys
    from PySide.QtCore import *
    from PySide.QtGui import *

    app = QApplication(sys.argv)

    size = QSize(100,100)
    picture = QImage(size, QImage.Format_RGB32)
    picture.fill(32)
    picture.setAlphaChannel(picture)

    painter = QPainter()
    painter.begin(picture)
    painter.setRenderHint(QPainter.Antialiasing)
    painter.setBackgroundMode(Qt.TransparentMode)
    pen = QPen()
    color = QColor(255,0,0)
    pen.setColor(color)
    pen.setWidth(5)
    pen.setCapStyle(Qt.RoundCap)
    painter.setPen(pen)
    painter.drawEllipse(10,10, 80,80)
    painter.end()

    imagefile = QImageWriter()
    imagefile.setFileName("circle")
    imagefile.setFormat("png")
    imagefile.setQuality(100)
    imagefile.write(picture)

    exit()

    app.exec_()
    sys.exit()
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.