V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Phishion
V2EX  ›  程序员

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

  •  
  •   Phishion · Mar 9, 2019 · 2094 views
    This topic created in 2610 days ago, the information mentioned may be changed or developed.

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

    4 replies    2019-03-10 10:59:49 +08:00
    hkitdog
        1
    hkitdog  
       Mar 9, 2019 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  
       Mar 9, 2019 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  
       Mar 9, 2019
    每个像素点都查一下,感觉好狠啊
    jdhao
        4
    jdhao  
       Mar 10, 2019 via Android
    这个需要聚类实现,事先设定要聚成几类,楼上计算 单通道均值不科学。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   809 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    ♥ Do have faith in what you're doing.