0

I have a QGraphicsView with a QGraphicsScene scene and I add a pixmap to this scene. This works fine:

image = QtGui.QImage(1, 1, QtGui.QImage.Format_Indexed8)
image.load(fileName)
image = image.convertToFormat(6)

"""
# make white pixels transparent
transparency = QtGui.QColor(255, 255, 255, 255)
transparencyValue = transparency.value()

for x in range(image.width()):
    for y in range(image.height()):
        color = QtGui.QColor(image.pixel(x, y))
        if color.red() == 255 and color.green() == 255 and color.blue() == 255:
            image.setPixel(x, y, transparencyValue)
"""

pixmap = QtGui.QPixmap.fromImage(image)
pixmapItem = myScene.addPixmap(pixmap)

However, when I add transparency to the pixmap (the multiline comment in the code), it gets blue striped borders left and right, as well as all additional single QGraphicsPixmapItems on the scene do. How can I get rid of the borders?

Correct display without borders Faulty display with borders

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Michael Westwort
  • 914
  • 12
  • 24
  • read this: http://stackoverflow.com/questions/43922099/qt-modify-alpha-channel-transparency-of-a-windowless-qlabel/43922931#43922931 – eyllanesc May 16 '17 at 02:13
  • I don't get the point. The pixmap in the right figure is transparent. The white area in the right pixmap is the background (the canvas of the QGraphicsView) whereas the white area in the left figure is part of the pixmap. Interestingly, adding another pixmap as a background (with a small zValue) immeadiately after making the first pixmap transparent does not help. Adding a pixmap as a background in a later step lets the blue border vanish. – Michael Westwort May 17 '17 at 22:10
  • The problem is caused by the fact that you are painting pixel by pixel, the functions that implementand drawing elements use anti-aliasing. Read more [here](https://en.wikipedia.org/wiki/Spatial_anti-aliasing) – eyllanesc May 17 '17 at 22:17
  • I must correct my first comment: Adding pixmaps of colors up to (254,254,254) as background removes the borders, adding a pixmap of a color where at least one value is 255 (in particular 255,255,255) does not remove the borders. How can a uniformly white area cause aliasing that must be anti-alised? The respective part of the drawn pixmap (the white area) is completely transparent so that only the background should be visible, as it is the case without the pixmap. – Michael Westwort May 17 '17 at 22:24
  • The anti-aliasing effect is caused by the rapid color transition, when you use (255, 255, 255) you generate the quick transition. – eyllanesc May 17 '17 at 22:27
  • 1
    In the solution that I place in the link I show the correct way to load a transparent `QPixmap` without having to access each pixel. – eyllanesc May 17 '17 at 22:30

0 Answers0