Performance bottleneck is saving images

I am watermarking JPGs with PNGs and then displaying them to users. I’m using a combination of PIL and OpenCV to create the watermark and then applying the watermark to the JPG. I was surprised to find that it took 1.5s from start to finish. I assumed that the bottleneck would be the graphics operations, but after timing every single line, I found that saving the watermarked JPG to SSD was the biggest culprit - 0.7s to 1s to save about 700kb images. I tried saving the image to memory in a BytesIO object, but that takes longer (1-1.5s).

  1. Can I upgrade to a faster I/O machine? Would that help?
  2. I am currently on a Shared 4 GB Memory / 2 AMD vCPUs / 50 GB Disk + 100 GB. Would upgrading to a dedicated machine help with either disk I/O or writing to a BytesIO object?
  3. Any other optimizations I can do? I find it hard to believe that 700kb saves take so long.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
June 15, 2023

Hi there,

Indeed, you can upgrade to a Storage-Optimized Droplet which use NVMe (non-volatile memory express), which is an interface protocol explicitly built for modern SSDs:

You can try creating a storage-optimized Droplet and deploying your application there as a test, rather than upgrading your existing Droplet directly. That way you will be able to verify if this improves the speed.

Also, another thing to keep in mind is that when you save an image using OpenCV or PIL, they have to encode the pixel data in the JPEG format. This is a CPU-bound operation, which means a faster CPU will perform it quicker. However, there are also libraries specifically designed for fast JPEG encoding/decoding, such as libjpeg-turbo, that you could potentially use:

Another thing you could try is to compress your images, as a smaller file size means less data to write to the disk. If image quality isn’t a top priority, you can consider lowering the quality level or changing the image resolution before saving.

Hope that this helps!



Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel