fix
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import sys
|
import sys
|
||||||
@@ -155,10 +156,31 @@ def _best_image(data: bytes) -> Optional[Tuple[int, int]]:
|
|||||||
def _parse_args(argv):
|
def _parse_args(argv):
|
||||||
# Accept: [size, input, output] or [input, output]
|
# Accept: [size, input, output] or [input, output]
|
||||||
if len(argv) == 3:
|
if len(argv) == 3:
|
||||||
return argv[1], argv[2]
|
return None, argv[1], argv[2]
|
||||||
if len(argv) == 4:
|
if len(argv) == 4:
|
||||||
return argv[2], argv[3]
|
return argv[1], argv[2], argv[3]
|
||||||
return None, None
|
return None, None, None
|
||||||
|
|
||||||
|
|
||||||
|
def _write_preview(preview: bytes, out_path: str, size: Optional[int]) -> bool:
|
||||||
|
try:
|
||||||
|
from PIL import Image
|
||||||
|
except Exception:
|
||||||
|
Image = None
|
||||||
|
|
||||||
|
if Image is None:
|
||||||
|
if preview.startswith(b"\x89PNG\r\n\x1a\n") and size is None:
|
||||||
|
with open(out_path, "wb") as f:
|
||||||
|
f.write(preview)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
with Image.open(io.BytesIO(preview)) as im:
|
||||||
|
im.load()
|
||||||
|
if size:
|
||||||
|
im.thumbnail((size, size), Image.Resampling.LANCZOS)
|
||||||
|
im.save(out_path, format="PNG")
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
@@ -204,14 +226,17 @@ def main() -> int:
|
|||||||
exit_code = 1
|
exit_code = 1
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
with open(out_path, "wb") as f:
|
if not _write_preview(preview, out_path, None):
|
||||||
f.write(preview)
|
sys.stderr.write(
|
||||||
|
f"failed to write preview for {in_path} (missing Pillow or unsupported format)\n"
|
||||||
|
)
|
||||||
|
exit_code = 1
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
sys.stderr.write(f"failed to write {out_path}: {exc}\n")
|
sys.stderr.write(f"failed to write {out_path}: {exc}\n")
|
||||||
exit_code = 1
|
exit_code = 1
|
||||||
return exit_code
|
return exit_code
|
||||||
|
|
||||||
in_path, out_path = _parse_args(sys.argv)
|
size_str, in_path, out_path = _parse_args(sys.argv)
|
||||||
if not in_path or not out_path:
|
if not in_path or not out_path:
|
||||||
sys.stderr.write(
|
sys.stderr.write(
|
||||||
"usage: clip-thumbnailer [size] INPUT OUTPUT | clip-extract-preview INPUT OUTPUT | "
|
"usage: clip-thumbnailer [size] INPUT OUTPUT | clip-extract-preview INPUT OUTPUT | "
|
||||||
@@ -219,6 +244,13 @@ def main() -> int:
|
|||||||
)
|
)
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
|
size = None
|
||||||
|
if size_str:
|
||||||
|
try:
|
||||||
|
size = max(1, int(size_str))
|
||||||
|
except ValueError:
|
||||||
|
size = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(in_path, "rb") as f:
|
with open(in_path, "rb") as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
@@ -229,8 +261,9 @@ def main() -> int:
|
|||||||
preview = _extract_canvas_preview(data)
|
preview = _extract_canvas_preview(data)
|
||||||
if preview is not None:
|
if preview is not None:
|
||||||
try:
|
try:
|
||||||
with open(out_path, "wb") as f:
|
if not _write_preview(preview, out_path, size):
|
||||||
f.write(preview)
|
sys.stderr.write("failed to write preview (missing Pillow or unsupported format)\n")
|
||||||
|
return 1
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
sys.stderr.write(f"failed to write {out_path}: {exc}\n")
|
sys.stderr.write(f"failed to write {out_path}: {exc}\n")
|
||||||
return 1
|
return 1
|
||||||
@@ -243,8 +276,9 @@ def main() -> int:
|
|||||||
|
|
||||||
start, end = loc
|
start, end = loc
|
||||||
try:
|
try:
|
||||||
with open(out_path, "wb") as f:
|
if not _write_preview(data[start:end], out_path, size):
|
||||||
f.write(data[start:end])
|
sys.stderr.write("failed to write preview (missing Pillow or unsupported format)\n")
|
||||||
|
return 1
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
sys.stderr.write(f"failed to write {out_path}: {exc}\n")
|
sys.stderr.write(f"failed to write {out_path}: {exc}\n")
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
26
flake.lock
generated
Normal file
26
flake.lock
generated
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1773371044,
|
||||||
|
"narHash": "sha256-UioNXlVNmX+3QQ0ZjdZkZFq7ctHyObpcvdHm9aZV0/c=",
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "65feaa4cdcddbc1659abb8796810f29c4531aad8",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "NixOS",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
packages = forAllSystems (system:
|
packages = forAllSystems (system:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs { inherit system; };
|
pkgs = import nixpkgs { inherit system; };
|
||||||
|
pythonEnv = pkgs.python3.withPackages (ps: [ ps.pillow ]);
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
clip-thumbnailer = pkgs.stdenvNoCC.mkDerivation {
|
clip-thumbnailer = pkgs.stdenvNoCC.mkDerivation {
|
||||||
@@ -26,6 +27,8 @@
|
|||||||
runHook preInstall
|
runHook preInstall
|
||||||
|
|
||||||
install -Dm755 ${./clip-thumbnailer.py} $out/bin/clip-thumbnailer
|
install -Dm755 ${./clip-thumbnailer.py} $out/bin/clip-thumbnailer
|
||||||
|
substituteInPlace $out/bin/clip-thumbnailer --replace \
|
||||||
|
"#!/usr/bin/env python" "#!${pythonEnv}/bin/python3"
|
||||||
ln -s $out/bin/clip-thumbnailer $out/bin/clip-extract-preview
|
ln -s $out/bin/clip-thumbnailer $out/bin/clip-extract-preview
|
||||||
install -Dm644 ${./clip.thumbnailer} $out/share/thumbnailers/clip.thumbnailer
|
install -Dm644 ${./clip.thumbnailer} $out/share/thumbnailers/clip.thumbnailer
|
||||||
install -Dm644 ${./clip-studio.xml} $out/share/mime/packages/clip-studio.xml
|
install -Dm644 ${./clip-studio.xml} $out/share/mime/packages/clip-studio.xml
|
||||||
|
|||||||
Reference in New Issue
Block a user