【问题标题】:Python how to change the size of screenshot from googlemap by PyQt4Python如何通过PyQt4从googlemap更改屏幕截图的大小
【发布时间】:2016-03-18 06:31:57
【问题描述】:

最近我使用 PyQt4 来抓取 google map 的网页。 但是截图图片的大小和我在谷歌地图上看到的不完全一样

这是 PyQt4 的结果

http://imgur.com/yUKaL0p

但原来的网页是这样的

http://imgur.com/QEyKGH6

看起来它只捕获了网页的中心部分。

谁能帮我解决这个问题?

这是将网页捕获为图像的代码

class Screenshot(QWebView):
def __init__(self):
    self.app = QApplication(sys.argv)
    QWebView.__init__(self)
    self._loaded = False
    self.loadFinished.connect(self._loadFinished)

def capture(self, url, output_file):
    self.load(QUrl(url))
    self.wait_load()
    # set to webpage size
    frame = self.page().mainFrame()
    #print frame.contentsSize()
    self.page().setViewportSize(frame.contentsSize())
    # render image
    image = QImage(self.page().viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    frame.render(painter)
    painter.end()
    print 'saving', output_file
    image.save(output_file)

def wait_load(self, delay=0):
    # process app events until page loaded
    while not self._loaded:
        self.app.processEvents()
        time.sleep(delay)
    self._loaded = False

def _loadFinished(self, result):
    self._loaded = True

我尝试手动设置视口的大小(见下文),但没有成功...

self.page().setViewportSize(PyQt4.QtCore.QSize(1280, 480))

非常感谢!!

【问题讨论】:

    标签: python python-2.7 google-maps pyqt4


    【解决方案1】:

    不要使用 PyQt 来执行该操作,而是使用专门的模块来截屏,例如:

    ImageGrab

    他们在页面上的示例似乎很有帮助:

        im = ImageGrab.grabclipboard()
    
        if isinstance(im, Image.Image):
            ... got an image ...
        elif im:
           for filename in im:
               try:
                   im = Image.open(filename)
               except IOError:
                   pass # ignore this file
               else:
                   ... got an image ...
        else:
            ... clipboard empty ...
    

    由于 ImageGrab 与 PIL (or Pillow) 一起使用,您应该能够对图像进行更多控制。

    【讨论】:

    • 非常感谢,但这并不是我要问的!反正我已经解决了,谢谢~
    • 你应该发布自己的答案并接受它。
    • 问题出在 gmplot.py 中。我使用这个模块来生成 googlemap。但是,在 gmplot.py 中有 f.write( '\t
      \n') 我改变了它到 f.write('\t
      \n').
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 2016-09-25
    • 2015-12-28
    • 1970-01-01
    相关资源
    最近更新 更多