views:

732

answers:

2

I have a lot of images in a folder, and I would like to find images with a similar color to a pre chosen image.

I would like to be able to do something like:

python find_similar.py sample.jpg

and have that return something like:

234324.jpg
55.jpg
9945.jpg
345434.jpg
104.jpg

Is this doable?

A: 

The algorithm for finding similar images is discussed in a Question on Stackoverflow, you might want to implement one of those in Python & PIL.

Also, you can straightaway use the ImageChops module from PIL and use the difference method to compare two images like this:

import Image
import ImageChops

im1 = Image.open("original.jpg")
im2 = Image.open("sample.jpg")

diff = ImageChops.difference(im2, im1)

That might help you in getting some idea about the difference in your original image and the others.

There is another similar question on Stackoverflow which discusses this.

Baishampayan Ghose
The sample image is, say, a square, left half pink, right half black. Will this find a square image with left half black and right half pink?
ΤΖΩΤΖΙΟΥ
+4  A: 

I cannot give you a canned solution, but here's an angle to tackle the problem. It's not PIL-specific, and it might be entirely bogus, since I have no experience in image processing.

  1. Perform color quantization on the image. That gives you a palette that encodes the color information in the image without any shape information.

  2. Run a principal components analysis to get the dominant components in the color cube. Strictly, you could run this without quantization first, but it might be too expensive.

  3. Do a least-squares fitting on the principal components of different images.

Hope this helps.

ddaa