At Tangocode, we’re always looking for smart approaches to solve problems for our clients. These approaches include techniques from areas such as Artificial Intelligence and Image Processing. One place where these tactics truly come in handy is when we work with images that need to be altered to meet specific criteria.
Throughout digital marketing, we can find examples where the displayed images have some restrictions in terms of size and quality. Size is important because digital marketing platforms such as Facebook and Google Ads do not accept small photos.
Quality is essential because blurry and pixelated images are not the best way to market a product. In our digital marketing products, a user can send ad information with its representations to mass-produce marketing campaigns based on that information.
In that case, the system tries to use those images, but if they do not comply with the requirements of the advertisement platform, the ads might not be approved. Another common problem in this situation is image size. To help our clients resolve these pain points, we added a couple of steps that aim to address these issues and help teams avoid the painful task of asking the user to verify and reupload thousands of images manually.
Step One: Resize Images
The first step is to resize small images. Let’s consider resizing those images to a minimum of 500px width and height to meet this criterion. We tested an algorithm that can get an image URL as an input and resize it larger when needed.
The implementation was completed in Python 3.6 using the library OpenCV (Open Computer Vision), which allows us to resize the image using different parameters (or zooming an image, in this case).
The recommended method for resizing is INTER_CUBIC – a bicubic interpolation over a 4×4 pixel neighborhood. All the images tested were resized successfully to a width of 500px (they were between 150x150px to 300x300px).
Step Two: Assess Quality
The second step is to determine the quality of the resized images. To do this, we started with images with an original size of 150×150 pixels and larger. This is because if the original image is smaller than that, the results after resizing to 500px width (required for Instagram) will be invariably poor, so any work performed will be a wasted effort.
To measure the quality of images is a challenge because it is a highly subjective task that depends on the observer. Fortunately, some measures try to represent this human perception.
There are two types of approaches: reference-based evaluation, which depends on a high-quality image to compare and evaluate the photos, and no-reference evaluation, which does not require other images to assess image quality.
For determining the quality of the resized images, we use a no-reference method called Blind/Referenceless Image Spatial Quality Evaluator (BRISQUE). This is an algorithm that takes an image as input and retrieves a quality score, which then helps determine the quality of the image. We tested an implementation on Python to calculate this score.
Step Three: Generate a Score
Using the BRISQUE method, we can get the score of an image and use it to determine its quality. If the image is low-quality (too blurry or pixelated, for example), the score retrieved will be high, which means that the image is of poor quality.
If that happens, we have the opportunity to either omit those images and not use them in the marketing campaigns or send a message to the users.
We implemented these two algorithms, with their respective dependencies, in Python 3.6 over Docker containers to manage the running environments. These containers were deployed in Amazon Elastic Container Service (Amazon ECS) with AWS Fargate (https://aws.amazon.com/ecs), which uses serverless computing for containers. With this, we have an implementation for our services that is scalable, reliable, and secure.
For testing purposes, we used a set of 1000 images that were smaller than 500 pixels in width and height. We saved the resulting images in an AWS S3 bucket and kept the URLs of the resized images with their scores in a CSV file.
After watching the output images and their ratings, a human expert defined the threshold for the quality of the images as a score of 40 for digital marketing advertisement (this is the maximum value acceptable on a scale of 0 to 100, with scores near 100 being poor quality).
To validate the process, we repeated it with another set of 2000 images, each of which were resized and classified by the process. From the 830 images that pass the threshold as usable images, the human expert approved all but one.
From the discarded set of images, only one could have been used for advertisement. This means the algorithm has a precision rate of greater than 97%, which is well within the acceptable margin set by the product owner.