【问题标题】:Batch download google images with tags批量下载带有标签的谷歌图片
【发布时间】:2016-02-06 14:28:59
【问题描述】:

我正在尝试找到一种有效且可复制的方法来从 Google 图片搜索中批量下载全尺寸图片文件。其他人也问过类似的问题,但我没有找到任何我正在寻找或我理解的东西。

大多数是指已弃用的 Google Image Search API 或 Google Custom Search API,它们似乎不适用于整个网络,或者只是从单个 URL 下载图像。

我想这可能是一个两步过程:首先,从搜索中提取所有图像 URL,然后从中批量下载?

我应该补充一点,我是一个初学者(这可能很明显;抱歉)。因此,如果有人可以解释并指出我正确的方向,那将不胜感激。

我还研究了免费软件选项,但这些选项似乎也参差不齐。除非有人知道可靠的。

Download images from google image search (python)

In Python, is there a way I can download all/some the image files (e.g. JPG/PNG) from a **Google Images** search result?

如果有人知道这些标签的任何信息,以及它们是否存在于某处/与图像相关联? https://en.wikipedia.org/wiki/Google_Image_Labeler

import json
import os
import time
import requests
from PIL import Image
from StringIO import StringIO
from requests.exceptions import ConnectionError

def go(query, path):
"""Download full size images from Google image search.
Don't print or republish images without permission.
I used this to train a learning algorithm.
"""
BASE_URL = 'https://ajax.googleapis.com/ajax/services/search/images?'\
         'v=1.0&q=' + query + '&start=%d'

BASE_PATH = os.path.join(path, query)

 if not os.path.exists(BASE_PATH):
 os.makedirs(BASE_PATH)

start = 0 # Google's start query string parameter for pagination.
while start < 60: # Google will only return a max of 56 results.
r = requests.get(BASE_URL % start)
for image_info in json.loads(r.text)['responseData']['results']:
  url = image_info['unescapedUrl']
  try:
    image_r = requests.get(url)
  except ConnectionError, e:
    print 'could not download %s' % url
    continue

  # Remove file-system path characters from name.
  title = image_info['titleNoFormatting'].replace('/', '').replace('\\', '')

  file = open(os.path.join(BASE_PATH, '%s.jpg') % title, 'w')
  try:
    Image.open(StringIO(image_r.content)).save(file, 'JPEG')
  except IOError, e:
    # Throw away some gifs...blegh.
    print 'could not save %s' % url
    continue
  finally:
    file.close()

print start
start += 4 # 4 images per page.

# Be nice to Google and they'll be nice back :)
time.sleep(1.5)

# Example use
go('landscape', 'myDirectory')

更新

我能够使用here 指定的完整网络创建自定义搜索,并成功执行以获取图片链接,但正如上一篇文章中提到的,它们与正常的 Google 图片不完全一致结果。

【问题讨论】:

  • 这似乎是一个与 python 相关的问题,而不是关于批处理文件。我会为您更新标签,但我建议您阅读您使用的标签的信息页面。
  • 谢谢@DennisvanGils
  • 如果您正在寻找您自己的应用程序的结果与您的常规 Google 图片搜索不同的原因,那是因为 Google 会根据您的 cookie 等更改结果,而您的应用程序没有这些
  • @DennisvanGils 更多关于更新的旁注。但是谢谢,我想它是那样的。如前所述,我需要的主要内容是能够有效地从每个图像链接下载图像,并尽可能使用相关的 alt 标签。
  • 不确定您是否仍在尝试完成这项工作。但是,Google 不仅会关心您的 cookie,还会关心您的用户代理字符串。抓取 Google 也不是一件容易的事,因为他们认为这违反了他们的条款和条件,如果他们发现抓取内容会迅速阻止您。

标签: python image batch-processing google-custom-search google-image-search


【解决方案1】:

尝试使用 ImageSoup 模块。要安装它,只需:

pip install imagesoup

示例代码:

>>> from imagesoup import ImageSoup
>>>
>>> soup = ImageSoup()
>>> images_wanted = 50
>>> query = 'landscape'
>>> images = soup.search(query, n_images=50)

现在您有一个包含来自 Google 图片的 50 张风景图片的列表。让我们玩第一个:

>>> im = images[0]
>>> im.URL
https://static.pexels.com/photos/279315/pexels-photo-279315.jpeg
>>> im.size
(2600, 1300)
>>> im.mode
RGB
>>> im.dpi
(300, 300)
>>> im.color_count
493230
>>> # Let's check the main 4 colors in the image. We use
>>> # reduce_size = True to speed up the process.
>>> im.main_color(reduce_size=True, n=4))
[('black', 0.2244), ('darkslategrey', 0.1057), ('darkolivegreen', 0.0761), ('dodgerblue', 0.0531)]
# Let's take a look on our image
>>> im.show()

>>> # Nice image! Let's save it.
>>> im.to_file('landscape.jpg')

每次搜索返回的图像数量可能会发生变化。通常是小于 900 的数字。如果要获取所有图像,请设置 n_images=1000。

要贡献或报告错误,请查看 github 存储库:https://github.com/rafpyprog/ImageSoup

【讨论】:

  • 您能更正您的代码吗?它根本不起作用。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 2012-09-19
  • 2016-04-30
  • 2016-11-25
相关资源
最近更新 更多