from google.colab import drive
drive.mount('/content/drive')
from PIL import Image
import numpy as np
import os
from scipy.spatial import distance
# filepathで指定した画像ファイルを読み込み、その画像のRGB値の平均をNumpyのArrayで【行ベクトルで】返す関数 ext_mean_rgb(filepath)
def ext_mean_rgb(filepath):
image = np.array(Image.open(filepath).convert('RGB')).reshape(-1,3)
return np.array([np.mean(image[:,0]),np.mean(image[:,1]),np.mean(image[:,2])])
# RGB値をnumpy array、qcolor=np.array([r,g,b])で与えられると[赤,橙,黄,緑,青,紫,ピンク,白,グレー,黒]の重みを成分とする10次元ベクトルが出力される関数gen_color_vec(qcolor)
def gen_color_vec(qcolor):
colorvec=np.array([])
palette=np.array(
[
[255,0,0], #赤
[255,102,0], #橙
[255,255,0], #黄
[0,128,0], #緑
[0,0,255], #青
[128,0,128], #紫
[255,0,255], #ピンク
[255,255,255], #白
[128,128,128], #グレー
[0,0,0] #黒
])
for col in palette:
colorvec=np.append(colorvec,distance.euclidean(col,qcolor))
colorvec=1-colorvec/np.linalg.norm(colorvec,np.inf)
return colorvec
filepath='/content/drive/My Drive/Colab Notebooks/Lec02img/pp.jpeg'
x=gen_color_vec(ext_mean_rgb(filepath)).reshape(-1,1)
red=["暑い","強い","危険","闘争","怖い","派手","情熱的","明るい","元気","興奮"]
orange=["暖かい","明るい","元気","友情","青春","希望","可愛い","楽しい","夏","希望"]
yellow=["明るい","元気","喜び","幸福","輝き","未来","優しい","夢","友情","危険","注意"]
green=["自然","安らぎ","落ち着き","安心","平和","生鮮","夏","若さ","成長","健康"]
blue=["冷たい","冷静","悲しみ","涼しい","寒い","青春","広大","夏","誠実","平和","自由"]
purple=["高貴","高級","大人っぽい","嫉妬","不安","セクシー","神秘的","和風","悪趣味","孤独"]
pink=["可愛い","優しい","幸せ","恋愛","甘い","柔らかい","心","春","夢","明るい","わがまま"]
white=["純粋","清潔","神聖","清楚","無","天使","平和","自由","潔白","きれい","雪","うさぎ","天国"]
gray=["不安","曖昧","暗い","悩み","迷い","控えめ","疑惑","不正","孤独","寂しい","悲しい"]
black=["恐怖","孤独","闇","強さ","絶望","夜","静寂","高級感","シック","かっこいい","重い"]
imp_colors=[red,orange,yellow,green,blue,purple,pink,white,gray,black]
colors=red+orange+yellow+green+blue+purple+pink+white+gray+black
#重複なしの印象語
impression=set(colors)
imp=[]
for n in imp_colors:
for i in impression:
if i in n:
imp.append(1)
else:
imp.append(0)
A=np.array(imp).reshape(10,88)
#印象を出力
color_impression=[]
y=np.dot(A.T,x)
for Y,c in zip(y,impression):
color_impression.append([Y,c])
pic_imp=sorted(color_impression,key=lambda x: x[0],reverse=True)
for _ in pic_imp:
print(_)
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme