io.write_png saves RGB image when given greyscale tensor
See original GitHub issue🐛 Bug
When giving a 3D 1xHxW uint8 tensor to torchvision.io.write_png
, it saves it as a coloured PNG instead of a greyscale PNG. torchvision.io.write_jpeg
works correctly.
To Reproduce
Steps to reproduce the behavior:
- Create a 3D 1x64x64 uint8 tensor.
- Give it to
torchvision.io.write_png
. - Check the saved PNG’s attributes, e.g. using
identify
from ImageMagick. It should say sRGB instead of greyscale.
import torch
from torchvision.io import write_png
img = (torch.rand(1, 64, 64) * 255).byte()
write_png(img, "test.png")
Expected behavior
The saved PNG should be greyscale.
Environment
Please copy and paste the output from our environment collection script (or fill out the checklist below manually).
You can get the script and run it with:
wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
PyTorch version: 1.8.0
Is debug build: False
CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A
OS: Arch Linux (x86_64)
GCC version: (GCC) 10.2.0
Clang version: 11.1.0
CMake version: Could not collect
Python version: 3.9 (64-bit runtime)
Is CUDA available: True
CUDA runtime version: 11.2.142
GPU models and configuration: GPU 0: GeForce GTX 960M
Nvidia driver version: 460.56
cuDNN version: Probably one of the following:
/usr/lib/libcudnn.so.8.1.0
/usr/lib/libcudnn_adv_infer.so.8.1.0
/usr/lib/libcudnn_adv_train.so.8.1.0
/usr/lib/libcudnn_cnn_infer.so.8.1.0
/usr/lib/libcudnn_cnn_train.so.8.1.0
/usr/lib/libcudnn_ops_infer.so.8.1.0
/usr/lib/libcudnn_ops_train.so.8.1.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] torch==1.8.0
[pip3] torchvision==0.9.0
[conda] Could not collect
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Reading/Writing images and videos - PyTorch
Detects whether an image is a JPEG or PNG and performs the appropriate operation to decode the image into a 3 dimensional RGB...
Read more >Saving a pytoch tensor as a 32-bit grayscale Image
This method automatically converts the tensor to an RGB format image. I want my output image to be a 32-bit grayscale image.
Read more >How RGB and Grayscale Images Are Represented in NumPy ...
NumPy array or tensor representation of RGB and grayscale images. Explore the MNIST digits dataset ... (Image by author, made with draw.io).
Read more >Color Space Conversions | TensorFlow I/O
The tensorflow-io package provides a list of color space conversions APIs that can be used to prepare and augment the image data.
Read more >How to Convert an RGB Image to Grayscale - Brandon Rohrer
We were working with a mixture of color and grayscale images and needed to transform them into a uniform format - all grayscale....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@rharish101 oh you are right, the ternary operator was not implemented correctly there and changing this will probably fix the issue
@andfoy could you have a look?