This sample shows you how to blur an image using ImageMagick in a Storage-triggered Cloud Function.
View the source code.
-
Follow the Cloud Functions quickstart guide to setup Cloud Functions for your project.
-
Clone this repository:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git cd python-docs-samples/functions/imagemagick
-
Create a Cloud Storage Bucket:
gsutil mb gs://YOUR_INPUT_BUCKET_NAME
This storage bucket is used to upload images for the function to check.
-
Create a second Cloud Storage Bucket:
gsutil mb gs://YOUR_OUTPUT_BUCKET_NAME
This second storage bucket is used to store blurred images. (Un-blurred images will not be saved to this bucket.)
This is necessary because saving the blurred image to the input bucket would cause your function to be invoked a second time with the blurred image itself.
-
Deploy the
blur_offensive_images
function with a Storage trigger:gcloud functions deploy blur_offensive_images --trigger-bucket=YOUR_INPUT_BUCKET_NAME --set-env-vars BLURRED_BUCKET_NAME=YOUR_OUTPUT_BUCKET_NAME --runtime python37
- Replace
YOUR_INPUT_BUCKET_NAME
andYOUR_OUTPUT_BUCKET_NAME
with the names of the respective Cloud Storage Buckets you created earlier.
- Replace
-
Upload an offensive image to the Storage bucket, such as this image of a flesh-eating zombie: https://cdn.pixabay.com/photo/2015/09/21/14/24/zombie-949916_1280.jpg
-
Check the logs for the
blur_offensive_images
function:gcloud functions get-logs blur_offensive_images
You should see something like this in your console:
D ... User function triggered, starting execution I ... `The image zombie.jpg has been detected as inappropriate.` D ... Execution took 1 ms, user function completed successfully