3

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?

2 Answers2

4

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
  • 52,890
  • 7
  • 50
  • 59
1

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.

Community
  • 1
  • 1
Baishampayan Ghose
  • 19,928
  • 10
  • 56
  • 60
  • 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? – tzot Feb 27 '09 at 09:54