如何在Python中复制远程图像?

发布于 2024-08-04 17:29:41 字数 98 浏览 12 评论 0原文

我需要将远程图像(例如 http://example.com/image.jpg)复制到我的服务器。这可能吗?

你如何验证这确实是一张图像?

I need to copy a remote image (for example http://example.com/image.jpg) to my server. Is this possible?

How do you verify that this is indeed an image?

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

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

发布评论

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

评论(4

蓝戈者 2024-08-11 17:29:41

下载:

import urllib2
img = urllib2.urlopen("http://example.com/image.jpg").read()

要验证可以使用 PIL

import StringIO
from PIL import Image
try:
    im = Image.open(StringIO.StringIO(img))
    im.verify()
except Exception, e:
    # The image is not valid

如果您只是想验证这是一个即使图像数据无效:您可以使用imghdr

import imghdr
imghdr.what('ignore', img)

方法检查标题并确定图像类型。如果图像无法识别,它将返回 None。

To download:

import urllib2
img = urllib2.urlopen("http://example.com/image.jpg").read()

To verify can use PIL

import StringIO
from PIL import Image
try:
    im = Image.open(StringIO.StringIO(img))
    im.verify()
except Exception, e:
    # The image is not valid

If you just want to verify this is an image even if the image data is not valid: You can use imghdr

import imghdr
imghdr.what('ignore', img)

The method checks the headers and determines the image type. It will return None if the image was not identifiable.

豆芽 2024-08-11 17:29:41

下载东西

import urllib
url = "http://example.com/image.jpg"
fname = "image.jpg"
urllib.urlretrieve( url, fname )

可以通过多种方式验证它是否是图像。最难的检查是使用 Python 图像库打开文件并查看它是否抛出错误。

如果您想在下载前检查文件类型,请查看远程服务器提供的 mime 类型。

import urllib
url = "http://example.com/image.jpg"
fname = "image.jpg"
opener = urllib.urlopen( url )
if opener.headers.maintype == 'image':
    # you get the idea
    open( fname, 'wb').write( opener.read() )

Downloading stuff

import urllib
url = "http://example.com/image.jpg"
fname = "image.jpg"
urllib.urlretrieve( url, fname )

Verifying that it is a image can be done in many ways. The hardest check is opening the file with the Python Image Library and see if it throws an error.

If you want to check the file type before downloading, look at the mime-type the remote server gives.

import urllib
url = "http://example.com/image.jpg"
fname = "image.jpg"
opener = urllib.urlopen( url )
if opener.headers.maintype == 'image':
    # you get the idea
    open( fname, 'wb').write( opener.read() )
放飞的风筝 2024-08-11 17:29:41

使用 httplib2 同样的事情...

from PIL import Image
from StringIO import StringIO
from httplib2 import Http

# retrieve image
http = Http()
request, content = http.request('http://www.server.com/path/to/image.jpg')
im = Image.open(StringIO(content))

# is it valid?
try:
    im.verify()
except Exception:
    pass  # not valid

Same thing using httplib2...

from PIL import Image
from StringIO import StringIO
from httplib2 import Http

# retrieve image
http = Http()
request, content = http.request('http://www.server.com/path/to/image.jpg')
im = Image.open(StringIO(content))

# is it valid?
try:
    im.verify()
except Exception:
    pass  # not valid
白衬杉格子梦 2024-08-11 17:29:41

对于问题中有关复制远程图像的部分,这是受此答案:

import urllib2
import shutil

url = 'http://dummyimage.com/100' # returns a dynamically generated PNG
local_file_name = 'dummy100x100.png'

remote_file = urllib2.urlopen(url)
with open(local_file_name, 'wb') as local_file:
    shutil.copyfileobj(remote_file, local_file)

请注意,此方法适用于复制任何二进制媒体类型的远程文件。

For the portion of the question with respect to copying a remote image, here's an answer inspired by this answer:

import urllib2
import shutil

url = 'http://dummyimage.com/100' # returns a dynamically generated PNG
local_file_name = 'dummy100x100.png'

remote_file = urllib2.urlopen(url)
with open(local_file_name, 'wb') as local_file:
    shutil.copyfileobj(remote_file, local_file)

Note that this approach will work for copying a remote file of any binary media type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文