-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implements the Image Expressions instead of the todo marcros
- Loading branch information
Showing
7 changed files
with
127 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from __future__ import annotations | ||
|
||
import numpy as np | ||
from PIL import Image | ||
|
||
import daft | ||
from daft import col | ||
from daft.datatype import DataType | ||
from daft.series import Series | ||
from tests.cookbook.assets import ASSET_FOLDER | ||
|
||
|
||
def test_image_resize_mixed_modes(): | ||
rgba = np.ones((2, 2, 4), dtype=np.uint8) | ||
rgba[..., 1] = 2 | ||
rgba[..., 2] = 3 | ||
rgba[..., 3] = 4 | ||
|
||
data = [ | ||
rgba[..., :3], # rgb | ||
rgba, # RGBA | ||
np.arange(12, dtype=np.uint8).reshape((1, 4, 3)), # RGB | ||
np.arange(12, dtype=np.uint8).reshape((3, 4)) * 10, # L | ||
np.ones(24, dtype=np.uint8).reshape((3, 4, 2)) * 10, # LA | ||
None, | ||
] | ||
|
||
s = Series.from_pylist(data, pyobj="force") | ||
df = daft.from_pydict({"img": s}) | ||
|
||
target_dtype = DataType.image() | ||
df = df.select(df["img"].cast(target_dtype)) | ||
|
||
assert df.schema()["img"].dtype == target_dtype | ||
|
||
df = df.with_column("resized", df["img"].image.resize(5, 5)) | ||
|
||
assert df.schema()["resized"].dtype == target_dtype | ||
|
||
as_py = df.to_pydict()["resized"] | ||
|
||
first_resized = np.array(as_py[0]["data"]).reshape(5, 5, 3) | ||
assert np.all(first_resized[..., 0] == 1) | ||
assert np.all(first_resized[..., 1] == 2) | ||
assert np.all(first_resized[..., 2] == 3) | ||
|
||
second_resized = np.array(as_py[1]["data"]).reshape(5, 5, 4) | ||
assert np.all(second_resized[..., 0] == 1) | ||
assert np.all(second_resized[..., 1] == 2) | ||
assert np.all(second_resized[..., 2] == 3) | ||
assert np.all(second_resized[..., 3] == 4) | ||
|
||
for i in range(2, 4): | ||
resized_i = np.array(as_py[i]["data"]).reshape(5, 5, -1) | ||
resized_i_gt = np.asarray(Image.fromarray(data[i]).resize((5, 5), resample=Image.BILINEAR)).reshape(5, 5, -1) | ||
assert np.all(resized_i == resized_i_gt), f"{i} does not match" | ||
|
||
# LA sampling doesn't work for some reason in PIL | ||
resized_i = np.array(as_py[4]["data"]).reshape(5, 5, -1) | ||
assert np.all(resized_i == 10) | ||
|
||
assert as_py[-1] == None | ||
|
||
|
||
def test_image_decode() -> None: | ||
df = ( | ||
daft.from_glob_path(f"{ASSET_FOLDER}/images/**") | ||
.into_partitions(2) | ||
.with_column("image", col("path").url.download().image.decode().image.resize(10, 10)) | ||
) | ||
target_dtype = DataType.image() | ||
assert df.schema()["image"].dtype == target_dtype | ||
df.collect() |