|
1 | 1 | import concurrent.futures
|
| 2 | +import contextlib |
2 | 3 | import glob
|
3 | 4 | import io
|
4 | 5 | import os
|
@@ -934,6 +935,32 @@ def test_decode_webp(decode_fun, scripted):
|
934 | 935 | img += 123 # make sure image buffer wasn't freed by underlying decoding lib
|
935 | 936 |
|
936 | 937 |
|
| 938 | +@pytest.mark.parametrize("decode_fun", (decode_webp, decode_image)) |
| 939 | +def test_decode_webp_grayscale(decode_fun, capfd): |
| 940 | + encoded_bytes = read_file(next(get_images(FAKEDATA_DIR, ".webp"))) |
| 941 | + |
| 942 | + # We warn at the C++ layer because for decode_image(), we don't do the image |
| 943 | + # type dispatch until we get to the C++ version of decode_image(). We could |
| 944 | + # warn at the Python layer in decode_webp(), but then users would get a |
| 945 | + # double wanring: one from the Python layer and one from the C++ layer. |
| 946 | + # |
| 947 | + # Because we use the TORCH_WARN_ONCE macro, we need to do this dance to |
| 948 | + # temporarily always warn so we can test. |
| 949 | + @contextlib.contextmanager |
| 950 | + def set_always_warn(): |
| 951 | + torch._C._set_warnAlways(True) |
| 952 | + yield |
| 953 | + torch._C._set_warnAlways(False) |
| 954 | + |
| 955 | + with set_always_warn(): |
| 956 | + img = decode_fun(encoded_bytes, mode=ImageReadMode.GRAY) |
| 957 | + assert "Webp does not support grayscale conversions" in capfd.readouterr().err |
| 958 | + |
| 959 | + # Note that because we do not support grayscale conversions, we expect |
| 960 | + # that the number of color channels is still 3. |
| 961 | + assert img.shape == (3, 100, 100) |
| 962 | + |
| 963 | + |
937 | 964 | # This test is skipped by default because it requires webp images that we're not
|
938 | 965 | # including within the repo. The test images were downloaded manually from the
|
939 | 966 | # different pages of https://developers.google.com/speed/webp/gallery
|
|
0 commit comments