update @ Sun Dec 15 17:45:06 EET 2024

Signed-off-by: Ari Archer <ari@ari.lt>
This commit is contained in:
Arija A. 2024-12-15 17:45:06 +02:00
parent 002771908a
commit 3cc0e1acbe
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: A50D5B4B599AF8A2
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

View file

@ -10,6 +10,6 @@
"credit": "Ari Archer",
"ext": "jpeg",
"mime": "image/jpeg",
"uploaded": 1734256615.75018
"uploaded": 1734269521.436514
}
}

View file

@ -439,11 +439,11 @@ STATS_TEMPLATE: typing.Final[str] = (
)
if NCI:
import requests
import http.server
import magic
import pyfzf # type: ignore
import requests
from PIL import Image
else:
pyfzf: typing.Any = None
@ -1728,7 +1728,12 @@ def media(config: dict[str, typing.Any]) -> int:
else:
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:
json.dump(licenses, fp)
@ -1777,26 +1782,59 @@ def media(config: dict[str, typing.Any]) -> int:
fpath: str = f"media/{filename}"
if mime.startswith("image/"):
with Image.open(path) as img:
width, height = img.size
shutil.copy(path, fpath)
if ext in {"jpeg", "png"}:
quality_s: str = iinput("image quality % (1-100)", "100")
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
quality_s: str = iinput("image quality % (1-100)", "80")
try:
quality: int = int(quality_s)
quality = 100 if quality > 100 else quality
quality: typing.Union[str, int] = max(min(int(quality_s), 100), 1)
except Exception:
quality = 100
quality = 80
img.save(
img_new = Image.new(img.mode, img.size)
img_new.putdata(img.getdata())
img_new.save(
fpath,
format=ext,
quality=quality,
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.close()
else:
return err("Unkown media extension.")
MEDIA_INDEX[hash_hex] = {
"type": "image",