简体   繁体   English

如何使用Python和PIL查找具有相似颜色的图像?

[英]How do I find images with a similar color using Python and PIL?

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? 这可行吗?

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. 它不是特定于PIL的,它可能完全是假的,因为我没有图像处理经验。

  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. 希望这可以帮助。

The algorithm for finding similar images is discussed in a Question on Stackoverflow , you might want to implement one of those in Python & PIL. 查找类似图像的算法在Stackoverflow问题中讨论,您可能希望在Python和PIL中实现其中一个。

Also, you can straightaway use the ImageChops module from PIL and use the difference method to compare two images like this: 此外,您可以直接使用PIL中的ImageChops模块,并使用difference方法比较两个图像,如下所示:

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. Stackoverflow上还有另一个类似的问题 ,讨论了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM