圖片主色調分析

# -*- coding: utf-8 -*-

"""
    項目參考:http://blog.nycdatascience.com/student-works/using-python-and-k-means-to-find-the-colors-in-movie-posters/
"""
from bs4 import BeautifulSoup
import requests
import re
import os
import skimage.io
import skimage.transform
import pandas as pd
from sklearn.cluster import KMeans
import numpy as np

# 路徑申明
save_path = './images/'
kmeans_results_path = './kmeans_results'
# 主題色個數
n_main_color = 10
# 是否下載圖片
is_download = False


def download_posters(image_type):
    """
        下載電影海報並保存到本地
    """
    print('正在下載{}類型的電影海報...'.format(image_type))
    query = 'movie 2018 ' + image_type + ' poster'
    url = 'http://global.bing.com/images/search?q=' + query + '&FORM=HDRSC2'
    soup = BeautifulSoup(requests.get(url).text, 'lxml')
    img_src_list = [a['src'] for a in soup.find_all('img', {'src': re.compile('mm.bing.net')})]
    for i, img_src in enumerate(img_src_list):
        img_data = skimage.io.imread(img_src)
        if img_data is not None:
            save_img_name = image_type + '_' + str(i + 1) + '.jpg'
            skimage.io.imsave(os.path.join(save_path, save_img_name), img_data)
            print('已下載{}張'.format(i + 1))
        else:
            print('該圖像無效', img_src)
    print()


def proc_img(img_filename):
    """
        讀取海報,並運行K-Means找出10個主要顏色
    """
    img = skimage.io.imread(os.path.join(save_path, img_filename))
    # 調整圖片大小至 200 x 200
    resized_img = skimage.transform.resize(img, (200, 200))
    img_data = resized_img.reshape(-1, 3)
    kmeans = KMeans(n_clusters=n_main_color)
    kmeans.fit(img_data)
    centers = kmeans.cluster_centers_

    # 將每個像素值擴展到20x20x3的矩形框中,用於保存查看
    color_block_size = 20
    main_color_img = np.zeros((color_block_size * n_main_color, color_block_size, 3))
    for i, center in enumerate(centers):
        main_color_img[i * color_block_size: (i + 1) * color_block_size, :, :] = center
    skimage.io.imsave(os.path.join(kmeans_results_path, img_filename), main_color_img)

    # 保存數據到一行dataframe中
    kmeans_result_df = pd.DataFrame()
    kmeans_result_df['image name'] = [img_filename]
    kmeans_result_df['movie type'] = [img_filename.split('_')[0]]
    for i, center in enumerate(centers):
        rgb_val = skimage.img_as_ubyte(center)
        kmeans_result_df['color{}_R'.format(i + 1)] = [rgb_val[0]]
        kmeans_result_df['color{}_G'.format(i + 1)] = [rgb_val[1]]
        kmeans_result_df['color{}_B'.format(i + 1)] = [rgb_val[2]]
    return kmeans_result_df


def run_main():
    """
        主程序
    """
    # 爬取電影海報
    movie_types = ['horror', 'comedy', 'animation', 'action']

    if is_download:
        for movie_type in movie_types:
            download_posters(movie_type)

    # 讀取每張海報,並運行K-Means找出每張海報的10個主要顏色,並構建數據集
    img_filename_list = os.listdir(save_path)
    result_df = pd.DataFrame()
    for img_filename in img_filename_list:
        print('正在處理', img_filename)
        kmeans_result_df = proc_img(img_filename)
        result_df = result_df.append(kmeans_result_df, ignore_index=True)

    result_df.to_csv('./kmeans_results.csv', index=False)

if __name__ == '__main__':
    run_main()

爬取得圖片資源:

提取的每張圖片的主色調:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章