如何在PyQt 4中拼接两个pixmap?

0 投票
1 回答
849 浏览
提问于 2025-04-16 15:18

假设你手上有两个相同大小的png图片文件(可以加载成两个图像对象),你想把它们横着拼接在一起,中间留一点透明的空隙,像这样:

aaaaa bbbbb
aaaaa bbbbb
aaaaa bbbbb

然后把拼接后的结果保存成另一个图像对象,应该怎么做呢?

环境:在Windows上使用PyQt 4.8.3

1 个回答

2

这应该是一个不错的起点(还没测试过):

painter = QPainter() // the QPainter to the object you want to paint

a = QImage("file1.png")
b = QImage("file2.png")
space = 5 // pixels
tile = QPixmap(QSize(a.width() + b.width(), a.height() + space)
painter2.setBrush(QBrush(QColor(0, 0, 0, 0)))
painter2.fillRect(0, 0, tile.width(), tile.height())
painter2 = QPainter(tile)
painter2.drawImage(QPoint(0, 0), a)
painter2.drawImage(QPoint(a.width(), 0), b)

painter.setBrush(tile.toImage())

// now anything you draw will be filled with the tiled pattern

撰写回答