In this blog post, I’ll describe a new approach to palette image generation for products sold on Flipkart.
Palette images on Flipkart.com
Palette images are the small colored boxes that represent the color of a product. You can see these in small groups below the product image on a browse page or right of the product image on a product page that look like these
The shades of the color in these images are the colors from the actual product.
For verticals (product groups) like apparels, shoes, bags, etc. we need to inform the customer that alternate colors are available for the particular product. Palette images are an efficient way of delivering this information visually. Instead of reading an alternate product color as Fushcia and wonder what color it could be, the palette shows it as a shade of purple.
Given a image for a product on Flipkart, we should be able to generate the palette image for it automatically. This leads to a question – how was it being done and is automating the process even required?
So, how did it work so far?
It was being handled manually. We have a console where the product image is displayed and one has to select to a tiny section of the image that would act as a palette. We capture the coordinates of the selection, product ID and image url and and put this information in a queue. The queue consumer would then crop the required section from the image and process it.
The need for automation?
The effort of generating palette images manually increases linearly with the number of products, hence it’s not scalable. On a given day we have about 2500 different products being shot (which converts to around 4k images). Generating the palettes manually from the UI takes significant time.
To top that, we now get images from marketplace sellers. None of them have palette images and we need to generate the palettes for them as well.
Given that lifestyle products are seasonal these appear in short unexpected bursts, making planning for this activity difficult.
How can this be automated?
We brainstormed through several existing techniques and why they didn’t fit the bill.
- There are a quite a few algorithms which can be used to generate a color palette out of an image, most of them involve some form of color quantization. This paper describes one such way. What these would give you us is something like this .
All the quantization-based algorithms generated palettes of the entire image. As it turned out, the background color dominated the palettes in most cases. But, we are not interested in the palette of the entire image, only the product inside the image!
- That seemed like a solvable problem. Remove the background from the image and then use the technique from step 1. This too did not work out. Even if the background is removed, we are still left with an image that contains the model. Also a product image will usually have the model wearing some other kind of clothing to give a better sense of the product. Now, we not only have to recognise and remove the hair, eyes and skin tone from the image but also need to recognise the product from other products in the image! For example, the product here is a scarf. The background, hair, skin tone, earrings, t-shirt, jean, wrist-band all need to be recognised and discarded for us to even get to the relevant portion of the image!
- Even if we manage to select the scarf from some combination of AI and image processing, we had other problems.
The importance and increased scale of the problem made us take a step back and have a re-look. We eventually decided that this was a difficult problem to be solved just from image processing POV and we really did not need to do it that way.
Apart from the product image itself, is there any other information that can be leveraged? As it turns out, every product in the catalog has a color property that can be used. Given a product ID, the product catalog system’s API returns the colors of the product.
For example, a shirt can have the color property as red and blue. These properties by themselves alone are not very useful, however combined with the product images it can be very useful. The colors themselves cannot be directly used in the palette because the color red in various products will be of different different shades and the palettes need to show a shade which is in the product image.
The approach we settled on:
To start with, all the colors from the product image have equal probabilities of being the palette color of the product. Then, we get the color properties from the backend.
The probability of any color from the image being the palette color will slide up or down depending on the closeness of the color from the color property. The closer the colors are, higher the probability of it being the palette color.
What I’ll do here is list the steps along with the commands so that you can run the same commands and try the output. I’ve used imagemagick and im4java library for image processing and Google’s Catalano library for calculations.
- Color quantise an image to reduce the number of unique colors. Before and after color quantisation an image looks like this
There are various methods of color quantization and you can use any of them. For this example I’ve used the following command:
convert nonquantized.jpeg -colors 16 -depth 8 quantized.jpeg
- Generate a histogram of the image and find the RGB values of the top 10 colors from the image. We make two assumptions now
- the color(s) in the palette is amongst these top 10 colors.
- the top colors now have an equal probability of being the palette color.
The Command for this is:
convert quantized.jpeg -colors 16 -depth 8 -format “%c” histogram:info: |sort -nr |head -n8
and the output is :
- For the product, get the color properties of the image and convert those colors to their respective RGB values. For the above product, the color properties are grey and pink. The respective RGB values are #808080 and #ffc0cb.
- Now we need to find the closeness of each of the top 8 colors to the each of color property. Unfortunately, calculating the euclidean distance between two RGB colors does not correlate with human-perceived distance between two colors. We need to switch to a color space this is perceptually uniform. For our case, we’ve decided to use CIELAB color space and DeltaE formula (Refer to the first answer on this SO thread for more info ).
The below figures illustrate the difference between RGB color space vs CIE Lab color space (images)
- For each color in the color property, select the color from top 10 colors which has the least distance. That color has the highest probability of being the palette color.
- Combine all the least distance color for each of the color properties and generate a palette image out of it. Output palette for the above example
Disadvantages of this approach:
- Reliance on correctness of this approach : If the color properties stored in the backend are not accurate, then this solution goes for a toss.
- Each color property is mapped to a specific RGB value, and in some cases it might need to be manually updated (peacock, navy, skin, etc). Some of them cannot be mapped to RGB color (multicolor etc). But, this mapping is a one time process that seems good enough.
Advantages of this approach
- It is several magnitudes faster than the manual approach.
- The size of the palette image generated using this method is smaller than the one generated by cropping the image. This is because it does not have any color gradients and can be efficiently compressed. For this product, the left palette image is live on site and is manually cropped and the second is auto generated. The manually generated one is 529 bytes and the auto generated one is 301 bytes. This gives an average saving of almost 50% across all images.
- The auto generated palette is guaranteed to have 1:1 aspect ratio whereas for the manually generated one it depends on the cropping, ensuring pixel-level consistency across the site.
- It generates better palette image for products that have fine design all over the product like the below image.
- It generates better palettes for multicolored products, which otherwise would have needed to crop the image at the intersection of colors which can prove tricky like below
(observe carefully to see the top ⅓rd is white)
- Seasonal distribution of products does not impact it all. Even if there are 500K new products generated in a day, we do not have to wait for weeks for someone to sit and generate these images.
The feature is now live in production in a pilot phase and seems to work with significant efficiency. Questions welcome!