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

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.

Except there's four things you need to do before using this peace of code in your project:

  1. Setup your Google developers account and project
  2. Install dependencies
  3. Edit settings.py
  4. Define search parameters and image path

1. Setup your Google developers account and project

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

Create custom search engine (ID of the engine is used as "GOOGLE_API_CUSTOM_SEARCH_CX" in settings.py):
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")

2. Install dependencies

pip install Pillow
pip install requests
pip install python-resize-image
pip install google-api-python-client

3. Edit settings.py

Replace "__enter_your_api_key_here__" with your API key:

```python GOOGLE_API_DEVELOPER_KEY = '__enter_your_api_key_here__' ```

Replace "__enter_your_cx_here__" with your cx:

```python GOOGLE_API_CUSTOM_SEARCH_CX = '__enter_your_cx_here__' ```

Define path where your new image will be saved:

```python IMAGE_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'images', '%s') ```

Define image size:

```python IMAGE_SIZE = [260, 260] ```

4. Define search parameters and image path

In run.py replace "__my_search_query__" with desired search term, replace "__my_image__.jpg" with desired name of the image, and define other search parameters as you like.
You can find detailed description of search parameters in google_api.py

```python search_params = { 'q': '__my_search_query__', 'num': 5, 'safe': 'off', 'fileType': 'jpg', 'imgType': 'photo', 'imgSize': 'large', 'searchType': 'image', 'imgDominantColor': 'black' } path_to_image = settings.IMAGE_PATH % '__my_image__.jpg' ```