slidedeckai.helpers.image_search#
Search photos using Pexels API.
Extracts the height and width from the URL parameters. |
|
Fetches an image from the specified URL and returns it as a BytesIO object. |
|
Return a randomly chosen photo from a Pexels search API response. |
|
Searches for images on Pexels using the provided query. |
Search photos using Pexels API.
- slidedeckai.helpers.image_search.extract_dimensions(url: str) tuple[int, int][source]#
Extracts the height and width from the URL parameters.
- Parameters:
url – The URL containing the image dimensions.
- Returns:
A tuple containing the width and height as integers.
- slidedeckai.helpers.image_search.get_image_from_url(url: str) BytesIO[source]#
Fetches an image from the specified URL and returns it as a BytesIO object.
This function sends a GET request to the provided URL, retrieves the image data, and wraps it in a BytesIO object, which can be used like a file.
- Parameters:
url – The URL of the image to be fetched.
- Returns:
A BytesIO object containing the image data.
- Raises:
requests.exceptions.RequestException – If the request to the URL fails.
- slidedeckai.helpers.image_search.get_photo_url_from_api_response(json_response: dict) tuple[str | None, str | None][source]#
Return a randomly chosen photo from a Pexels search API response. In addition, also return the original URL of the page on Pexels.
- Parameters:
json_response – The JSON response.
- Returns:
The selected photo URL and page URL or None. Empty tuple if no photos found or API key is not set.
- slidedeckai.helpers.image_search.search_pexels(query: str, size: Literal['small', 'medium', 'large'] = 'medium', per_page: int = 3) dict[source]#
Searches for images on Pexels using the provided query.
This function sends a GET request to the Pexels API with the specified search query and authorization header containing the API key. It returns the JSON response from the API.
[2024-08-31] Note: curl succeeds but API call via Python requests fail. Apparently, this could be due to Cloudflare (or others) blocking the requests, perhaps identifying as Web-scraping. So, changing the user-agent to Firefox. https://stackoverflow.com/a/74674276/147021 https://stackoverflow.com/a/51268523/147021 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox#linux
- Parameters:
query – The search query for finding images.
size – The size of the images: small, medium, or large.
per_page – No. of results to be displayed per page.
- Returns:
The JSON response from the Pexels API containing search results. Empty dict if API key is not set.
- Raises:
requests.exceptions.RequestException – If the request to the Pexels API fails.