-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAK_compression.py
More file actions
61 lines (46 loc) · 1.83 KB
/
AK_compression.py
File metadata and controls
61 lines (46 loc) · 1.83 KB
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
54
55
56
57
58
59
60
61
import cv2
import numpy as np
from PIL import Image
ak_path = "/Users/Arush/Documents/GitHub/Image-Processing/ORIGINAL_IMAGES"
files = [
# '/Users/a970/Documents/CamCode/NAmericaEarth.jpeg',
# '/Users/a970/Documents/CamCode/BlueEarthTest.jpeg',
# '/Users/a970/Documents/CamCode/Polar(Clouds).jpeg'
'BlueEarthTest.jpeg',
'NAmericaEarth.jpeg',
'Polar(Clouds).jpeg'
]
def compress(folder, file, factor, img_quality):
img = Image.open(f"{folder}/{file}")
width, height = img.size
new_size = (width//factor, height//factor)
resized = img.resize(new_size)
new_name = "CMP" + file
resized.save(new_name, 'JPEG', optimize=True, quality=img_quality)
return new_name
def thumbnail(file, factor, img_quality):
img = Image.open(file)
width, height = img.size
img.thumbnail((width//factor, height//factor), Image.LANCZOS)
new_name = "THB" + file
img.save(new_name, 'JPEG', optimize=True, quality=img_quality)
return new_name
def analyze_color_range_score(filename):
img = cv2.imread(filename)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#npix = cv2.countNonZero(img_bw)
bestpixs = cv2.inRange(img_hsv, np.array([0, 0, 0]), np.array([150, 100, 100]))
rows, cols = img.shape[:2]
return cv2.countNonZero(bestpixs)/(rows*cols)*100
def analyze_black_score(filename):
img = cv2.imread(filename)
img_bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold = 30
_, img_bw = cv2.threshold(img_bw, threshold, 255, cv2.THRESH_BINARY_INV)
npix = cv2.countNonZero(img_bw)
rows, cols = img.shape[:2]
return 100 - (1 - npix/(rows*cols))*100
"""for file in files:
new_file = thumbnail(file, 10, 50)
print(f"Color range score for {new_file}:", (analyze_color_range_score(new_file)))
print(f"\t Negative Space: {analyze_black_score(new_file)}")"""