Mika Tähtinen

Capturing webpage thumbnails using Python PyQt WebKit

tiistaina 1. joulukuuta 2009 klo 14.44

Premium users in IRC-Galleria are allowed to customize their profile pages using CSS based modifications (mods). The picture above shows the tool that the users use to select from existing mods. Thumbnails for the mods are being generated using a small Python script that utilizes PyQt WebKit library and is controlled with Thrift.

Here's a simplified pseudocode example of the process used to capture the web pages containing mods.


# The QWebPage used to render the pages needs a QApplication environment, so create it
app = QApplication([])
 
# app.exec_() blocks so use QTimer to shoot the actual main
QTimer().singleShot(0, main)
app.exec_()
 
...
 
def main():
  # wait for thrift calls and sent them to snapshot()
 
...
 
def snapshot(url, filename):
  # Create page
  page = QWebPage()
 
  # Disable scrollbars from the captures
  frame.setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
  frame.setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
 
  loading = True
  success = False
 
  # loadFinished event listener for updating loading and success
  def load_finished(result):
    loading = False
    success = result
 
  # Bind loadFinished event
  connect(page, SIGNAL("loadFinished(bool)", load_finished)
 
  # Start loading
  page.mainFrame().load(QUrl(url))
 
  # Wait
  while self.loading:
    QCoreApplication.processEvents()
 
  # Check result
  if success == False:
    return
 
  # Resize the browser
  size = page.viewportSize()
  size.setWidth(1024)
  size.setHeight(1024)
  page.setViewportSize(size)
 
  # Render the page to an QImage using QPainter
  image = QImage(size, QImage.Format_RGB32)
  painter = QPainter(image)
  page.mainFrame().render(painter)
  painter.end()
 
  # Scale and save to file
  image.scaled(200, 200, Qt.IgnoreAspectRatio, Qt.SmoothTransformation).save(filename, 'JPG', 95)

Kommentit

Ei kommentteja.

Jätä uusi kommentti

Mikäli tämä kirjoitus herätti ajatuksia tai oli jollain tavalla hyödyllinen tai mielenkiintoinen niin arvostaisin suuresti, jos jättäisit kommentin! :)

Syötä nimesi!

Virheellinen osoite!

Kirjoita kommentti!

Virhe! Tarkista sanat.

Syötä yllänäkyvät sanat kenttään - tällä varmistetaan, että olet ihka oikea ihminen etkä joku salakavala spammiviestejä lähettävä automaatio. Mikäli et saa sanoista selvää, klikkaa kentän oikealla puolella olevaa päivitysnappia ja saat uudet sanat ihmeteltäviksi.