Saving a pytoch tensor as a 32-bit grayscale Image - python

I have manipulated a 32-bit grayscale .tif image which I converted to tensor using PIL. After this I saved it with:
torchvision.utils.save_image(train_img_poac,fp=str(j)+".tif")
This method automatically converts the tensor to an RGB format image. I want my output image to be a 32-bit grayscale image.
I tried to use the arguments in the save_image function but could not find anything. Is converting it to numpy ndarray and then converting it to a 32-bit Image an option?

Unfortunately save_image doesn't have an option for preserving one-channel images. You can use a different library like OpenCV:
import cv2
image = train_img_poac.numpy()
cv2.imwrite('image_name', image)

Related

Convert grayscale png to RGB png image

I have a dataset of medical images in grayscale Png format which must be converted to RGB format. Tried many solutions but in vain.
GIMP, Menu image -> Mode -> RGB mode
If you want to just convert the format, the following method will help you:
In python3, using PILLOW and Numpy:
From PIL import Image
import numpy as np
im = Image.open(path/to/image, 'r').convert('L')
im = np.stack((im,)*3, axis=-1)
im = Image.fromarray(im)
im.save(path/to/save)
But if you want to colorize the image, know that colorization is an well-known image translation problem. Even if multiple approachs exist depending on the domain, I don't know any method that colorize any kind of images.
Some ways of doing so is to train a neural network, but for that, you need to have a dataset of B/W and colored images. Here are some approaches:
Using CNNs and considering the colorization as a regression problem: Let there be Color!
Using CNNs and considering the colorization as a classification problem: Colorful Image Colorization
Using GANs : cycle-gan

Convert boolean matrtix to image matrix in python

I have obtained boolean matrix from sauvola thresholding using scikit image library now I want to do further processing like blob detection on image. How to convert map boolean matrix to grayscale binary image in python.
Followed this link https://stackoverflow.com/a/47667753/9155046
but the output image need to be mapped to grayscale.

Three different types of output when reading an image with three different libraries in Python

I am reading an image in python with three different libraries
imageio
PIL.Image
cv2.
The output I am getting on reading image with each one of these libraries is different. For example
On reading with imageio
a = imageio.imread('test_img.png')
The output is of type - uint8 and size is (500,334,4)
using Image
b = Image.open('test_img.png')
type - Image, size (334,500)
using cv2
c = cv2.imread('test_img.png')
type- uint8, size (500,334,3)
Why I am getting three different size for same image when using three different libraries? Please, help me in understanding the difference.
What you get returned from both imageio & OpenCV are three properties of the image, Height, Width & Channels (or Depth). For standard BGR images you only have 3 channels, that's why you see 3 for the OpenCV
For the imageio it is likely that it is reading a fourth channel, usually alpha, which represents the image transparency and is often found in PNG images.
If you wanted the fourth channel with OpenCV then you would need to use the following code:
Mat image = imread("image.png", IMREAD_UNCHANGED);
Which would give you a fourth channel

How to convert an image with PILLOW temporarily?

I know that PILLOW can convert an image from say jpg to png using the save method but is there a way to convert the image to another format and just keep it as an Image object without actually saving it as another file?
So I want to convert a user supplied image to common format for working with in the program because certain tools I am using only support png.
jpg and png are just compression techniques for saving an image to a file. An image as an object, is just an array of RGB(or any other colorspace/format used by the library which you used to read the file) values of all the pixels.
So technically, you can use the image object as the common format for working with other tools. But you need to keep in mind about the colorspace which is used by each library. Like, OpenCV considers an image object in BGR format, so you need to convert the image object to this format before you use it in OpenCV.

Resample the Image Pixels in Python

I have an image of 300*300 pixels size and want to convert it into 1500*1500 pixels size using python. I need this because i have to georeferenced this image with 1500*1500 pixel raster image. Any python library function or basic fundamental how to do this are welcome.
You should try using Pillow (fork of PIL = Python Image Library)
Simple like this:
from PIL import Image
img = Image.open("my_image.png")
img.resize((1500, 1500, ))
img.save("new_image.png")

Categories

Resources