This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

Painting an overlay on an image

On the #pyqt channel on Freenode, hugo___ asked for a way to paint an overlay onto an image.

The following code reads two images, painting the second onto the first using a QPainter instance. QPainter allows various painting operations to be performed, and can apply transformations to a group of operations, making it quite easy to produce more complex overlays.

 1 import sys
 2 from PyQt4.QtCore import *
 3 from PyQt4.QtGui import *
 4 
 5 if __name__ == "__main__":
 6 
 7  app = QApplication(sys.argv)
 8  
 9  if len(app.arguments()) < 2:
 10  
 11  sys.stderr.write("Usage: %s <image file> <overlay file>\n" % sys.argv[0])
 12  sys.exit(1)
 13  
 14  image = QImage(app.arguments()[1])
 15  if image.isNull():
 16  sys.stderr.write("Failed to read image: %s\n" % app.arguments()[1])
 17  sys.exit(1)
 18  
 19  overlay = QImage(app.arguments()[2])
 20  if overlay.isNull():
 21  sys.stderr.write("Failed to read image: %s\n" % app.arguments()[2])
 22  sys.exit(1)
 23  
 24  if overlay.size() > image.size():
 25  
 26  overlay = overlay.scaled(image.size(), Qt.KeepAspectRatio)
 27  
 28  painter = QPainter()
 29  painter.begin(image)
 30  painter.drawImage(0, 0, overlay)
 31  painter.end()
 32  
 33  label = QLabel()
 34  label.setPixmap(QPixmap.fromImage(image))
 35  label.show()
 36  
 37  sys.exit(app.exec_())

2026年02月14日 16:12

AltStyle によって変換されたページ (->オリジナル) /