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

Feat 4122 compress image python #160

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

Conversation

rubynguyen1510
Copy link

@rubynguyen1510 rubynguyen1510 commented Jul 18, 2023

Feat 4122: Add compressImage function in Python

Checklist:

  • Ask to be assigned to the issue.
  • Implement function compressImage() in Python/ folder.
  • Manually execute the newly implemented function to ensure it works.
  • Submit a pull request in open-runtimes/examples and include screenshots of successful execution.

Closes appwrite/appwrite#4122.

Authors: Noah Jacinto, Ngoc Nguyen.

Summary

This pull request creates a function that performs lossless compression on an image by integrating Tinypng and KrakenIO API. The function should receive a request containing a payload and environment variables. The payload should contain a provider and a base64 encoded image, while the variables contain the corresponding api key(s). After sending a request, the function will return a response with the compressed version of the image in base64 encoded format.

Details

  • README.md contains instructions on how to run the main function.

  • main.py contains functions to validate requests and perform image optimization through the TinyPNG/KrakenIO provider.

  • requirements.txt contains Python packages and dependencies used for our build.

  • secret.py contains API Key Variables for Tinypng and KrakenIO. It includes API_KEY_TINYPNG, API_KEY_KRAKENIO and SECRET_API_KEY_KRAKENIO. These values should be changed to your respective api keys.

  • test_main.py contains unittest for the main.py file with various scenarios like happy paths, value errors, unexpected exceptions etc. test_main.py uses 1kb.png, 1kb_result_encoded_krakenio.txt and 1kb_result_encoded_tinypng.txt inside the test folder.

Testing

More information about setting up the environment, refer toREADME.md.

  • Tinypng

TinyPNG Curl Example (Supports only API_KEY in Environment Variables)

curl http://localhost:3000/ -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" -d '{"payload": {"provider": "tinypng", "image": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdj+L+U4T8ABu8CpCYJ1DQAAAAASUVORK5CYII="}, "variables": {"API_KEY": "<YOUR_API_KEY>"}}

TinyPNG response.
image

  • KrakenIO

KrakenIO Curl Example (Supports API_KEY and SECRET_API_KEY in Environment Variables)

curl http://localhost:3000/ -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" -d '{"payload": {"provider": "krakenio", "image": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdj+L+U4T8ABu8CpCYJ1DQAAAAASUVORK5CYII="}, "variables": {"API_KEY": "<YOUR_API_KEY>", "SECRET_API_KEY": "<YOUR_SECRET_API_KEY>"}}'

KrakenIO response.
image


Test Results

Provider Before After
TinyPNG image image
KrakenIO image image

@Mushmou
Copy link

Mushmou commented Jul 18, 2023

@rubynguyen1510

@rubynguyen1510 rubynguyen1510 changed the title Feat 4122 compress image python # Feat 4122 compress image python Jul 18, 2023
@stnguyen90 stnguyen90 self-requested a review July 25, 2023 01:49
Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

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

Hey 👋 awesome work on your PR! We've approved your work and it'll be merged soon!

I did also leave some minor feedback.

Comment on lines 40 to 41
- **API_KEY** - Tinypng API Key or KrakenIO API Key
- **SECRET_API_KEY** - KrakenIO Secret API Key
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be 3 different variables for the different values so that a function can be deployed with all tinypng and krakenio variables and then executed with either tinypng or krakenio.

Copy link

Choose a reason for hiding this comment

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

Hello, we recently added a commit addressing the 3 variables for the different values. It should show now.

Comment on lines 114 to 117
IMPLEMENTATIONS = {
"krakenio": krakenio_impl,
"tinypng": tinypng_impl,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This approach to defining different implementations is a little odd. An object-oriented approach is more common.

Copy link
Author

Choose a reason for hiding this comment

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

We fixed this one.

return optimized_image


def tinypng_impl(variables):
Copy link
Contributor

Choose a reason for hiding this comment

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

methods are more commonly named as verbs.

Copy link
Author

Choose a reason for hiding this comment

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

We fixed this one.

@stnguyen90 stnguyen90 requested a review from Meldiron July 25, 2023 23:03
Mushmou and others added 2 commits July 27, 2023 10:17
Addressed stnguyen90 regarding 3 different variables for TinyPNG and KrakenIO.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

⚡ Write a compressImage() Function using Python
3 participants