-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack_img.py
66 lines (62 loc) · 2.19 KB
/
pack_img.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
from bs4 import BeautifulSoup as BS
import base64
class ImgPacker(object):
def __init__(self, html: bytes) -> None:
self.bs = BS(str(html), "html.parser")
def pack(self) -> None:
img_all = self.bs.find_all("img")
for img in img_all:
img_url = str(img.attrs["src"])
img_type = img_url.split(".")[-1]
new_url = "data:image/"
if img_type == "apng":
new_url += "apng"
elif img_type == "avif":
new_url += "avif"
elif img_type == "gif":
new_url += "gif"
elif img_type in ["jpg", "jpeg", "jfif", "pjpeg", "pjp"]:
new_url += "jpeg"
elif img_type == "png":
new_url += "png"
elif img_type == "svg":
new_url += "svg+xml"
elif img_type == "webp":
new_url += "webp"
elif img_type == "bmp":
new_url += "bmp"
elif img_type in ["ico", "cur"]:
new_url += "x-icon"
elif img_type in ["tif", "tiff"]:
new_url += "tiff"
else:
raise TypeError("Invalid img file type")
new_url += ";base64,"
if img_url.startswith("/"):
img_url = img_url[1:]
f = open(img_url, "rb")
new_url += base64.b64encode(f.read()).decode("utf-8")
f.close()
img.attrs["src"] = new_url
def get(self) -> bytes:
return self.bs.prettify()
if __name__ == "__main__":
import os
for file in os.listdir("."):
if file.split(".")[-1] in ["htm", "html"]:
try:
try:
os.makedirs("output")
except FileExistsError:
pass
f = open(file, "r", encoding="utf-8")
packer = ImgPacker(f.read())
f.close()
packer.pack()
f = open("output/packed-"+file, "w", encoding="utf-8")
f.write(packer.get())
f.close()
except Exception as e:
print("err: "+file)
print(e)
#raise e