Python 获取网页截图调色板

Python get web page screenshot color palette

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

到目前为止我尝试过的方法都没有产生令人满意的结果。

当网页有其他可能不占主导地位但在主题上很重要的颜色时,调色板会集中在绿色、灰色和蓝色上,这些颜色应该出现在调色板中。

此处包含输出示例。我在每个图像缩略图上方放置了 5 个单元格 table,每个单元格显示 5 种颜色中的一种。

我的方法注释掉的代码如下,但总而言之,我一直在使用 Pillow 和一个名为 colorthief.

的有前途的模块

我认为大多数这些调色板练习都适用于充满色彩的场景和物体的照片或图形。网页是不同的。他们有大量的白色 space 和黑色文本。

虽然远非令人满意,但最好的结果是一种将白色像素变为透明的方法。这允许一些屏幕截图展示的调色板不仅仅是蓝色、灰色、绿色。

我怀疑如果我可以先从屏幕截图中删除所有白色和黑色像素,并且可能在与白色和黑色相关的 % 内删除所有其他像素(例如灰白色、深灰色),那么我可以生成调色板来自仅具有颜色的像素集。

Web 搜索没有揭示任何专门处理网页或文档调色板生成的技术。

我可能不得不重新考虑调色板生成并直接从 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()

我不确定您是否对数学如此感兴趣,您可能想阅读 finding dominant colors in an image 上的本教程。这个想法是使用图像颜色的统计数据来计算调色板。您从单一 "main" 颜色开始 - 整个图像的平均颜色。然后你把这个颜色分成两个部分,然后是三个,依此类推。该代码可让您决定要提取多少种颜色。

这是我使用网站上提到的代码得到的结果: