Goal: I would like a user to be able to upload an image (ideally multiple) that we can compress or resize before sending to Salesforce, as we need to make the images smaller for speed and storage's sake.
I have found a few older posts that lead me to believe there is no way to do this in Retool, but I wanted to confirm on a fresh post.
This post's code doesn't work for me, it's several years old and seems to be outdated now.
This post seems to cover a resize feature but it's for mobile and my app is on web.
I tried importing a JS library to help with compression but it requires a File or Blob object which seems to not be allowed in Retool based on this post.
I would probably subscribe to an API service that I could send images to, receive them back processed the way I want and then store them. Here is a list of some.
Hi there @eslinz . While I agree with @Shawn_Crocker that outsourcing this to a purpose-built API is a good way to go, you should also be able to achieve this with a Workflow
Here's a GIF:
And the code to get you started:
import io
import uuid
import base64
import requests
from PIL import Image
# Helper function to output base64 encoded string of image
def image_to_base64_string(image_path):
# Open the image using Pillow
with Image.open(image_path) as img:
# Create a BytesIO object to hold the image data
buffered = io.BytesIO()
# Save the image to the BytesIO object in the desired format (e.g., JPEG)
img.save(buffered, format="JPEG") # Change format if needed
# Get the byte data from the BytesIO object
img_byte_data = buffered.getvalue()
# Encode the byte data to base64
base64_encoded_data = base64.b64encode(img_byte_data).decode('utf-8')
return base64_encoded_data
# Generate unique id
generated_uuid = uuid.uuid4()
original_filename = f"{generated_uuid}_orig.jpg"
resized_filename = f"{generated_uuid}_resized.jpg"
# Download image
url = startTrigger.data.imageUrl # Replace with actual URL
response = requests.get(url)
img = Image.open(io.BytesIO(response.content))
img_orig = img.save(original_filename,"JPEG")
# Resize image
img_resized = img.resize((120, 80))
# Save resized image to disk
img_resized.save(resized_filename, "JPEG", quality=75)
return {
'resized': {
'b64string':image_to_base64_string(resized_filename),
'filename': resized_filename
},
'original': {
'b64string':image_to_base64_string(original_filename),
'filename': original_filename
}
}