Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main.py - MIME Type & Metadata #13279

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

paulleroyza
Copy link
Contributor

@paulleroyza paulleroyza commented Apr 1, 2025

Function has been re-written to:

  • Preserve Source MIMEtype
  • Add Metadata to indicate source and wether blurred or not
  • Blur radius increaed to 160 (16 did nothing to hide image)

Description

Fixes #

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

Function has been re-written to:
- Preserve Source MIMEtype
- Add Metadata to indicate source and wether blurred or not
- Blur radius increaed to 160 (16 did nothing to hide image)
@paulleroyza paulleroyza requested review from a team as code owners April 1, 2025 16:16
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @paulleroyza, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request updates the main.py file in the functions/imagemagick directory to improve image blurring functionality. The changes include increasing the blur radius, preserving the source MIME type, and adding metadata to the blurred image indicating its source and blur status. The primary goal is to enhance the image blurring process and provide more context about the blurred images.

Highlights

  • Blur Radius Increase: The blur radius has been increased from 16 to 160 to achieve a more significant blurring effect.
  • MIME Type Preservation: The MIME type of the source image is now preserved when creating the blurred image.
  • Metadata Addition: Metadata is added to the blurred image to indicate that it has been blurred and to store the name of the original file.

Changelog

  • functions/imagemagick/main.py
    • Increased the blur radius from 16 to 160 on line 73.
    • Added code to preserve the MIME type of the source blob on line 86.
    • Added code to include metadata indicating the image is blurred and the original filename on lines 89-92.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A pixel's plight,
A blur to hide from sight,
Metadata's guide.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request updates the image blurring function to preserve the source MIME type and add metadata indicating the source and whether the image was blurred. The blur radius has also been increased. Overall, the changes seem reasonable and address the issue mentioned in the description.

Summary of Findings

  • Missing Unit Tests: The pull request modifies the __blur_image function to include setting the content type and metadata. However, there are no corresponding unit tests to verify this new functionality. It's important to add tests to ensure that the MIME type and metadata are correctly set when blurring an image.

Merge Readiness

The changes introduced in this pull request seem reasonable and address the intended functionality. However, the lack of unit tests for the new MIME type and metadata functionality is a concern. I recommend adding these tests before merging. I am unable to directly approve this pull request, and other reviewers should review and approve this code before merging.

@product-auto-label product-auto-label bot added samples Issues that are directly related to samples. api: cloudfunctions Issues related to the Cloud Run functions API. labels Apr 1, 2025
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
davidcavazos
davidcavazos previously approved these changes Apr 2, 2025
new_blob.content_type = current_blob.content_type

# Add custom metadata.
# Add custom metadata.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate comment?

@davidcavazos davidcavazos requested review from davidcavazos and removed request for davidcavazos April 2, 2025 23:01
@davidcavazos davidcavazos dismissed their stale review April 2, 2025 23:03

Changes are not reflected on other implementations

Copy link
Contributor

@glasnt glasnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've identified an issue with the sample: the blur does not work correctly. However there are multiple other changes that are being made in this PR that require clarification.

Comment on lines +90 to +96
import datetime
new_blob.metadata = {
"blurred": "true",
"original_file": file_name,
"blurred_at": datetime.datetime.utcnow().isoformat(),
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why you're adding this functionality?

For context: This is one of multiple implementations of the same functionality across multiple programming languages. We try and keep all implementations functionality the same, so adding this needs to be considered in the context of other samples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. When I teach Data Engineering: I use this as an event driven processing example, and using the metadata to store the data load stage and discuss that the metadata is held in Spanner
  2. When I teach Application Development: I use metadata as an example to define when images are safe to display in the app

If this was production the code would need to always create the file in the target bucket and optionally blur it based on the ML result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample is designed to demonstrate Cloud Run functions, the Cloud Vision API, and ImageMagick to detect and blur offensive images that get uploaded to a Cloud Storage bucket. It is not designed for your classroom. You are free to reuse the code for your own requirements as long as you adhere to the open source license attached.

@@ -70,7 +70,7 @@ def __blur_image(current_blob):

# Blur the image using ImageMagick.
with Image(filename=temp_local_filename) as image:
image.resize(*image.size, blur=16, filter="hamming")
image.resize(*image.size, blur=160, filter="hamming")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct here that the blur value doesn't work. There's a different change that's required here to correct this, as it looks like the underlying library (Wand) has changed since this sample was implemented.

Context: other samples use 0x8 for the value, which in the CLI maps to a radius and sigma value, which was first implemented in Wand 0.4.5. This sample was written originally using 0.4.4

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the current version of Wand, this is the equivalent implementation to a -blur 0x8 from other samples that use the command line:

Suggested change
image.resize(*image.size, blur=160, filter="hamming")
image.blur(radius=0, sigma=16)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The blur does work, but it is not strong enough to hide the underlying picture.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggested updates the blur method to match the other implementations of this sample that call the CLI directly.

@iennae iennae assigned glasnt and unassigned kweinmeister Apr 4, 2025
Comment on lines +84 to +95

# Copy mimetype from the source blob.
new_blob.content_type = current_blob.content_type

# Add custom metadata.
# Add custom metadata.
import datetime
new_blob.metadata = {
"blurred": "true",
"original_file": file_name,
"blurred_at": datetime.datetime.utcnow().isoformat(),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Copy mimetype from the source blob.
new_blob.content_type = current_blob.content_type
# Add custom metadata.
# Add custom metadata.
import datetime
new_blob.metadata = {
"blurred": "true",
"original_file": file_name,
"blurred_at": datetime.datetime.utcnow().isoformat(),
}

If you would like a version of your PR to be accepted (as you did correctly identify the blur issue and should get credit in resolving it), this additional functionality will have to be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just use my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants