如何使用python保存图像?
我想使用 python 将网页中的 .jsp 图像保存到我的计算机中。
我尝试了很多方法,包括
机械化中的检索功能和 urllib.urlretrieve('http://example.com/img.jsp', 'img.jsp')
但问题是当我尝试使用图像库打开图像时它会抛出以下错误
File "code.py", line 71, in extract_image
im = Image.open(image_file)
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
我什至尝试保存.png 格式的图像,但它不起作用。 但我可以通过转到图像网址然后保存图像来手动保存。
请帮忙!
I want to save a .jsp image from a web page in to my computer using python.
I have tried many methods including
retrieve function in mechanize and
urllib.urlretrieve('http://example.com/img.jsp', 'img.jsp')
but the problem is when I try to open the image using the image library it throws the following error
File "code.py", line 71, in extract_image
im = Image.open(image_file)
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
I have even tried saving the image in .png format, but its not working.
But I can do the save manually by going to the image url and then saving the image.
Pls help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有提供足够的信息,但我的猜测是 Web 服务器没有按照您想象的方式响应 - 您是否使用 Fiddler 或 Firebug 查看 HTTP 流量或查看文件中的内容?
您可以通过其他方式获取图像的副本吗?如果可以,请将其与您以编程方式下载的内容进行比较。
最后,我不确定 JSP 图像是什么 - 如果 img.jsp 是一个用图像响应的 JavaServerPage,这不会使图像成为 JSP 图像 - 它仍然采用与其 Content-type 相对应的格式。
You haven't provided enough information, but my guess is that the web server isn't responding the way you think it is -- did you peek the HTTP traffic with Fiddler or Firebug or look at what's in the file?
Can you get a copy of the image some other way -- if so, compare that to what you downloaded programmatically.
Finally, I am not sure what a JSP image is -- if img.jsp is a JavaServerPage that responds with an image, that doesn't make the image a JSP image -- it's still in the format that corresponds to its Content-type.