V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Phishion
V2EX  ›  程序员

获取图片主题色如何用 Python 实现?

  •  
  •   Phishion · 2019-03-09 23:30:19 +08:00 · 1755 次点击
    这是一个创建于 1890 天前的主题,其中的信息可能已经有所发展或是发生改变。

    就是比如一张蓝天白云的照片,程序跑下来输出一个蓝色的颜色值给我,各位大佬能不能给一个现成的轮子啊?最好不要依赖太多安装包

    4 条回复    2019-03-10 10:59:49 +08:00
    hkitdog
        1
    hkitdog  
       2019-03-09 23:36:45 +08:00 via iPhone
    用 php 做過,Python 應該差不多
    <?php

    $image=imagecreatefromjpeg('image.jpg');
    $thumb=imagecreatetruecolor(1,1); imagecopyresampled($thumb,$image,0,0,0,0,1,1,imagesx($image),imagesy($image));
    $mainColor=strtoupper(dechex(imagecolorat($thumb,0,0)));
    echo $mainColor;

    ?>
    hkitdog
        2
    hkitdog  
       2019-03-09 23:45:28 +08:00 via iPhone
    from PIL import Image

    def compute_color(img):
    width, height = img.size

    r_total = 0
    g_total = 0
    b_total = 0

    count = 0
    for x in range(0, width):
    for y in range(0, height):
    r, g, b = img.getpixel((x,y))
    r_total += r
    g_total += g
    b_total += b
    count += 1

    return (r_total/count, g_total/count, b_total/count)

    img = Image.open('image.png')
    img_color = compute_color(img)
    print(img_color)
    #試了沒問題
    mamahaha
        3
    mamahaha  
       2019-03-09 23:53:33 +08:00
    每个像素点都查一下,感觉好狠啊
    jdhao
        4
    jdhao  
       2019-03-10 10:59:49 +08:00 via Android
    这个需要聚类实现,事先设定要聚成几类,楼上计算 单通道均值不科学。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1241 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:16 · PVG 02:16 · LAX 11:16 · JFK 14:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.