Skip to content

Commit e51366f

Browse files
committed
Warn when webp is asked to decode into grayscale
1 parent 904dad4 commit e51366f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

test/test_image.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,20 @@ def test_decode_webp(decode_fun, scripted):
934934
img += 123 # make sure image buffer wasn't freed by underlying decoding lib
935935

936936

937+
@pytest.mark.parametrize("decode_fun", (decode_webp, decode_image))
938+
def test_decode_webp_grayscale(decode_fun):
939+
encoded_bytes = read_file(next(get_images(FAKEDATA_DIR, ".webp")))
940+
941+
# Note that we warn at the C++ layer because dispatching for decode_image
942+
# doesn't happen until we hit C++. The C++ layer does not propagate
943+
# warnings up to Python, so we can't test for them.
944+
img = decode_fun(encoded_bytes, mode=ImageReadMode.GRAY)
945+
946+
# Note that because we do not support grayscale conversions, we expect
947+
# that the number of color channels is still 3.
948+
assert img.shape == (3, 100, 100)
949+
950+
937951
# This test is skipped by default because it requires webp images that we're not
938952
# including within the repo. The test images were downloaded manually from the
939953
# different pages of https://developers.google.com/speed/webp/gallery

torchvision/csrc/io/image/cpu/decode_webp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ torch::Tensor decode_webp(
3232
res == VP8_STATUS_OK, "WebPGetFeatures failed with error code ", res);
3333
TORCH_CHECK(
3434
!features.has_animation, "Animated webp files are not supported.");
35+
TORCH_WARN_ONCE(
36+
mode == IMAGE_READ_MODE_GRAY ||
37+
mode == IMAGE_READ_MODE_GRAY_ALPHA,
38+
"Webp does not support grayscale conversions. "
39+
"The returned tensor will be in the colorspace of the original image.");
3540

3641
auto return_rgb =
3742
should_this_return_rgb_or_rgba_let_me_know_in_the_comments_down_below_guys_see_you_in_the_next_video(

0 commit comments

Comments
 (0)