0

I am trying to add a black border around a QPixmap. I dumbly thought scaling it down would do it, but this wasn't the case, as you can see here:

enter image description here

I've tried a bunch of things, QGraphicsPixmapItem(), QGraphicsDropShadowEffect(), but I think I'm missing something. I need to create a drop shadow effect, or something, and then paint that back under the QPixmap, somehow. Or fill outward for only a few pixels?

Here is some code that I've been working with:

icon = "iconpath/icon.png"
base_pixmap = QtGui.QPixmap(icon)
color = QtGui.QColor("green")

# set black outline
mask = base_pixmap.createMaskFromColor(QtCore.Qt.transparent, QtCore.Qt.MaskInColor)
base_pixmap.fill(QtGui.QColor("black"))

# set mask
base_pixmap.setMask(mask)

# Take icon, edit size, add to pixmap
pixmap_image = QtGui.QPixmap(icon)

mask = pixmap_image.createMaskFromColor(QtCore.Qt.transparent, QtCore.Qt.MaskInColor)
pixmap_image.fill(color)

# set mask
pixmap_image.setMask(mask)

painter = QtGui.QPainter(self)
margin = 10  # shrink image to fit inside border
height_percent = (float(base_pixmap.height()) / 100) * float(margin)
width_percent = (float(base_pixmap.width()) / 100) * float(margin)

painter.drawPixmap(QtCore.QRect(width_percent / 2, height_percent / 2,
                                self.width() - width_percent,
                                self.height() - height_percent),
                   pixmap_image)

painter.end()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Adam Sirrelle
  • 357
  • 8
  • 18
  • share the icon.png – eyllanesc Apr 06 '21 at 16:47
  • This icon is a Maya default icon :areaLight.closed.svg. I just tried adding it to the original post, but stack overflow won't let me upload a svg file. Honestly any image file should do, as I want this to work with any image, not just one. – Adam Sirrelle Apr 07 '21 at 01:09
  • I'm looking for this, but in Pyside: https://stackoverflow.com/questions/61405583/how-can-i-add-an-outline-stroke-border-to-a-png-image-with-pillow-library-in-pyt – Adam Sirrelle Apr 07 '21 at 01:48

0 Answers0