2

我用 python、gtk 和 webkit 打开页面。现在 - 如何从该页面保存图像而不从互联网再次下载?

4

1 回答 1

0

这是一个将呈现的网页保存到图像的python程序:http: //pastie.org/4572412

这应该是您最感兴趣的部分:

    size = self.browser.mainFrame().contentsSize()
    if width > 0:
        size.setWidth(width)
    self.browser.setViewportSize(size)

    # Render the virtual browsers viewport to an image.
    image = QImage(self.browser.viewportSize(), QImage.Format_ARGB32)
    paint = QPainter(image) #Have the painters target be our image object
    self.browser.mainFrame().render(paint) #Render browser window to painter
    paint.end()
    image = image.scaledToWidth(width) #ensure the image is your desired width
    extension = os.path.splitext(filename)[1][1:].upper()  #save the file as your desired image extension
    if extension not in self.image_extensions:
        raise ValueError("'filename' must be a valid extension: {0}".format(self.image_extensions))
    image.save(filename, extension)

希望这可以帮助!

于 2012-08-23T04:50:14.620 回答