A Python Cloud Function for compressing images without losing quality using Tinypng API.
Example input:
{
"provider":"tinypng",
"image":"iVBORw0KGgoAAAANSUhEUgAAAaQAAALiCAY...QoH9hbkTPQAAAABJRU5ErkJggg=="
}
Example output:
{
"success":true,
"image":"iVBORw0KGgoAAAANSUhE...o6Ie+UAAAAASU5CYII="
}
{
"success":false,
"image":"iVBORw0KGgoAAAANSUhE...o6Ie+UAAAAASU5CYII="
}List of environment variables used by this cloud function:
- TINYPNG_API_KEY - Tinypng API Key
ℹ️ Create your TinyPNG API key at https://tinypng.com/developers
- Clone this repository, and enter this function folder:
git clone https://github.com/open-runtimes/examples.git && cd examples
cd python/compress-image- Enter this function folder and build the code:
docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/python:v2-3.10 sh /usr/local/src/build.shAs a result, a code.tar.gz file will be generated.
- Start the Open Runtime:
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=main.py --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/python:v2-3.10 sh /usr/local/src/start.shMake sure to replace
YOUR_API_KEYwith your key. Your function is now listening on port3000, and you can execute it by sendingPOSTrequest with appropriate authorization headers. To learn more about runtime, you can visit Python runtime README.
- Run the cURL function to send request.
curl http://localhost:3000/ -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" -d '{"payload":{"provider":"tinypng","image":"iVBORw0KGgoAAAANSUhEUgAAAaQAAALiCAY...QoH9hbkTPQAAAABJRU5ErkJggg=="}, "variables": {"API_KEY": "<YOUR_API_KEY>"}}'- This function is designed for use with Appwrite Cloud Functions. You can learn more about it in Appwrite docs.
- This example is compatible with Python 3.10. Other versions may work but are not guaranteed to work as they haven't been tested.