Skip to main content

Search for image using Google Custom Search API and resize & crop the image afterwords

Project description

Google Images Search

PyPI version Build Status GitHub GitHub last commit

Ok, here's the thing, you want to fetch one image from Google Images and you want to resize it and crop it from the middle This code enables you to do that.

Before you continue you need to setup your Google developers account and project:

https://console.developers.google.com (Among all of the Google APIs enable "Custom Search API" for your project)

https://cse.google.com/cse/all (In the web form where you create/edit your custom search engine enable "Image search" option and and for "Sites to search" option select "Search the entire web but emphasize included sites")

After setting up you Google developers account and project you should have your developers API key and project CX

Installation

> pip install Google-Images-Search

CLI usage

# without environment variables:

> gimages -k __your_dev_api_key__ -c __your_project_cx__ search -q puppies
# with environment variables:

> export GCS_DEVELOPER_KEY=__your_dev_api_key__
> export GCS_CX=__your_project_cx__
>
> gimages search -q puppies
# search only (no download and resize):

> gimages search -q puppies
# search and download only (no resize):

> gimages search -q puppies -d /path/on/your/drive/where/images/should/be/downloaded
# search, download and resize:

> gimages search -q puppies -d /path/ -w 500 -h 500

Programmatic usage

from google_images_search import GoogleImagesSearch

# if you don't enter api key and cx, the package will try to search
# them from environment variables GCS_DEVELOPER_KEY and GCS_CX
gis = GoogleImagesSearch('your_dev_api_key', 'your_project_cx')

# example: GoogleImagesSearch('ABcDeFGhiJKLmnopqweRty5asdfghGfdSaS4abC', '012345678987654321012:abcde_fghij')

#define search params:
_search_params = {
    'q': '...',
    'num': 1-50,
    'safe': 'high|medium|off',
    'fileType': 'jpg|gif|png',
    'imgType': 'clipart|face|lineart|news|photo',
    'imgSize': 'huge|icon|large|medium|small|xlarge|xxlarge',
    'searchType': 'image',
    'imgDominantColor': 'black|blue|brown|gray|green|pink|purple|teal|white|yellow'
}

# this will only search for images:
gis.search(_search_params=_search_params)

# this will search and download:
gis.search(_search_params=_search_params, path_to_dir='/path/')

# this will search, download and resize:
gis.search(_search_params=_search_params, path_to_dir='/path/', width=500, height=500)

# search first, then download and resize afterwards
gis.search(_search_params=_search_params)
for image in gis.results():
    image.download('/path/')
    image.resize(500, 500)

Saving to a BytesIO object

from google_images_search import GoogleImagesSearch
from io import BytesIO
from PIL import Image

# in this case we're using PIL to keep the BytesIO as an image object
# this way we don't have to wait for disk save / write times
# the image is simply kept in memory
# this example should display 3 pictures of puppies!

gis = GoogleImagesSearch('your_dev_api_key', 'your_project_cx')

my_bytes_io = BytesIO()

gis.search({'q': 'puppies', 'num': 3})
for image in gis.results():
    # here we tell the BytesIO object to go back to address 0
    my_bytes_io.seek(0)

    # take raw image data
    raw_image_data = image.get_raw_data()

    # this function writes the raw image data to the object
    image.copy_to(my_bytes_io, raw_image_data)

    # or without the raw data which will be automatically taken
    # inside the copy_to() method
    image.copy_to(my_bytes_io)

    # we go back to address 0 again so PIL can read it from start to finish
    my_bytes_io.seek(0)

    # create a temporary image object
    temp_img = Image.open(my_bytes_io)

    # show it in the default system photo viewer
    temp_img.show()

Changelog

0.3.3

Fixed:

  • Travis CI definition for PyPi upload.

0.3.2

Fixed:

  • API call default parameter changed from specific to blank.

0.3.1

Added:

  • Class docstrings.

0.3.0

Added:

  • Tests added.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Google Images Search-0.3.3.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Google_Images_Search-0.3.3-py2.py3-none-any.whl (8.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file Google Images Search-0.3.3.tar.gz.

File metadata

  • Download URL: Google Images Search-0.3.3.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for Google Images Search-0.3.3.tar.gz
Algorithm Hash digest
SHA256 de4a6a6b67d5d225bb1816abdd76fec123ce8ffdd9ad1e2ca0451696136ca41e
MD5 ea8ea2bab554cc2afa0f6eeb865695a4
BLAKE2b-256 657ac040c654a332350e77dae2eacc9f92cbade49f3298e2bbfcda3a23667cd3

See more details on using hashes here.

File details

Details for the file Google_Images_Search-0.3.3-py2.py3-none-any.whl.

File metadata

  • Download URL: Google_Images_Search-0.3.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for Google_Images_Search-0.3.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 77be7c545051f0779dab13d3a1970933d9a7ea4de919c34982611ee8658e2215
MD5 fd2b57fbec342998a984dd291e220a27
BLAKE2b-256 836dd4a367fdda6d79c45f7f59d88900ab8bbda5cedcc60f5bb0d89e14895b7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page