Displaying a grayscale Image - python

My aim:
Read an image into the PIL format.
Convert it to grayscale.
Plot the image using pylab.
Here is the code i'm using:
from PIL import Image
from pylab import *
import numpy as np
inputImage='C:\Test\Test1.jpg'
##outputImage='C:\Test\Output\Test1.jpg'
pilImage=Image.open(inputImage)
pilImage.draft('L',(500,500))
imageArray= np.asarray(pilImage)
imshow(imageArray)
##pilImage.save(outputImage)
axis('off')
show()
My Problem:
The image get's displayed like the colours are inverted.
But I know that the image is getting converted to grayscale, because when I write it to the disk it is appearing as a grayscale image.(Just as I expect).
I feel that the problem is somewhere in the numpy conversion.
I've just started programming in Python for Image Processing.
And Tips and Guideline will also be appreciated.

You want to over-ride the default color map:
imshow(imageArray, cmap="Greys_r")
Here's a page on plotting images and pseudocolor in matplotlib .

This produces a B&W image:
pilImage=Image.open(inputImage)
pilImage = pilImage.convert('1') #this convert to black&white
pilImage.draft('L',(500,500))
pilImage.save('outfile.png')
From the convert method docs:
convert
im.convert(mode) => image
Returns a converted copy of an image.
When translating from a palette image, this translates pixels through the palette.
If mode is omitted, a mode is chosen so that all information in the image and the palette can be represented without a palette.
When from a colour image to black and white, the library uses the ITU-R 601-2 luma transform:
L = R * 299/1000 + G * 587/1000 + B * 114/1000
When converting to a bilevel image (mode "1"), the source image is first converted to black and white.
Resulting values larger than 127 are then set to white, and the image is dithered.
To use other thresholds, use the point method.

Related

Why cv2.write saves black images?

hi folks, greetings
am using this code that I found on the web, to apply a wiener filter on an image, the code :
from scipy.signal.signaltools import deconvolve
from skimage import color, data, restoration
img = color.rgb2gray(img)
from scipy.signal import convolve2d
psf = np.ones((5, 5)) / 25
img = convolve2d(img, psf, 'same')
img += 0.1 * img.std() * np.random.standard_normal(img.shape)
deconvolved_img = restoration.wiener(img, psf, 1100)
f, (plot1, plot2) = plt.subplots(1, 2)
plot1.imshow(img)
plot2.imshow(deconvolved_img)
plt.show()
cv2.imwrite("wiener result 2.jpeg",deconvolved_img)
the issue is when I plot the result using Matplotlib I get this :
but when I type cv2.imwrite("wiener result 2.jpeg",deconvolved_img) to save the image, I get this :
why do I get a black image when I save it ??
There are two ways to save your images as a file:
Method 1: Using matplotlib
Since you are using matplotlib library to show the image plt.show(), you can use the same library to save your plots as well using plt.savefig()
plot1.imshow(img)
plot2.imshow(deconvolved_img)
plt.savefig('path_to_save) # mention the path you want to save the plots
plt.show()
Method 2: Using OpenCV
You can also save your file using OpenCV.
But prior to saving your image there is a conversion required. The image variable deconvolved_img is of float data type, with values ranging between [0 - 1]. Hence when you save such images they are perceived as a black image.
In OpenCV you can convert your image to int data type and scaling the pixel intensities between the expected [0 - 255] using cv2.normalize() function:
result = cv2.normalize(deconvolved_img, dst=None, alpha=0, beta=255,norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
cv2.imwrite('path_to_save', result) # mention the path you want to save the result

Why 16bit to 8bit conversion produces striped image?

I am testing a segmentation algorithm on several VHSR satellite images, which originally comes in 16bit format, but when I convert them to 8bit images, the produced images are showing striped appearance.
I've been trying different python libraries (skimage, cv2, scipy) getting similar results.
1) The original 16-bit image it is a 4 band image (NIR,B,G,R), so you need to choose the right bands to create a true color image, RGB image (4,3,2 bands). thanks in advance. It can be downloaded from this link:
16bit image
2) I use this code to convert each pixel value, from a 16-bit integer now fitting within 8-bit range:
from scipy.misc import bytescale
SS = io.imread('Imag16bit.tif')
SS = bytescale(SS)
SS = np.asarray(SS)
plt.imshow(SS)
This is my result of above code:
bytescale works for me. I think the asarray step messes up something.
import cv2
from skimage import io
from scipy.misc import bytescale
image = io.imread('SkySat_16bit.tif')
cv2.imshow('Original', image)
print(image.dtype)
image = bytescale(image)
print(image.dtype)
cv2.imshow('Converted', image)
cv2.waitKey(0)
I think this is a way to do it:
#!/usr/local/bin/python3
from PIL import Image
from tifffile import imsave, imread
# Load image
im = imread('SkySat_16bit.tif')
# Extract Red, Green and Blue bands into separate 8-bit arrays
R = (im[:,:,3]/256).astype(np.uint8)
G = (im[:,:,2]/256).astype(np.uint8)
B = (im[:,:,1]/256).astype(np.uint8)
# Combine bands into RGB array
RGB = np.dstack((R,G,B))
# Save to disk
Image.fromarray(RGB).save('result.png')
You may want to adjust the contrast a bit, and check I selected the correct bands.

