examples icon indicating copy to clipboard operation
examples copied to clipboard

Feat 4122 compress image python

Open rubynguyen1510 opened this issue 1 year ago • 1 comments

Feat 4122: Add compressImage function in Python

Checklist:

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

Closes https://github.com/appwrite/appwrite/issues/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

rubynguyen1510 avatar Jul 18 '23 22:07 rubynguyen1510

@rubynguyen1510

Mushmou avatar Jul 18 '23 22:07 Mushmou