Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [Solved] QImage.save() generates strange image files
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Language Bindings
2 Posts 1 Posters 11.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Edgard
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Edgard
      wrote on last edited by
      #2

      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()
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved