使用Python捕获嵌入式谷歌地图图像,无需使用浏览器
我注意到,从 Google 地图页面,您可以获得一个“嵌入”链接,将其放入 iframe 中并在浏览器中加载地图。 (这里没有新闻)
图像尺寸可以调整到非常大,所以我有兴趣将一些大图像作为单个.PNG。
更具体地说,我想从边界框(右上和左下坐标)定义一个矩形区域,并使用适当的缩放系数获取相应的图像。
但我的问题是:如何使用Python来获取该地图的“像素内容”作为图像对象?
(我的理由是:如果浏览器可以获取并渲染这样的图像内容,那么Python也应该能够做到)。
编辑:这是显示我的示例地图的 HTML 文件的内容:
<iframe
width="2000"
height="1500"
frameborder="0"
scrolling="yes"
marginheight="0"
marginwidth="0"
src="http://maps.google.com.br/maps?hl=pt-BR&ll=-30.027489,-51.229248&spn=1.783415,2.745209&z=10&output=embed"/>
编辑:我按照 Ned Batchelder 的建议进行操作,并使用 读取
地址取自上面的 iframe。结果是大量的 JavaScript 代码,我认为这与 Google Maps JavaScript API 有关。所以,问题仍然存在:我怎样才能从Python中的所有这些东西中做一些有用的事情来获取地图图像?urllib.urlopen()
调用的内容src
编辑:此链接似乎包含一些有关 Google 地图如何平铺地图的非常相关的信息: http://www.codeproject.com/KB/scrapbook/googlemap.aspx
I have noticed that, from Google Maps page, you can get an "embed" link to put inside an iframe and load the map in a browser. (no news here)
The image size can be adjusted to be very large, so I am interested in getting som big images as single .PNGs.
More specifically, I would like to define a rectangular area from a bounding box (upper-right and lower-left coordinates), and get the corresponding image, with an appropriate zoom factor.
But my question is: How can I use Python to get the "pixel content" of this map as an image object?
(My rationale is: if the browser can get and render such image content, then Python should be capable of doing it, too).
EDIT: this is the content of the HTML file that shows my sample map:
<iframe
width="2000"
height="1500"
frameborder="0"
scrolling="yes"
marginheight="0"
marginwidth="0"
src="http://maps.google.com.br/maps?hl=pt-BR&ll=-30.027489,-51.229248&spn=1.783415,2.745209&z=10&output=embed"/>
EDIT: I did as suggested by Ned Batchelder, and read the content of an urllib.urlopen()
call using the src
address taken from the iframe above. The result was a lot of javascript code, which I think has to do with the Google Maps JavaScript API. So, the question lingers: how could I do some useful stuff from all this stuff in Python in order to get the map image?
EDIT: this link appears to contain some pretty relevant info on how Google Maps tiles their maps:
http://www.codeproject.com/KB/scrapbook/googlemap.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我感谢所有的答案。我最终以另一种方式解决了这个问题,使用 Google Maps Static API 和一些公式将坐标空间转换为像素空间,这样我就可以获得很好地“缝合”在一起的精确图像。
对于任何感兴趣的人,这里是代码。如果对某人有帮助,请评论!
=============================
I thank for all the answers. I ended up solving the problem another way, using Google Maps Static API and some formulas to convert from Coordinate space to Pixel space, so that I can get precise images that "stitch" nicely together.
For anyone interested, here is the code. If it helps someone, please comment!
=============================
您应该直接访问 Google API 以获取静态图形形式的图像,而不是尝试使用嵌入链接。这是 Google 地图静态图像 API 的链接 - 您似乎可以只需在 URL 中传入 long/lat 参数,就像处理普通的可嵌入参数一样。例如:
为您提供以上面给出的坐标为中心的 600x600 街道级别概览,该坐标似乎是巴西的阿雷格里港。现在您可以按照 Ned 的建议使用
urlopen
和PIL
:Rather than trying to use the embed link, you should go directly to the Google API to get images as static graphics. Here's the link to the Google Maps static image API - it looks like you can just pass in the long/lat parameters in the URL just as you do for the normal embeddable one. For example:
gives you an 600x600 street-level overview centered on the co-ordinates you give above, which seems to be Porto Alegre in Brazil. Now you can use
urlopen
andPIL
as Ned suggests:编辑:
此答案中的代码已得到改进和简化,此处:https://stackoverflow.com/a/50536888/5859283
基于 heltonbiker 的出色回答以及 BenElgar 的更改,下面是 Python 3 的一些更新代码并添加了 API 密钥访问,希望它对某人有用:
Edit:
the code in this answer has been improved and simplified, here: https://stackoverflow.com/a/50536888/5859283
Based on the excellent answer from heltonbiker with changes from BenElgar, below is some updated code for Python 3 and the addition of API key access, hope its useful for somebody:
@4Oh4 的答案是正确的,但数学比他们需要的更复杂。度数和弧度之间的转换发生得比需要的要频繁。无缘无故地调用地球半径——它在所有计算中都被抵消。无缘无故地向像素坐标添加了偏移量。标志的截面积比需要的要大。还有其他一些零碎的东西,这些都已写在更改中。这是我的版本:
@4Oh4's answer is right, but the maths are more complicated than they need to be. Conversions between degrees and radians happen more often than they need to. The Earth's radius is invoked for no reason—it cancels in all calculations. An offset is added to the pixel coordinates for no reason. The logo cutoff is bigger than it needs to be. And a few other odds and ends, which have been written in the changes. Here's my version:
这是 Daniel Roseman 为使用 python 3.x 的人提供的答案:
Python 3.x 代码:
This is Daniel Roseman's answer for people that use python 3.x:
Python 3.x code:
一个更简洁的Python 2.x兼容方法是
A more concise Python 2.x compatible method is
捕获/保存 Google 静态地图图像(作为 png)的最简单方法:
The most simplest way to have the Google static map image captured/saved (as a png):
urllib.urlopen
将打开一个 URL,结果将有一个.read()
方法,您可以使用它来获取图像字节。cStringIO
有一个基于内存中字符串的类文件对象。 PIL 有一个 Image.open 函数,可以打开一个类似文件的东西来给你一个图像对象。可以询问图像对象的像素值。urllib.urlopen
will open a URL, the result will have a.read()
method you can use to get the image bytes.cStringIO
has a file-like object based on a string in memory. PIL has anImage.open
function that opens a file-like thing to give you an image object. Image objects can be asked about their pixel values.