gpt4 book ai didi

python - 用python抓取网页的所有颜色

转载 作者:行者123 更新时间:2023-11-28 00:02:43 24 4
gpt4 key购买 nike

我想找到一种有效的方法来使用 python 从给定的页面 url 中提取某种调色板(列表或其他内容)。我想要的是采用所有背景颜色、标题颜色和所有其他元素。

我已经在这里看到了 [ Build a color palette from image URL ] 可以从图像中获取调色板,但是页面呢?

最佳答案

是否将 selenium 与上面的示例混合在一起。下面的示例展示了如何从 Google 的搜索中获取前十种颜色。

只需用网络爬虫截取网页,然后对图像进行处理

#!/bin/env python3
from selenium import webdriver
import numpy as np
from PIL import Image

def palette(img):
"""
Return palette in descending order of frequency
"""
arr = np.asarray(img)
palette, index = np.unique(asvoid(arr).ravel(), return_inverse=True)
palette = palette.view(arr.dtype).reshape(-1, arr.shape[-1])
count = np.bincount(index)
order = np.argsort(count)
return palette[order[::-1]]

def asvoid(arr):
"""View the array as dtype np.void (bytes)
This collapses ND-arrays to 1D-arrays, so you can perform 1D operations on them.
http://stackoverflow.com/a/16216866/190597 (Jaime)
http://stackoverflow.com/a/16840350/190597 (Jaime)
Warning:
>>> asvoid([-0.]) == asvoid([0.])
array([False], dtype=bool)
"""
arr = np.ascontiguousarray(arr)
return arr.view(np.dtype((np.void, arr.dtype.itemsize * arr.shape[-1])))


def savePrint(imageFile):
driver = webdriver.Firefox()
driver.get("https://google.com.br")
driver.get_screenshot_as_file(imageFile)

imageFile = '/tmp/tmp.png'
savePrint(imageFile)
img = Image.open(imageFile, 'r').convert('RGB')
print(palette(img)[:10])

关于python - 用python抓取网页的所有颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55910155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com