US07-03: Harden Media and Metadata Edge Cases (#85)
This commit was merged in pull request #85.
This commit is contained in:
@@ -16,7 +16,9 @@ from __future__ import annotations
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageOps
|
||||
from PIL import ImageOps
|
||||
|
||||
from photo_pipeline import imaging
|
||||
|
||||
PIXEL_HASH_VERSION = 1
|
||||
PHASH_VERSION = 1
|
||||
@@ -41,7 +43,9 @@ def _digest_file(path: Path | str, digest) -> str:
|
||||
|
||||
|
||||
def pixel_sha256(path: Path | str) -> str:
|
||||
with Image.open(path) as image:
|
||||
# Bounded decode: a header claiming a billion pixels is refused before it is
|
||||
# ever turned into memory (US07-03).
|
||||
with imaging.open_image(path) as image:
|
||||
oriented = ImageOps.exif_transpose(image)
|
||||
rgb = oriented.convert("RGB")
|
||||
header = f"{PIXEL_HASH_VERSION}:{rgb.width}x{rgb.height}:".encode()
|
||||
@@ -68,7 +72,9 @@ def phash(path: Path | str) -> str:
|
||||
import numpy as np
|
||||
from scipy.fftpack import dct
|
||||
|
||||
with Image.open(path) as image:
|
||||
from PIL import Image
|
||||
|
||||
with imaging.open_image(path) as image:
|
||||
small = image.convert("L").resize((32, 32), Image.LANCZOS)
|
||||
matrix = np.asarray(small, dtype=np.float64)
|
||||
transformed = dct(dct(matrix, axis=0), axis=1)
|
||||
|
||||
Reference in New Issue
Block a user