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:
- Setup your Google developers account and project
- Install dependencies
- Edit settings.py
- Define search parameters and image path
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")
pip install Pillow
pip install requests
pip install python-resize-image
pip install google-api-python-client
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] ```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