使用urllib.request无法保存JPG文件的Google Maps API

发布于 2025-02-09 07:10:06 字数 646 浏览 1 评论 0原文

我使用的是Google Maps API,静态地图,并希望以jpg的格式保存图像文件。

当我使用urllib.request.urlretrieve(url,'map_46_6.png')保存png时,这很好。但是,当我使用urllib.request.urlretrieve(url,'map_46_6.jpg')时,这是不起作用的。打开文件会产生错误« 不是jpg文件:以0x89 0x50 »开始。手动更改向PNG的扩展将解决它。

以下是代码 :

import urllib.request
url = 'http://maps.googleapis.com/maps/api/staticmap?scale=2&center=46.257632,6.108669&zoom=12&size=400x400&maptype=satellite&key=xxxxx'

urllib.request.urlretrieve(url, 'map_46_6.jpg')

由于此代码是先前构建的管道的一部分,因此我需要下一步的JPG文件。

我的问题是,Urllib,Google Maps或其他可能导致此错误的其他设置?非常感谢您提前 !

I am using Google Maps API, static map, and would like to save an image file in format JPG.

When I am saving a PNG using urllib.request.urlretrieve(url, 'map_46_6.png') this is working fine. However, when I am using urllib.request.urlretrieve(url, 'map_46_6.jpg'), this is not working. Opening the file gives an error « Not a JPG file: starts with 0x89 0x50 ». Changing manually the extension to PNG will resolve it.

The following is the code :

import urllib.request
url = 'http://maps.googleapis.com/maps/api/staticmap?scale=2¢er=46.257632,6.108669&zoom=12&size=400x400&maptype=satellite&key=xxxxx'

urllib.request.urlretrieve(url, 'map_46_6.jpg')

As this code is part of a previously built pipeline, I would need the JPG files for the next steps.

My question is, is there a setting in Urllib, Google Maps or anything else that could result in this error? Thank you very much in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2025-02-16 07:10:06

我找到了解决方案。如果一个人想要JPG,则需要明确编码格式,& format = jpg如下:

import urllib.request
url = 'https://maps.googleapis.com/maps/api/staticmap?scale=2¢er=46.257632,6.108669&zoom=16&size=400x400&maptype=satellite&format=jpg&key=xxxx'
urllib.request.urlretrieve(url, 'map_46_6.jpg')

I have found a solution. If one wants jpg, one needs to explicitly code the format, &format=jpg like the following:

import urllib.request
url = 'https://maps.googleapis.com/maps/api/staticmap?scale=2¢er=46.257632,6.108669&zoom=16&size=400x400&maptype=satellite&format=jpg&key=xxxx'
urllib.request.urlretrieve(url, 'map_46_6.jpg')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文