Skip to content

Commit 62bf0ce

Browse files
authored
Implement simple converter func, releaving dependency on skimage (#15)
* Implement simple converter func, releaving dependency on skimage * enable py39
1 parent 8e053e6 commit 62bf0ce

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jobs:
2424
- name: Linux py38
2525
os: ubuntu-latest
2626
pyversion: '3.8'
27-
# - name: Linux py39
28-
# os: ubuntu-latest
29-
# pyversion: '3.9'
27+
- name: Linux py39
28+
os: ubuntu-latest
29+
pyversion: '3.9'
3030
- name: MacOS py38
3131
os: macos-latest
3232
pyversion: '3.8'

dash_slicer/utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33

44
import numpy as np
55
import PIL.Image
6-
import skimage
6+
7+
8+
def img_as_ubyte(img):
9+
"""Quick-n-dirty conversion function.
10+
We'll have explicit contrast limits eventually.
11+
"""
12+
if img.dtype == np.uint8:
13+
return img
14+
else:
15+
img = img.astype(np.float32)
16+
mi, ma = img.min(), img.max()
17+
img = (img - mi) * (255 / (ma - mi)) + 0.5
18+
return img.astype(np.uint8)
719

820

921
def img_array_to_uri(img_array, new_size=None):
1022
"""Convert the given image (numpy array) into a base64-encoded PNG."""
11-
img_array = skimage.util.img_as_ubyte(img_array)
23+
img_array = img_as_ubyte(img_array)
1224
# todo: leverage this Plotly util once it becomes part of the public API (also drops the Pillow dependency)
1325
# from plotly.express._imshow import _array_to_b64str
1426
# return _array_to_b64str(img_array)

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ numpy
33
plotly
44
dash
55
dash_core_components
6-
scikit-image

tests/test_utils.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1-
from dash_slicer.utils import img_array_to_uri, get_thumbnail_size, shape3d_to_size2d
1+
from dash_slicer.utils import (
2+
img_as_ubyte,
3+
img_array_to_uri,
4+
get_thumbnail_size,
5+
shape3d_to_size2d,
6+
)
27

38
import numpy as np
49
from pytest import raises
510

611

12+
def test_img_as_ubyte():
13+
14+
im = np.zeros((100, 100), np.float32)
15+
im[0, 0] = 100
16+
17+
# Anything but uint8 is stretched to min-max
18+
im2 = img_as_ubyte(im)
19+
assert im2.dtype == np.uint8
20+
assert im2.min() == 0 and im2.max() == 255
21+
22+
# Uint- stays where it is
23+
im2[0, 0] = 100
24+
im3 = img_as_ubyte(im2)
25+
assert im3.dtype == np.uint8
26+
assert im3.min() == 0 and im3.max() == 100
27+
28+
729
def test_img_array_to_uri():
830

931
im = np.random.uniform(0, 255, (100, 100)).astype(np.uint8)

0 commit comments

Comments
 (0)