2016-07-07 147 views
0

我使用Python 2.7尝试从网页截图中获取5色调色板。Python获取网页截图调色板

迄今为止我尝试过的方法不能产生令人满意的结果。

当网页有其他可能不占主导地位,但应具备重要色彩的调色板时,调色板会集中在绿色,灰色和蓝色上。

此处包含一个输出样本。我在每个图像缩略图上方放置了一个5格的表格,每个表格显示5种颜色之一。

enter image description here

我与注释掉方法的代码低于但我一直在使用Pillow并呼吁colorthief一个有前途的模块来概括。

我认为这些调色板的大部分练习都适用于场景和物体的照片或图形,这些场景和物体上充满了颜色。网页是不同的。他们有大量的空白和黑色文本。

最好的结果虽然远非令人满意,但却是一种将白色像素变为透明的方法。这允许一些截图显示比蓝色,灰色,绿色更多的调色板。

我怀疑如果我可以首先从屏幕截图中删除所有白色和黑色像素,并且可能从白色和黑色(例如关闭白色,深灰色)中删除相关%中的所有其他像素,然后我可以从中生成调色板只有一组颜色的像素。

网页搜索还没有透露任何具体处理网页或文档调色板生成的技术。

我可能不得不重新考虑调色板生成,并直接从HTML获取。但如果可能的话,想尝试使图像方法起作用。

所以问题是如何从排除白色,黑色和仅基于图像颜色的网页的屏幕截图获取调色板?

import os, os.path 
from PIL import Image 
import psycopg2 
from colorthief import ColorThief 

conn_string = \ 
    "host='localhost' \ 
    dbname='databasename' \ 
    user='username' \ 
    password='password'" 

conn = psycopg2.connect(conn_string)  
cur = conn.cursor() 

## dev paths 
screenshots_path = 'path/to/screenshots/' 

screenshots_dir = os.listdir(screenshots_path) 
for screenshot in screenshots_dir: 
    if screenshot != 'Thumbs.db': 

     try: 
      img_orig = Image.open(screenshots_path + screenshot) 

      ## method 1 replace white pixels with transparent 
      # img = img_orig.convert("RGBA") 
      # datas = img.getdata() 
      # newData = [] 
      # for item in datas: 
       # if item[0] == 255 and item[1] == 255 and item[2] == 255: 
        # newData.append((255, 255, 255, 0)) 
       # else: 
        # newData.append(item) 
      # img.putdata(newData) 

      ## method 2 - pillow 
      img = img_orig.convert('P', palette=Image.ADAPTIVE, colors=5) 
      width, height = img.size 
      height = img.size[1] 
      quantized = img.quantize(colors=5, kmeans=3) 
      palette = quantized.getpalette()[:15] 
      convert_rgb = quantized.convert('RGB') 
      colors = convert_rgb.getcolors(width*height) 
      color_str = str(sorted(colors, reverse=True)) 
      color_str = str([x[1] for x in colors]) 
      print screenshot + ' ' + color_str 


     ## method 3 - colorthief 
     # try: 
      # img = Image.open(screenshots_path + screenshot) 
      # color_thief = ColorThief(screenshots_path + screenshot) 
      ## get the dominant color 
      # dominant_color = color_thief.get_color(quality=1) 
      # build a color palette 
      # color_str = color_thief.get_palette(color_count=5) 
      # print screenshot + ' ' + str(height) + ' ' + str(color_str) 


      cur.execute("UPDATE screenshots set color_palette = %s, height = %s WHERE filename like %s", (str(color_str), height, '%' + screenshot + '%',)) 
      conn.commit() 

     except: 
      continue 

cur.close() 
conn.close() 

回答

0

我不确定你是否在数学上倾向于倾向,因为你可能想阅读finding dominant colors in an image的这篇教程。这个想法是使用图像颜色的统计数据来找出调色板。你从一个“主”颜色开始 - 整个图像的平均颜色。然后你把这个颜色分成两个部分,然后是三个部分。该代码可让您决定要提取多少种颜色。

这里有一个结果我得到了使用网站上提到的代码:

Finding dominant colors of a screenshot

+0

这种联系是好的,让我在寻找正确的方向。 opencv非常简单。发现另一个[博客文章](http://www.pyimagesearch.com/2014/05/26/opencv-python-k-means-color-clustering/),它具有类似于Python实现的功能,并且工作正常。 – curtisp

+1

顺便说一句,感兴趣的读者,我原来的解决方案实际上是工作。它产生的结果与Utkarsh&上面的pyimagesearch链接显示的结果类似。因为我在表格背景颜色中使用了错误的内联样式HTML引用(我在每个td中显示一种颜色),所以我得到了蓝/绿颜色。我使用'bgcolor =“rgb(232,147,31)”'而不是'style =“background-color:rgb(232,147,31)”'浏览器不喜欢'bgcolor'只在css中内联。但很高兴我挖入opencv。 – curtisp