python - 批量下载带有标签的谷歌图片

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

我正在尝试找到一种有效且可复制的方式来从 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 ,并成功执行以获取图像链接,但正如上一篇文章中也提到的那样,它们与正常的谷歌图像结果并不完全一致。

最佳答案

尝试使用 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()

enter image description here

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

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

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

关于python - 批量下载带有标签的谷歌图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35242151/

相关文章:

Eclipse C++ : Format Source Files in Batch

python - 在 Ubuntu 上使用 PyGObject 在 python 中进行 Gtk 拼写检查

python - Pandas 按值排序,然后按索引排序

python - 如何从 Python 3 脚本创建独立的可执行文件?

javascript - 如何在 Onsen-UI 中使用自定义图像/图标

带有非 GUI 设备的 R dev.copy() 用于批处理图形脚本

python - Django Celery 任务成功但客户端等待

javascript - 将图像从 Base64 解码为 jpg

javascript - 如何在ajax调用后重新加载img attr "src"而不知道图像标签中的文件名?

mysql 防止两个线程选择相同的行