python - Django 检测图像的主色

标签 python django detection

我有一个包含很多文章的博客,我想知道如何构建一个函数来检测每篇文章图像的主色,并为每篇文章设置主色的背景。

(我正在使用 Django +1.8 和 Python 3.4.x)

我正在尝试从头开始构建它,步骤是什么?

颜色检测功能应该是什么样子?

有什么想法/建议吗?

最佳答案

Yeah I just wanted a schema of how it would work on django

让我们假设一个类似于下面的骨架

class Article(Model):
    background_color = CharField(max_length=6) # hex code of color

class AricleImage(Model):
    article = ForeignKey(Article)
    image = ImageField()

    def get_dominant_color(self):
         return _get_dominant_color(self.image.open())
         # or pass self.image.file, depending on your storage backend
         # We'll implement _get_dominant_color() below later

    def set_article_background_color(self):
         self.article.background_color = self.get_dominant_color()

Django 提供了 ImageField 它继承自 FileField ,它提供了 .open()方法,这就是我们上面用来获取图像文件句柄的方法(该句柄将传递给下面的 scipy/PIL 代码)

...to build a function that can detect the dominant color of each article image and for each article set the background with the dominant color.

要在所有文章上运行此操作,我们可以执行以下操作:

for article_image in ArticleImage.objects.all():
    article_image.set_article_background_color() 

让我们调整 this answer 中的代码并用它创建一个函数:

import struct                                                               
import Image                                                                
import scipy                                                                
import scipy.misc                                                           
import scipy.cluster                                                        

NUM_CLUSTERS = 5                                                            

def _get_dominant_color(image_file):                                       
    """                                                                     
    Take a file object and return the colour in hex code       
    """                                                                     

    im = image_file                                            
    im = im.resize((150, 150))      # optional, to reduce time              
    ar = scipy.misc.fromimage(im)                                           
    shape = ar.shape                                                        
    ar = ar.reshape(scipy.product(shape[:2]), shape[2])                     

    print 'finding clusters'                                                
    codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)                 
    print 'cluster centres:\n', codes                                       

    vecs, dist = scipy.cluster.vq.vq(ar, codes)         # assign codes      
    counts, bins = scipy.histogram(vecs, len(codes))    # count occurrences 

    index_max = scipy.argmax(counts)                    # find most frequent
    peak = codes[index_max]                                                 
    colour = ''.join(chr(c) for c in peak).encode('hex')                    
    return colour                                          

在模板中设置文章背景

最后但并非最不重要的一点是,当您渲染文章时,只需使用 {{article.background_color}}

例如如果你想覆盖通用 style.css你可以定义一个 <style></style> HTML 中的 block

<style>
body {
    background: #{{article.background_color}};
}
</style>

(仅举个例子,您还可以让 django 生成一个 /css/style-custom.css 文件以包含在主 /css/style.css 之后)

关于python - Django 检测图像的主色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34958777/

相关文章:

python - Pandas 数据框 : error joining

python - 如何在 Python/Django 中将此持续时间转换为天/小时/分钟/秒?

javascript - 捕获按键以过滤元素

javascript - 页面如何知道我正在用 Firebug 分析它

java - 2d 碰撞检测 - 尝试获取两个 Sprite 的所有非透明像素

python - python 退出并删除类实例

python - 如何使用 f2py 将字符串数组传递给 Fortran 子例程

java - 有多少数据可以存储到 Google App Engine 应用程序中?

python - 为什么我需要在 Django 的 TestCase 中使用辅助方法 create_user() ?

Pythonic/djangonic 以秒为单位处理用户超时的方式(如果需要的话,也可以以分钟为单位)