ifixkart-backend/Backend/tests/test_blurhash_upload.py

45 lines
1.6 KiB
Python

import pytest
import io
from PIL import Image
import blurhash
import numpy as np
def create_dummy_image(format="JPEG", mode="RGB", size=(100, 100), color=(255, 0, 0)):
img = Image.new(mode, size, color)
buf = io.BytesIO()
img.save(buf, format=format)
return buf.getvalue()
def test_blurhash_encode_jpeg():
raw_bytes = create_dummy_image(format="JPEG", mode="RGB")
img = Image.open(io.BytesIO(raw_bytes)).convert("RGB")
thumb = img.resize((32, 32))
hash_str = blurhash.encode(np.asarray(thumb), 4, 3)
assert isinstance(hash_str, str)
assert len(hash_str) > 5
def test_blurhash_encode_png_rgba():
raw_bytes = create_dummy_image(format="PNG", mode="RGBA", color=(0, 255, 0, 128))
img = Image.open(io.BytesIO(raw_bytes)).convert("RGB")
thumb = img.resize((32, 32))
hash_str = blurhash.encode(np.asarray(thumb), 4, 3)
assert isinstance(hash_str, str)
assert len(hash_str) > 5
def test_blurhash_encode_webp():
raw_bytes = create_dummy_image(format="WEBP", mode="RGB", color=(0, 0, 255))
img = Image.open(io.BytesIO(raw_bytes)).convert("RGB")
thumb = img.resize((32, 32))
hash_str = blurhash.encode(np.asarray(thumb), 4, 3)
assert isinstance(hash_str, str)
assert len(hash_str) > 5
def test_non_image_blurhash_is_none():
non_image_bytes = b"%PDF-1.4 header dummy data"
b_hash = None
try:
img = Image.open(io.BytesIO(non_image_bytes)).convert("RGB")
b_hash = blurhash.encode(np.asarray(img.resize((32, 32))), 4, 3)
except Exception:
b_hash = None
assert b_hash is None