How to change grayscale to colour in python?

I have found the following link: Python colour to greyscale
I would however like to do the opposite. Is this possible with Pyython (preferably with PIL, but other options are welcome as well like matplotlib)?
I need to read in a greyscale png image, which I would like to convert to a rainbow scale (preferably desaturated rainbow, but not necessary). The images originally come from c-code that generates numbers between 0 and 256 and converts those to grey tones. I would like to map those values now linearly to a colour-map (but I currently only have access to the png-image, not the c-code any more). So is there a way to map white to blue, and black to red, with all colours of the rainbow in between?
The mapping from color to grey is not invertable. So you need to indeed define some colormapping like the matplotlib colormaps do.
import matplotlib.pyplot as plt
# generate gray scale image
import scipy.misc
face = scipy.misc.face()
plt.imsave("face.png", face.mean(2), cmap="gray")
# read in image
im = plt.imread("face.png")
# plot image in color
plt.imshow(im.mean(2), cmap="jet_r")
#save image in color
plt.imsave("color.png", im.mean(2), cmap="jet_r")
plt.show()

Python: Greyscale image: Make everything white, except for black pixels

I tried to open (already greyscale) images and change all non-black pixels to white pixels. I implemented the following code:
from scipy.misc import fromimage, toimage
from PIL import Image
import numpy as np
in_path = 'E:\\in.png'
out_path = 'E:\\out.png'
# Open gray-scale image
img = Image.open(in_path).convert('L')
# Just for testing: The image is saved correct
#img.save(out_path)
# Make all non-black colors white
imp_arr = fromimage(img)
imp_arr = (np.ceil(imp_arr / 255.0) * 255.0).astype(int)
# Save the image
img = toimage(imp_arr, mode='L')
img.save(out_path)
The calculation to make all pixels white, except for the black ones is quite simple and also very fast. For my use-case it is especially important that it works very fast, for this reason i used numpy. For some reason this code does not work with all images?
An example: The following image is the input.
It contains a grey rectangle and also a white border. The output should be a complete white image, but for some reason the output is a black image:
With some other images it works quite well. What do i do wrong? I think floating point shouldn't be a big issue here, because this code does not require a high calculation accuracy to work.
Thank you very much
toimage expects a byte array, so convert to uint8 not int:
imp_arr = (np.ceil(imp_arr / 255.0) * 255.0).astype('uint8')
I seems to work for int if there is a mix of black and white pixels in the output, but not if they are all white. I can't find any explanation for this in the documentation.

Using Python Pillow lib to set Color depth

I am using the Python Pillow lib to change an image before sending it to device.
I need to change the image to make sure it meets the following requirements
Resolution (width x height) = 298 x 144
Grayscale
Color Depth (bits) = 4
Format = .png
I can do all of them with the exception of Color Depth to 4 bits.
Can anyone point me in the right direction on how to achieve this?
So far, I haven't been able to save 4-bit images with Pillow. You can use Pillow to reduce the number of gray levels in an image with:
import PIL.Image as Image
im = Image.open('test.png')
im1 = im.point(lambda x: int(x/17)*17)
Assuming test.png is a 8-bit graylevel image, i.e. it contains values in the range 0-255 (im.mode == 'L'), im1 now only contains 16 different values (0, 17, 34, ..., 255). This is what ufp.image.changeColorDepth does, too. However, you still have a 8-bit image. So instead of the above, you can do
im2 = im.point(lambda x: int(x/17))
and you end up with an image that only contains 16 different values (0, 1, 2, ..., 15). So these values would all fit in an uint4-type. However, if you save such an image with Pillow
im2.save('test.png')
the png will still have a color-depth of 8bit (and if you open the image, you see only really dark gray pixels). You can use PyPng to save a real 4-bit png:
import png
import numpy as np
png.fromarray(np.asarray(im2, np.uint8),'L;4').save('test4bit_pypng.png')
Unfortunately, PyPng seems to take much longer to save the images.
using changeColorDepth function in ufp.image module.
import ufp.image
import PIL
im = PIL.Image.open('test.png')
im = im.convert('L') # change to grayscale image
im.thumbnail((298, 144)) # resize to 294x144
ufp.image.changeColorDepth(im, 16) # change 4bits depth(this function change original PIL.Image object)
#if you will need better convert. using ufp.image.quantizeByImprovedGrayScale function. this function quantized image.
im.save('changed.png')
see example : image quantize by Improved Gray Scale. [Python]

Categories

Resources