-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcolor_sorting.py
53 lines (47 loc) · 1.5 KB
/
color_sorting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# import the necessary packages
from imutils import build_montages
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
from selenium import webdriver
from PIL import Image
def image_colorfulness(imageP):
image = cv2.imread(imageP)
# split the image into its respective RGB components
(B, G, R) = cv2.split(image.astype("float"))
# compute rg = R - G
rg = np.absolute(R - G)
# compute yb = 0.5 * (R + G) - B
yb = np.absolute(0.5 * (R + G) - B)
# compute the mean and standard deviation of both `rg` and `yb`
(rbMean, rbStd) = (np.mean(rg), np.std(rg))
(ybMean, ybStd) = (np.mean(yb), np.std(yb))
# combine the mean and standard deviations
stdRoot = np.sqrt((rbStd ** 2) + (ybStd ** 2))
meanRoot = np.sqrt((rbMean ** 2) + (ybMean ** 2))
# derive the "colorfulness" metric and return it
return stdRoot + (0.3 * meanRoot)
def savePrint(imageFile, url):
driver = webdriver.Chrome()
driver.get(url)
driver.get_screenshot_as_file(imageFile)
# 1 - Not colorful
# 2 - Slightly colorful
# 3 - Moderately colorful
# 4 - Averagely colorful
# 5 - Quite colorful
# 6 - Highly colorful
# 7 - Extremely colorful
# \sigma_{rgyb} = \sqrt{\sigma_{rg}^2 + \sigma_{yb}^2}
# \mu_{rgyb} = \sqrt{\mu_{rg}^2 + \mu_{yb}^2}
# C = \sigma_{rgyb} + 0.3 * \mu_{rgyb}
# 0 (min) - 150 (max) (valorile metricii)
def get_colorcoeff(url):
'''
0 - 10
'''
img = "img.png"
savePrint(img, url)
return image_colorfulness(img) / 16.0