Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compatibility with thumbor.filters.fill #24

Merged
merged 3 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/thumbor_video_engine/engines/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ def is_multiple(self):
def cleanup(self):
pass

def __getattribute__(self, attr):
# For compatibility with some thumbor filters, return the image_engine
# or video_engine class, when appropriate, for self.__class__
if attr == "__class__" and self.__dict__.get("engine"):
return self.__dict__["engine"].__class__
return object.__getattribute__(self, attr)

def __getattr__(self, attr):
if not self.__dict__.get('engine'):
raise AttributeError("'Engine' object has no attribute '%s'" % attr)
Expand Down
Binary file added tests/data/hotdog-transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 23 additions & 1 deletion tests/engines/test_video_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
from thumbor_video_engine.engines.ffmpeg import Engine as FFmpegEngine
from thumbor_video_engine.engines.video import Engine as VideoEngine

from tests.utils import color_diff, repr_rgb


def assert_colors_similar(rgb1, rgb2, message):
delta_e = color_diff(rgb1, rgb2)
assert delta_e < 0.05, "%s: %s != %s" % (message, repr_rgb(rgb1), repr_rgb(rgb2))


@pytest.fixture
def config(config):
config.FILTERS = ['thumbor_video_engine.filters.format']
config.FILTERS = ['thumbor_video_engine.filters.format', 'thumbor.filters.fill']
return config


Expand All @@ -39,6 +46,21 @@ def test_dispatch_to_image_engine(mocker, http_client, base_url):
assert BaseEngine.get_mimetype(response.body) == 'image/jpeg'


@pytest.mark.gen_test
def test_fill_filter(http_client, base_url):
response = yield http_client.fetch(
"%s/unsafe/filters:format(jpg):fill(ffff00,1)/hotdog-transparent.png" % base_url
)

assert response.code == 200
assert BaseEngine.get_mimetype(response.body) == "image/jpeg"

im = Image.open(BytesIO(response.body))

top_left_color = im.getpixel((0, 0))[:3]
assert_colors_similar(top_left_color, (255, 255, 0), "Fill not applied")


@pytest.mark.gen_test
def test_dispatch_non_animated_gif_to_gif_engine(mocker, config, http_client, base_url):
config.FFMPEG_USE_GIFSICLE_ENGINE = True
Expand Down
14 changes: 7 additions & 7 deletions tests/result_storages/test_file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from thumbor_video_engine.engines.video import Engine as VideoEngine
from thumbor_video_engine.engines.ffmpeg import Engine as FFMpegEngine


@pytest.fixture
Expand Down Expand Up @@ -55,12 +55,12 @@ def test_file_result_storage_retrieve(config, mocker, http_client, base_url, tmp
src_file,
"%s/%s/ba/68/88258f0b20357d15380b611a7b31da32f19b" % (tmp_path, subdir))

mocker.spy(VideoEngine, "load")
mocker.spy(FFMpegEngine, "load")

response = yield http_client.fetch("%s/unsafe/hotdog.gif" % base_url,
headers={'Accept': mime_type})
assert response.code == 200
assert VideoEngine.load.call_count == 0
assert FFMpegEngine.load.call_count == 0
assert response.headers.get('content-type') == mime_type
if auto_gif:
assert response.headers.get('vary') == 'Accept'
Expand All @@ -71,7 +71,7 @@ def test_file_result_storage_retrieve(config, mocker, http_client, base_url, tmp
"%s/unsafe/pbj-time.gif" % base_url,
headers={'Accept': mime_type})
assert response.code == 200
assert VideoEngine.load.call_count == 1
assert FFMpegEngine.load.call_count == 1


@pytest.mark.gen_test
Expand All @@ -92,15 +92,15 @@ def test_file_result_storage_legacy_retrieve(
src_file,
"%s/v2%s/un/sa/unsafe/hotdog.gif" % (tmp_path, subdir))

mocker.spy(VideoEngine, "load")
mocker.spy(FFMpegEngine, "load")

response = yield http_client.fetch("%s/unsafe/hotdog.gif" % base_url,
headers={'Accept': accept})
assert response.code == 200
assert VideoEngine.load.call_count == 0
assert FFMpegEngine.load.call_count == 0

response = yield http_client.fetch(
"%s/unsafe/pbj-time.gif" % base_url,
headers={'Accept': accept})
assert response.code == 200
assert VideoEngine.load.call_count == 1
assert FFMpegEngine.load.call_count == 1
6 changes: 3 additions & 3 deletions tests/result_storages/test_s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
asyncio = None

from thumbor.engines import BaseEngine
from thumbor_video_engine.engines.video import Engine as VideoEngine
from thumbor_video_engine.engines.ffmpeg import Engine as FFMpegEngine


# Subclassed Popen that gets around mirakuru not capturing stderr
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_s3_result_storage_load(mocker, config, http_client, base_url, auto_gif,
if mime_type == 'image/gif':
config.FFMPEG_GIF_AUTO_H264 = False

mocker.spy(VideoEngine, "load")
mocker.spy(FFMpegEngine, "load")

if not auto_gif and mime_type != 'image/png':
bucket_key = 'unsafe/hotdog.gif'
Expand Down Expand Up @@ -134,7 +134,7 @@ def test_s3_result_storage_load(mocker, config, http_client, base_url, auto_gif,
assert response.headers.get("vary") == "Accept"
else:
assert response.headers.get("vary") is None
assert VideoEngine.load.call_count == 0
assert FFMpegEngine.load.call_count == 0


@pytest.mark.skipif(Bucket is None, reason="tc_aws unavailable")
Expand Down
174 changes: 174 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# -*- coding: utf-8 -*-
import math


def esc_code(codes=None):
if codes is None:
# reset escape code
return "\x1b[0m"
if not isinstance(codes, (list, tuple)):
codes = [codes]
return "\x1b[0;" + ";".join(map(str, codes)) + "m"


def get_luminance(rgb):
rgb_map = []
for val in rgb:
val = val / 256
if val <= 0.03928:
rgb_map.append(val / 12.92)
else:
rgb_map.append(pow((val + 0.055) / 1.055, 2.4))

return (0.2126 * rgb_map[0]) + (0.7152 * rgb_map[1]) + (0.0722 * rgb_map[2])


def repr_rgb(rgb):
r, g, b = rgb
codes = (48, 2, r, g, b)
reset = "\x1b[0m"
hex_color = "#%s" % ("".join(["%02x" % c for c in rgb]))
luminance = get_luminance(rgb)
if luminance > 0.5:
codes += (38, 2, 0, 0, 0)
else:
codes += (38, 2, 255, 255, 255)

return "%(codes)s%(hex)s%(reset)s" % {
"codes": esc_code(codes),
"hex": hex_color,
"reset": reset,
}


def rgb_to_lab(input_color):
RGB = [0, 0, 0]

for i, value in enumerate(input_color):
value = float(value) / 255

if value > 0.04045:
value = ((value + 0.055) / 1.055) ** 2.4
else:
value = value / 12.92

RGB[i] = value * 100

XYZ = [0, 0, 0]

X = RGB[0] * 0.4124 + RGB[1] * 0.3576 + RGB[2] * 0.1805
Y = RGB[0] * 0.2126 + RGB[1] * 0.7152 + RGB[2] * 0.0722
Z = RGB[0] * 0.0193 + RGB[1] * 0.1192 + RGB[2] * 0.9504
XYZ = [round(n, 4) for n in [X, Y, Z]]

# Observer= 2°, Illuminant= D65
XYZ[0] = float(XYZ[0]) / 95.047 # ref_X = 95.047
XYZ[1] = float(XYZ[1]) / 100.0 # ref_Y = 100.000
XYZ[2] = float(XYZ[2]) / 108.883 # ref_Z = 108.883

for i, value in enumerate(XYZ):
if value > 0.008856:
value = value ** (0.3333333333333333)
else:
value = (7.787 * value) + (16 / 116)

XYZ[i] = value

Lab = [0, 0, 0]

L = (116 * XYZ[1]) - 16
a = 500 * (XYZ[0] - XYZ[1])
b = 200 * (XYZ[1] - XYZ[2])

Lab = [round(n, 4) for n in [L, a, b]]

return Lab


def avg(*args):
return float(sum(args)) / len(args)


def delta_e_cie2000(lab1, lab2, k_L=1.0, k_C=1.0, k_H=1.0):
L1, a1, b1 = lab1
L2, a2, b2 = lab2
pow25_7 = math.pow(25, 7)
C1 = math.sqrt(math.pow(a1, 2) + math.pow(b1, 2))
C2 = math.sqrt(math.pow(a2, 2) + math.pow(b2, 2))
C_avg = avg(C1, C2)
G = 0.5 * (1 - math.sqrt(math.pow(C_avg, 7) / (math.pow(C_avg, 7) + pow25_7)))
L1_ = L1
a1_ = (1 + G) * a1
b1_ = b1
L2_ = L2
a2_ = (1 + G) * a2
b2_ = b2
C1_ = math.sqrt(math.pow(a1_, 2) + math.pow(b1_, 2))
C2_ = math.sqrt(math.pow(a2_, 2) + math.pow(b2_, 2))
h1_ = (
0
if a1_ == 0 and b1_ == 0
else math.degrees(math.atan2(b1_, a1_)) + (0 if b1_ >= 0 else 360.0)
)
h2_ = (
0
if a2_ == 0 and b2_ == 0
else math.degrees(math.atan2(b2_, a2_)) + (0 if b2_ >= 0 else 360.0)
)
dh_cond = 1.0 if h2_ - h1_ > 180 else (2.0 if h2_ - h1_ < -180 else 0)
dh_ = (
h2_ - h1_
if dh_cond == 0
else (h2_ - h1_ - 360.0 if dh_cond == 1 else h2_ + 360.0 - h1_)
)
dL_ = L2_ - L1_
dC_ = C2_ - C1_
dH_ = 2 * math.sqrt(C1_ * C2_) * math.sin(math.radians(dh_ / 2.0))
L__avg = avg(L1_, L2_)
C__avg = avg(C1_, C2_)
h__avg_cond = (
3.0
if C1_ * C2_ == 0
else (0 if abs(h2_ - h1_) <= 180 else (1.0 if h2_ + h1_ < 360 else 2.0))
)
h__avg = (
h1_ + h2_
if h__avg_cond == 3
else (
avg(h1_, h2_)
if h__avg_cond == 0
else (avg(h1_, h2_) + 180.0 if h__avg_cond == 1 else avg(h1_, h2_) - 180.0)
)
)
AB = math.pow(L__avg - 50.0, 2) # (L'_ave-50)^2
S_L = 1 + 0.015 * AB / math.sqrt(20.0 + AB)
S_C = 1 + 0.045 * C__avg
T = (
1
- 0.17 * math.cos(math.radians(h__avg - 30.0))
+ 0.24 * math.cos(math.radians(2.0 * h__avg))
+ 0.32 * math.cos(math.radians(3.0 * h__avg + 6.0))
- 0.2 * math.cos(math.radians(4 * h__avg - 63.0))
)
S_H = 1 + 0.015 * C__avg * T
dTheta = 30.0 * math.exp(-1 * math.pow((h__avg - 275.0) / 25.0, 2))
R_C = 2.0 * math.sqrt(math.pow(C__avg, 7) / (math.pow(C__avg, 7) + pow25_7))
R_T = -math.sin(math.radians(2.0 * dTheta)) * R_C
AJ = dL_ / S_L / k_L # dL' / k_L / S_L
AK = dC_ / S_C / k_C # dC' / k_C / S_C
AL = dH_ / S_H / k_H # dH' / k_H / S_H
dE = math.sqrt(math.pow(AJ, 2) + math.pow(AK, 2) + math.pow(AL, 2) + R_T * AK * AL)

dE_norm = dE / 100.0
if dE_norm > 1:
return 1
elif dE_norm < 0:
return 0
else:
return dE_norm


def color_diff(rgb_a, rgb_b):
lab_a = rgb_to_lab(rgb_a)
lab_b = rgb_to_lab(rgb_b)
return delta_e_cie2000(lab_a, lab_b)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ deps =
py27: PyYAML==5.3.1
py27: moto[server] <= 2.1.0
py27: flask-cors<4
!py27: moto[server]
!py27: moto[server]<5
py37: boto3==1.21.21
py37: botocore==1.24.21

Expand Down
Loading