2023-02-06 15:00:47 +01:00
|
|
|
import io
|
2023-02-07 14:00:19 +01:00
|
|
|
from pathlib import Path
|
2023-02-06 15:00:47 +01:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2023-05-07 10:58:55 +02:00
|
|
|
from lama_cleaner.helper import pil_to_bytes, load_img
|
2023-02-06 15:00:47 +01:00
|
|
|
|
2023-02-07 14:00:19 +01:00
|
|
|
current_dir = Path(__file__).parent.absolute().resolve()
|
|
|
|
|
|
|
|
|
2023-02-06 15:00:47 +01:00
|
|
|
def print_exif(exif):
|
|
|
|
for k, v in exif.items():
|
|
|
|
print(f"{k}: {v}")
|
|
|
|
|
|
|
|
|
2023-05-07 10:58:55 +02:00
|
|
|
def run_test(img_p: Path):
|
|
|
|
print(img_p)
|
|
|
|
ext = img_p.suffix.strip(".")
|
|
|
|
img_bytes = img_p.read_bytes()
|
|
|
|
np_img, _, exif_infos = load_img(img_bytes, False, True)
|
|
|
|
print(exif_infos)
|
|
|
|
print("Original exif_infos")
|
|
|
|
print_exif(exif_infos["exif"])
|
2023-02-06 15:00:47 +01:00
|
|
|
|
2023-05-07 10:58:55 +02:00
|
|
|
pil_to_bytes(Image.fromarray(np_img), ext=ext, exif_infos={})
|
2023-02-06 15:00:47 +01:00
|
|
|
|
2023-05-07 10:58:55 +02:00
|
|
|
pil_bytes = pil_to_bytes(Image.fromarray(np_img), ext=ext, exif_infos=exif_infos)
|
2023-02-06 15:00:47 +01:00
|
|
|
res_img = Image.open(io.BytesIO(pil_bytes))
|
2023-05-07 10:58:55 +02:00
|
|
|
print(f"Result img info: {res_img.info}")
|
2023-02-06 15:00:47 +01:00
|
|
|
res_exif = res_img.getexif()
|
2023-05-07 10:58:55 +02:00
|
|
|
print_exif(res_exif)
|
|
|
|
assert res_exif == exif_infos["exif"]
|
|
|
|
assert exif_infos["parameters"] == res_img.info.get("parameters")
|
2023-02-06 15:00:47 +01:00
|
|
|
|
|
|
|
|
2023-05-07 10:58:55 +02:00
|
|
|
def test_png():
|
|
|
|
run_test(current_dir / "image.png")
|
|
|
|
run_test(current_dir / "pnginfo_test.png")
|
2023-02-06 15:00:47 +01:00
|
|
|
|
|
|
|
|
2023-05-07 10:58:55 +02:00
|
|
|
def test_jpeg():
|
|
|
|
jpg_img_p = current_dir / "bunny.jpeg"
|
|
|
|
run_test(jpg_img_p)
|