diff --git a/media/b97a4bd25804d1315833a59d353c7af1716c1c55a811f05dcd35460d73f79b84.jpeg b/media/b97a4bd25804d1315833a59d353c7af1716c1c55a811f05dcd35460d73f79b84.jpeg index 06b1ad0..dd040ec 100644 Binary files a/media/b97a4bd25804d1315833a59d353c7af1716c1c55a811f05dcd35460d73f79b84.jpeg and b/media/b97a4bd25804d1315833a59d353c7af1716c1c55a811f05dcd35460d73f79b84.jpeg differ diff --git a/media/media.json b/media/media.json index a09574f..a111a0b 100644 --- a/media/media.json +++ b/media/media.json @@ -10,6 +10,6 @@ "credit": "Ari Archer", "ext": "jpeg", "mime": "image/jpeg", - "uploaded": 1734256615.75018 + "uploaded": 1734269521.436514 } } \ No newline at end of file diff --git a/scripts/blog.py b/scripts/blog.py index 9e332ce..908b441 100755 --- a/scripts/blog.py +++ b/scripts/blog.py @@ -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)