PyQt4 - 图像水印

发布于 2024-08-05 03:18:44 字数 527 浏览 7 评论 0原文

我试图打开一个 PNG 图像并通过 QImage 和 QPainter 向其中写入一些文本(水印)。该代码在 Linux 上 100% 运行,但当我在 Windows XP 上运行它时(尚未使用任何其他版本的 Windows 进行测试),文本永远不会写入图像。我的代码位于 try/ except 块中,但没有返回错误。

image = QtGui.QImage('demo.png')

painter = QtGui.QPainter()
painter.begin(image)
painter.setOpacity(0.8)
painter.setPen(QtCore.Qt.blue)
painter.setFont(QtGui.QFont('arial', 12))
painter.drawText(image.rect(), QtCore.Qt.AlignCenter, 'Watermark')
painter.end()

image.save('demo.png')

使用Python 2.6.2,PyQt 4.5.4

有什么想法吗?

I'm trying to open a PNG image and write some text to it (a watermark) via QImage and QPainter. The code works 100% on Linux but when I run it on Windows XP (haven't tested with any other versions of Windows) the text is never written to the image. I have the code in a try/except block, but no errors are returned.

image = QtGui.QImage('demo.png')

painter = QtGui.QPainter()
painter.begin(image)
painter.setOpacity(0.8)
painter.setPen(QtCore.Qt.blue)
painter.setFont(QtGui.QFont('arial', 12))
painter.drawText(image.rect(), QtCore.Qt.AlignCenter, 'Watermark')
painter.end()

image.save('demo.png')

Using Python 2.6.2, PyQt 4.5.4

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿透光 2024-08-12 03:18:44

我首先想到的可能是它在 Windows 上找不到指定的字体。

First thing that comes to my mind is maybe it isn't finding the specified font on Windows.

傾城如夢未必闌珊 2024-08-12 03:18:44

我的猜测是,无论您在 Windows 上使用什么 png 库都不会(正确)实现透明度

My guess would be that whatever png lib you are using on Windows doesn't do tranparency (properly)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文