This tool will fetch, re-process, and display images from the web. It uses Pillow for on the fly image manipulation. Run it locally or on a Google Cloud Function.
<localhost or Cloud Function>/<your-endpoint>?url=<external-image-url>&w=300&gs=1&fmt=png
Original Image: https://storage.googleapis.com/misc-shared-images-public/rita-pup.png (3.5 MB 1817 x 2837)
Example using ?url=https://storage.googleapis.com/misc-shared-images-public/rita-pup.png&w=300&bg=rgb(9,189,201)&fmt=jpeg&qlt=1
|
Resize (327 KB, 500 x 780)
|
Set Background Color (24 KB, 300 x 468)
|
Reduce File Size (2 KB, 300 x 468)
|
Parameters:
url: REQUIRED. url of image to ingest. eg.?url=https://site.io/image.pngw: width in pixels. Applying just width will auto scale height. eg.&w=300h: height in pixels. Applying just height will auto scale width. eg.&h=150fmt: outputs image format in jpeg, png, or webp only. Default is png. eg.&fmt=jpegqlt: for jpeg and webp, decreases the image quality and file size. Default is 90. eg.&qlt=10gs: boolean for grayscale. eg.&gs=1bg: to set transparency background color. eg.&bg=rgb(9,189,201)rotate: Pick from 90, 180, and 270. eg.&rotate=180apikey: Check incoming request for validation. eg&apikey=<api-key-you-create>
-
Image Processing Google Cloud Function (
image_processor_func/main.py):- Fetches an image from a given URL.
- Optionally resizes the image to specified dimensions.
- Optionally rotates the image.
- Optionally converts the image to grayscale.
- Optionally applies a background color, RGB.
- Optionally reduces quality of JPEGs and WEBPs.
- Returns a PNG, JPEG, or WEBP.
- The entry point for the function is
process_image_for_transformation.
-
Local Development Server (
local_dev.py):- A Flask application that allows local testing of the image processing logic found in
image_processor_func.main.process_image_for_transformation.
- A Flask application that allows local testing of the image processing logic found in
The project uses Conda for environment management. You can create and activate the environment using the environment.yml file:
conda env create -f environment.yml
conda activate cloud-image-transformIn root directory of this folder. Update the <values> for Cloud Run and apikey.
export CLOUD_IMAGE_API_KEY="<api-key-you-create>"
export GCF_FUNCTION_NAME_ENV_VAR="<your-google-cloud-function-name>"
export GCF_REGION_ENV_VAR="<GCP-region-you-picked>"apikey can be optional, just comment out the following in image_processor_func/main.py:
try:
api_key = environ.get("CLOUD_IMAGE_API_KEY")
provided_api_key = flask_request.args.get("apikey")
if not provided_api_key or provided_api_key != api_key:
return "Unauthorized: Access is denied due to an invalid API key.", 401
except Exception as e:
print(f"Error checking API key: {e}")
return "Authorization Failed: Access is denied.", 401This approach uses a header parameter for basic auth, alternatively, security could be achieved directly within GCP's IAM.
To test the image processing function locally, run the Flask development server:
python local_dev.py-
Deployment: The Google Cloud Function can be deployed using the deploy script:
chmod +x deploy.sh ./deploy.sh
-
Dependencies: Python dependencies for the cloud function are listed in
requirements.txt










