mirror of
https://git.ari.lt/ari.lt/blog.ari.lt.git
synced 2025-02-04 09:39:25 +01:00
update @ Sun Dec 15 17:45:06 EET 2024
Signed-off-by: Ari Archer <ari@ari.lt>
This commit is contained in:
parent
002771908a
commit
3cc0e1acbe
3 changed files with 61 additions and 23 deletions
Binary file not shown.
Before Width: | Height: | Size: 123 KiB After Width: | Height: | Size: 46 KiB |
|
@ -10,6 +10,6 @@
|
||||||
"credit": "Ari Archer",
|
"credit": "Ari Archer",
|
||||||
"ext": "jpeg",
|
"ext": "jpeg",
|
||||||
"mime": "image/jpeg",
|
"mime": "image/jpeg",
|
||||||
"uploaded": 1734256615.75018
|
"uploaded": 1734269521.436514
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -439,11 +439,11 @@ STATS_TEMPLATE: typing.Final[str] = (
|
||||||
)
|
)
|
||||||
|
|
||||||
if NCI:
|
if NCI:
|
||||||
import requests
|
|
||||||
import http.server
|
import http.server
|
||||||
|
|
||||||
import magic
|
import magic
|
||||||
import pyfzf # type: ignore
|
import pyfzf # type: ignore
|
||||||
|
import requests
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
else:
|
else:
|
||||||
pyfzf: typing.Any = None
|
pyfzf: typing.Any = None
|
||||||
|
@ -1728,7 +1728,12 @@ def media(config: dict[str, typing.Any]) -> int:
|
||||||
else:
|
else:
|
||||||
lnew("fetching SPDX license IDs")
|
lnew("fetching SPDX license IDs")
|
||||||
|
|
||||||
licenses = tuple(license["licenseId"] for license in requests.get("https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json").json()["licenses"])
|
licenses = tuple(
|
||||||
|
license["licenseId"]
|
||||||
|
for license in requests.get(
|
||||||
|
"https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json"
|
||||||
|
).json()["licenses"]
|
||||||
|
)
|
||||||
|
|
||||||
with open("licenses.json", "w") as fp:
|
with open("licenses.json", "w") as fp:
|
||||||
json.dump(licenses, fp)
|
json.dump(licenses, fp)
|
||||||
|
@ -1777,32 +1782,65 @@ def media(config: dict[str, typing.Any]) -> int:
|
||||||
fpath: str = f"media/{filename}"
|
fpath: str = f"media/{filename}"
|
||||||
|
|
||||||
if mime.startswith("image/"):
|
if mime.startswith("image/"):
|
||||||
with Image.open(path) as img:
|
shutil.copy(path, fpath)
|
||||||
|
|
||||||
|
if ext == "svg":
|
||||||
|
tree: etree.ElementTree = etree.parse(path)
|
||||||
|
root: etree.Element = tree.getroot()
|
||||||
|
|
||||||
|
w = root.attrib.get("width")
|
||||||
|
h = root.attrib.get("height")
|
||||||
|
|
||||||
|
if w is None or h is None:
|
||||||
|
view_box = root.attrib.get("viewBox")
|
||||||
|
|
||||||
|
if view_box:
|
||||||
|
view_box_values = view_box.split()
|
||||||
|
|
||||||
|
if len(view_box_values) >= 4:
|
||||||
|
w = view_box_values[2]
|
||||||
|
h = view_box_values[3]
|
||||||
|
|
||||||
|
if w is None or h is None:
|
||||||
|
width, height = 0, 0
|
||||||
|
else:
|
||||||
|
width = int(float(w.replace("px", "", 1)))
|
||||||
|
height = int(float(h.replace("px", "", 1)))
|
||||||
|
elif ext in {"jpeg", "png", "gif", "webp", "avif"}:
|
||||||
|
img = Image.open(path)
|
||||||
|
|
||||||
width, height = img.size
|
width, height = img.size
|
||||||
|
|
||||||
shutil.copy(path, fpath)
|
quality_s: str = iinput("image quality % (1-100)", "80")
|
||||||
|
|
||||||
if ext in {"jpeg", "png"}:
|
try:
|
||||||
quality_s: str = iinput("image quality % (1-100)", "100")
|
quality: typing.Union[str, int] = max(min(int(quality_s), 100), 1)
|
||||||
|
except Exception:
|
||||||
|
quality = 80
|
||||||
|
|
||||||
try:
|
img_new = Image.new(img.mode, img.size)
|
||||||
quality: int = int(quality_s)
|
img_new.putdata(img.getdata())
|
||||||
quality = 100 if quality > 100 else quality
|
img_new.save(
|
||||||
except Exception:
|
fpath,
|
||||||
quality = 100
|
format=ext,
|
||||||
|
quality=quality if ext in {"jpeg", "webp"} else None,
|
||||||
|
optimize=True,
|
||||||
|
lossless=yn("lossless", "n"),
|
||||||
|
subsampling=yn("subsampling"),
|
||||||
|
progressive=yn("progressive"),
|
||||||
|
dpi=img.info.get("dpi", (72, 72)),
|
||||||
|
)
|
||||||
|
img_new.close()
|
||||||
|
|
||||||
img.save(
|
img.close()
|
||||||
fpath,
|
else:
|
||||||
format=ext,
|
return err("Unkown media extension.")
|
||||||
quality=quality,
|
|
||||||
optimize=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
MEDIA_INDEX[hash_hex] = {
|
MEDIA_INDEX[hash_hex] = {
|
||||||
"type": "image",
|
"type": "image",
|
||||||
"width": width,
|
"width": width,
|
||||||
"height": height,
|
"height": height,
|
||||||
}
|
}
|
||||||
elif mime.startswith("audio/"):
|
elif mime.startswith("audio/"):
|
||||||
shutil.copy(path, fpath)
|
shutil.copy(path, fpath)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue