Skip to content

Commit 01c9c99

Browse files
committed
Fix y/x being the wrong way around
1 parent 33d7417 commit 01c9c99

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

blurhash/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from .blurhash import blurhash_encode as encode
22
from .blurhash import blurhash_decode as decode
3+
from .blurhash import blurhash_components as components
4+
from .blurhash import srgb_to_linear as srgb_to_linear
5+
from .blurhash import linear_to_srgb as linear_to_srgb
36

4-
__all__ = ['encode', 'decode']
7+
__all__ = ['encode', 'decode', 'components', 'srgb_to_linear', 'linear_to_srgb']

blurhash/blurhash.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ def linear_to_srgb(value):
6060
return int(value * 12.92 * 255 + 0.5)
6161
return int((1.055 * math.pow(value, 1 / 2.4) - 0.055) * 255 + 0.5)
6262

63+
def blurhash_components(blurhash):
64+
"""
65+
Decodes and returns the number of x and y components in the given blurhash.
66+
"""
67+
if len(blurhash) < 6:
68+
raise ValueError("BlurHash must be at least 6 characters long.")
69+
70+
# Decode metadata
71+
size_info = base83_decode(blurhash[0])
72+
size_y = int(size_info / 9) + 1
73+
size_x = (size_info % 9) + 1
74+
75+
return size_x, size_y
76+
6377
def blurhash_decode(blurhash, width, height, punch = 1.0, linear = False):
6478
"""
6579
Decodes the given blurhash to an image of the specified size.
@@ -80,8 +94,8 @@ def blurhash_decode(blurhash, width, height, punch = 1.0, linear = False):
8094

8195
# Decode metadata
8296
size_info = base83_decode(blurhash[0])
83-
size_x = int(size_info / 9) + 1
84-
size_y = (size_info % 9) + 1
97+
size_y = int(size_info / 9) + 1
98+
size_x = (size_info % 9) + 1
8599

86100
quant_max_value = base83_decode(blurhash[1])
87101
real_max_value = (float(quant_max_value + 1) / 166.0) * punch

tests/test_blurhash.py

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ def test_decode():
1818
reference_image = np.load(os.path.join(base_path, "blurhash_out.npy"))
1919
assert np.sum(np.abs(image - reference_image)) < 1.0
2020

21+
def test_asymmetric():
22+
image = PIL.Image.open(os.path.join(base_path, "cool_cat.jpg"))
23+
blur_hash = blurhash.encode(np.array(image.convert("RGB")), components_x = 2, components_y = 8)
24+
assert blur_hash == "%BMOZfK1BBNG2skqs9n4?HvgJ.Nav}J-$%sm"
25+
26+
decoded_image = blurhash.decode(blur_hash, 32, 32)
27+
assert np.sum(np.var(decoded_image, axis = 0)) > np.sum(np.var(decoded_image, axis = 1))
28+
29+
blur_hash = blurhash.encode(np.array(image.convert("RGB")), components_x = 8, components_y = 2)
30+
decoded_image = blurhash.decode(blur_hash, 32, 32)
31+
assert np.sum(np.var(decoded_image, axis = 0)) < np.sum(np.var(decoded_image, axis = 1))
32+
33+
def test_components():
34+
image = PIL.Image.open(os.path.join(base_path, "cool_cat.jpg"))
35+
blur_hash = blurhash.encode(np.array(image.convert("RGB")), components_x = 8, components_y = 3)
36+
size_x, size_y = blurhash.components(blur_hash)
37+
assert size_x == 8
38+
assert size_y == 3
39+
2140
def test_linear_dc_only():
2241
image = PIL.Image.open(os.path.join(base_path, "cool_cat.jpg"))
2342
linearish_image = np.array(image.convert("RGB")) / 255.0

0 commit comments

Comments
 (0)