-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover_resizer.py
137 lines (120 loc) · 5.74 KB
/
cover_resizer.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from pelican import signals
from pelican.contents import Content
from PIL import Image
from bs4 import BeautifulSoup as bs
import cssutils, os, re
from pelican import signals
from pelican.readers import BaseReader
def cover_resize(cover_pictures_rel):
quality_val = 60
script_dir = os.path.dirname(__file__)
print("1")
print(cover_pictures_rel)
print("2")
script_dir_parent_1 = os.path.dirname(script_dir)
print(script_dir_parent_1)
script_dir_parent_2 = os.path.dirname(script_dir_parent_1)
print(script_dir_parent_2)
cover_pictures_abs = os.path.join(script_dir_parent_2, cover_pictures_rel)
cover_pictures_abs_no_name = cover_pictures_abs.split("/")
article_path = cover_pictures_abs_no_name[-2]
print(article_path)
cover_pictures_abs_no_name.pop()
cover_pictures_abs_no_name = ('/').join(cover_pictures_abs_no_name)
print(cover_pictures_abs)
print(cover_pictures_abs_no_name)
img = Image.open(cover_pictures_abs)
img_width = img.size[0]
img_height = img.size[1]
img_ratio = img_width / float(img_height)
thumb_1x1_width = 680
thumb_1x1_height = 680
thumb_1x1_ratio = thumb_1x1_width / float(thumb_1x1_height)
thumb_4x3_width = 906
thumb_4x3_height = 680
thumb_4x3_ratio = thumb_4x3_width / float(thumb_4x3_height)
thumb_16x9_width = 1024
thumb_16x9_height = 576
thumb_16x9_ratio = thumb_16x9_width / float(thumb_16x9_height)
thumb_ratio_dict = {thumb_1x1_ratio : ["1x1", thumb_1x1_width, thumb_1x1_height],
thumb_4x3_ratio : ["4x3", thumb_4x3_width, thumb_4x3_height],
thumb_16x9_ratio : ["16x9", thumb_16x9_width, thumb_16x9_height]
}
for ratio in thumb_ratio_dict.items():
print(ratio)
print(ratio[0])
print(ratio[1])
if img_ratio > ratio[0]:
new_width = int(ratio[0] * img_height)
offset = (img_width - new_width) / 2
resize = (offset, 0, img_width - offset, img_height)
else:
new_height = int(img_width / ratio[0])
offset = (img_height - new_height) / 2
resize = (0, offset, img_width, img_height - offset)
thumb = img.crop(resize).resize((ratio[1][1], ratio[1][2]), Image.ANTIALIAS)
# thumb_name = cover_pictures_abs_no_name + "/" + "copertina_" + ratio[1][0] + ".jpg"
thumb_name_theme_folder = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/copertina_" + ratio[1][0] + ".jpg"
# thumb.save(thumb_name)
thumb.save(thumb_name_theme_folder)
new_name = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/copertina.jpg"
new_name_webp = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/copertina.webp"
thumb_name = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/thumb.jpg"
thumb_name_webp = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/thumb.webp"
X1_name = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/copertinaX1.jpg"
X1_name_webp = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/copertinaX1.webp"
X2_name = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/copertinaX2.jpg"
X2_name_webp = script_dir_parent_2 + "/" + "theme/dt/static/img/" + article_path + "/copertinaX2.webp"
# print(new_name_webp)
# os.popen('cp cover_pictures_rel new_name')
# print(cover_pictures_rel)
# os.popen('cwebp -q 80 cover_pictures_rel -o new_name_webp')
# img_webp = img.copy()
img.save(new_name_webp, 'webp', save_all=True, quality=quality_val)
img.save(new_name, quality=quality_val)
sizeX2 = 1400, 788
sizeX2_img = img.resize(sizeX2, Image.ANTIALIAS)
# img.thumbnail(sizeX2, Image.ANTIALIAS)
sizeX2_img.save(X2_name, "jpeg", optimize=True, quality=quality_val)
sizeX2_img.save(X2_name_webp, "webp", optimize=True, quality=quality_val)
# os.system('cwebp -q 80 cover_pictures_rel -o new_name_webp')
# os.rename(cover_pictures_rel, new_name)
sizeX1 = 700, 394
sizeX1_img = img.resize(sizeX1, Image.ANTIALIAS)
# img.thumbnail(sizeX1, Image.ANTIALIAS)
sizeX1_img.save(X1_name, "jpeg", optimize=True, quality=quality_val)
sizeX1_img.save(X1_name_webp, "webp", optimize=True, quality=quality_val)
# size = 200, 113
size = 350, 198
img.thumbnail(size, Image.ANTIALIAS)
img.save(thumb_name, quality=quality_val)
img.save(thumb_name_webp, "webp", quality=quality_val)
# Create a new reader class, inheriting from the pelican.reader.BaseReader
class NewReader(BaseReader):
enabled = True
# The list of file extensions you want this reader to match with.
# If multiple readers were to use the same extension, the latest will
# win (so the one you're defining here, most probably).
file_extensions = ['jpg']
# You need to have a read method, which takes a filename and returns
# some content and the associated metadata.
def read(self, filename):
print(filename)
#filename_encoded = filename.encode("utf-8")
#print(filename_encoded)
#filename_splitted = filename_encoded.split("/")
filename_splitted = filename.split("/")
filename_file = filename_splitted[-1]
filename_splitted.pop()
filepath = ("/").join(filename_splitted)
if filename_file == "copertina_raw.jpg":
cover_resize(filename)
new_name = filepath + "/copertina.jpg"
print(new_name)
os.rename(filename, new_name)
else:
print("problema")
def add_reader(readers):
readers.reader_classes['jpg'] = NewReader
def register():
signals.readers_init.connect(add_reader)