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",
|
||||
"ext": "jpeg",
|
||||
"mime": "image/jpeg",
|
||||
"uploaded": 1734256615.75018
|
||||
"uploaded": 1734269521.436514
|
||||
}
|
||||
}
|
|
@ -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,32 +1782,65 @@ def media(config: dict[str, typing.Any]) -> int:
|
|||
fpath: str = f"media/{filename}"
|
||||
|
||||
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
|
||||
|
||||
shutil.copy(path, fpath)
|
||||
quality_s: str = iinput("image quality % (1-100)", "80")
|
||||
|
||||
if ext in {"jpeg", "png"}:
|
||||
quality_s: str = iinput("image quality % (1-100)", "100")
|
||||
try:
|
||||
quality: typing.Union[str, int] = max(min(int(quality_s), 100), 1)
|
||||
except Exception:
|
||||
quality = 80
|
||||
|
||||
try:
|
||||
quality: int = int(quality_s)
|
||||
quality = 100 if quality > 100 else quality
|
||||
except Exception:
|
||||
quality = 100
|
||||
img_new = Image.new(img.mode, img.size)
|
||||
img_new.putdata(img.getdata())
|
||||
img_new.save(
|
||||
fpath,
|
||||
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(
|
||||
fpath,
|
||||
format=ext,
|
||||
quality=quality,
|
||||
optimize=True,
|
||||
)
|
||||
img.close()
|
||||
else:
|
||||
return err("Unkown media extension.")
|
||||
|
||||
MEDIA_INDEX[hash_hex] = {
|
||||
"type": "image",
|
||||
"width": width,
|
||||
"height": height,
|
||||
}
|
||||
MEDIA_INDEX[hash_hex] = {
|
||||
"type": "image",
|
||||
"width": width,
|
||||
"height": height,
|
||||
}
|
||||
elif mime.startswith("audio/"):
|
||||
shutil.copy(path, fpath)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue