Python:将图像从Web保存到磁盘

时间:2023-01-24 17:58:54

Can I save images to disk using python? An example of an image would be:

我可以使用python将图像保存到磁盘吗?图像的一个例子是:

Python:将图像从Web保存到磁盘

3 个解决方案

#1


3  

Easiest is to use urllib.urlretrieve.

最简单的是使用urllib.urlretrieve。

Python 2:

import urllib
urllib.urlretrieve('http://chart.apis.google.com/...', 'outfile.png')

Python 3:

import urllib.request
urllib.request.urlretrieve('http://chart.apis.google.com/...', 'outfile.png')

#2


1  

If your goal is to download a png to disk, you can do so with urllib:

如果您的目标是将png下载到磁盘,则可以使用urllib执行此操作:

import urllib
urladdy = "http://chart.apis.google.com/chart?chxl=1:|0|10|100|1%2C000|10%2C000|100%2C000|1%2C000%2C000|2:||Excretion+in+Nanograms+per+gram+creatinine+milliliter+(logarithmic+scale)|&chxp=1,0|2,0&chxr=0,0,12.1|1,0,3&chxs=0,676767,13.5,0,lt,676767|1,676767,13.5,0,l,676767&chxtc=0,-1000&chxt=y,x,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,12.1&chd=t:0,0,0,0,0,0,0,0,0,1,0,0,3,2,4,6,6,9,3,6,5,11,9,10,6,2,2,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0&chdl=n=87&chtt=William+MD+-+Buprenorphine+Graph"
filename = r"c:\tmp\toto\file.png"
urllib.urlretrieve(urladdy, filename)

In python 3, you will need to use urllib.request.urlretrieve instead of urllib.urlretrieve.

在python 3中,您需要使用urllib.request.urlretrieve而不是urllib.urlretrieve。

#3


0  

The Google chart API produces PNG files. Just retrieve them with urllib.urlopen(url).read() or something along these lines and safe to a file the usual way.

Google图表API会生成PNG文件。只需使用urllib.urlopen(url).read()或这些行中的某些内容检索它们,并以通常的方式安全地访问文件。

Full example:

>>> import urllib
>>> url = 'http://chart.apis.google.com/chart?chxl=1:|0|10|100|1%2C000|10%2C000|100%2C000|1%2C000%2C000|2:||Excretion+in+Nanograms+per+gram+creatinine+milliliter+(logarithmic+scale)|&chxp=1,0|2,0&chxr=0,0,12.1|1,0,3&chxs=0,676767,13.5,0,lt,676767|1,676767,13.5,0,l,676767&chxtc=0,-1000&chxt=y,x,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,12.1&chd=t:0,0,0,0,0,0,0,0,0,1,0,0,3,2,4,6,6,9,3,6,5,11,9,10,6,2,2,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0&chdl=n=87&chtt=William+MD+-+Buprenorphine+Graph'
>>> image = urllib.urlopen(url).read()
>>> outfile = open('chart01.png','wb')
>>> outfile.write(image)
>>> outfile.close()

As noted in other examples, 'urllib.urlretrieve(url, outfilename)` is even more straightforward, but playing with urllib and urllib2 will surely be instructive for you.

如其他示例中所述,'urllib.urlretrieve(url,outfilename)`更加直截了当,但使用urllib和urllib2肯定会对你有所帮助。

#1


3  

Easiest is to use urllib.urlretrieve.

最简单的是使用urllib.urlretrieve。

Python 2:

import urllib
urllib.urlretrieve('http://chart.apis.google.com/...', 'outfile.png')

Python 3:

import urllib.request
urllib.request.urlretrieve('http://chart.apis.google.com/...', 'outfile.png')

#2


1  

If your goal is to download a png to disk, you can do so with urllib:

如果您的目标是将png下载到磁盘,则可以使用urllib执行此操作:

import urllib
urladdy = "http://chart.apis.google.com/chart?chxl=1:|0|10|100|1%2C000|10%2C000|100%2C000|1%2C000%2C000|2:||Excretion+in+Nanograms+per+gram+creatinine+milliliter+(logarithmic+scale)|&chxp=1,0|2,0&chxr=0,0,12.1|1,0,3&chxs=0,676767,13.5,0,lt,676767|1,676767,13.5,0,l,676767&chxtc=0,-1000&chxt=y,x,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,12.1&chd=t:0,0,0,0,0,0,0,0,0,1,0,0,3,2,4,6,6,9,3,6,5,11,9,10,6,2,2,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0&chdl=n=87&chtt=William+MD+-+Buprenorphine+Graph"
filename = r"c:\tmp\toto\file.png"
urllib.urlretrieve(urladdy, filename)

In python 3, you will need to use urllib.request.urlretrieve instead of urllib.urlretrieve.

在python 3中,您需要使用urllib.request.urlretrieve而不是urllib.urlretrieve。

#3


0  

The Google chart API produces PNG files. Just retrieve them with urllib.urlopen(url).read() or something along these lines and safe to a file the usual way.

Google图表API会生成PNG文件。只需使用urllib.urlopen(url).read()或这些行中的某些内容检索它们,并以通常的方式安全地访问文件。

Full example:

>>> import urllib
>>> url = 'http://chart.apis.google.com/chart?chxl=1:|0|10|100|1%2C000|10%2C000|100%2C000|1%2C000%2C000|2:||Excretion+in+Nanograms+per+gram+creatinine+milliliter+(logarithmic+scale)|&chxp=1,0|2,0&chxr=0,0,12.1|1,0,3&chxs=0,676767,13.5,0,lt,676767|1,676767,13.5,0,l,676767&chxtc=0,-1000&chxt=y,x,x&chbh=a,1,0&chs=640x465&cht=bvs&chco=A2C180&chds=0,12.1&chd=t:0,0,0,0,0,0,0,0,0,1,0,0,3,2,4,6,6,9,3,6,5,11,9,10,6,2,2,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0&chdl=n=87&chtt=William+MD+-+Buprenorphine+Graph'
>>> image = urllib.urlopen(url).read()
>>> outfile = open('chart01.png','wb')
>>> outfile.write(image)
>>> outfile.close()

As noted in other examples, 'urllib.urlretrieve(url, outfilename)` is even more straightforward, but playing with urllib and urllib2 will surely be instructive for you.

如其他示例中所述,'urllib.urlretrieve(url,outfilename)`更加直截了当,但使用urllib和urllib2肯定会对你有所帮助。