question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

  1. Create a 3D 1x64x64 uint8 tensor.
  2. Give it to torchvision.io.write_png.
  3. 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:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
fmassacommented, Mar 11, 2021

@rharish101 oh you are right, the ternary operator was not implemented correctly there and changing this will probably fix the issue

1reaction
fmassacommented, Mar 10, 2021

@andfoy could you have a look?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found