Email Multiple Images and Convert to Thumbnails

Resize mutiple cat images

Resizing images is a common task easily handled by many tools and utilities. We recently had the need to create thumbnails for a bunch of image files. However, we also wanted to give everyone on the team—regardless of technical skill level—access to the utility.

So, we set up a quick pipe to accept attachments from an email, use a little Python (PIL) to perform the conversion and then email the thumbnails back to the sender. This article shows how we built it.

Try the Pipe


Before we get into the build, let’s give the pipe a try. First, grab this image below (or take your own photo).

curious cat

Now, email it as an attachment to: thumbnail@pipes.flex.io. Within a few minutes, you’ll receive a new email in your inbox with the thumbnail of the image. Boom.

Build the Pipe: Email Image and Convert to a Thumbnail


OK, the build process is pretty simple – accept an email, convert the files and send back to the sender via email.

  1. Email input

    For the input, we’re just using a stdin, which means we can accept files from the command line or api or, in this case, an email attachment.

    input file:
    

    The email address for this pipe follows the pattern: pipealias@pipes.flex.io. You can add an alias at the top of the pipe builder.

  2. Python script for image processing

    Next, we’ll add a little script that will take our images and transform them to a thumbnail. We’ll add an execute step and click Save:

    execute lang: python
    

    A text editor will appear. We can then enter our Python script using the PIL library. The actual size of the resulting thumbnail can be adjusted by setting the basewidth variable.

    import PIL
    from PIL import Image
    from io import BytesIO
    
    def flexio_file_handler(input,output):
        output.content_type = input.content_type
    
        imagedata = input.read()
        file_imagedata = BytesIO(imagedata)
        img = Image.open(file_imagedata)
        imgformat = img.format
    
        basewidth = 100
        wpercent = (basewidth / float(img.size[0]))
        hsize = int((float(img.size[1]) * float(wpercent)))
        img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS)
    
        file_output = BytesIO()
        img.save(file_output, format = imgformat)
        file_output.seek(0)
        contents = file_output.getvalue()
        file_output.close()
    
        output.write(contents)
    
  3. Email thumbnail back to sender

    As a last step in the pipe, we’ll take the metadata from the original email and use it to send the attachment back to the sender. Here’s the command:

    email to: ${email-from} subject: Here is your thumbnail body: Hey there, ${email-from-display}, here is your thumbnail. data: attachment
    

    In this case, we’re using the following parameters:

    • ${email-from} - Sender’s email address from original email
    • ${email-from-display} - Sender’s name from original email

    That’s it. The sender will receive an email back that says something like “Hey there, John, here is your thumbnnail.” with the resized image attached.

Get Started


To use this pipe yourself, an easy way to get going is to use this email to thumbnail pipe template

Click on the button below to copy the pipe and modify it with your own data and commands.

Email to Resize Image Template