I am reading an RGB image and converting it into HSV mode using PIL. Now I am trying to save this HSV image but I am getting an error.
filename = r'\trial_images\cat.jpg'
img = Image.open(filename)
img = img.convert('HSV')
destination = r'\demo\temp.jpg'
img.save(destination)
I am getting the following error:
OSError: cannot write mode HSV as JPEG
How can I save my transformed image? Please help
Easy one...save as a numpy array. This works fine, but the file might be pretty big (for me it go about 7 times bigger than the jpeg image). You can numpy's savez_compressed
function to cut that in half to about 3-4 times the size of the original image. Not fantastic, but when you are doing image processing you are probably fine.
Related
I am trying to apply a BinaryMorphologicalClosingImageFilter to a binary TIFF image to fill empty spaces between structures (where ImageJ Fill Holes doesn't help). Here is the code I use:
import SimpleITK as sitk
#Import TIFF image
image = sitk.ReadImage("C:/Users/Christian Nikolov/Desktop/STL/4_bina.tif")
#Apply Filter
sitk.BinaryMorphologicalClosingImageFilter()
#Export Image
sitk.WriteImage(image, "C:/Users/Christian Nikolov/Desktop/STL/4_bina_itk.tif")
The code runs without an error but the problem is that I can't figure out how to set the Kernel Size of the filter and the image doesn't experience a change. Any advise?
(I got the idea to use this filter from the following post on SO: Fill holes on a 3D image)
You've created the BinaryMorphologicalClosingImageFilter object, but you haven't actually applied it to your input image. Your code should be something like this:
import SimpleITK as sitk
#Import TIFF image
image = sitk.ReadImage("C:/Users/Christian Nikolov/Desktop/STL/4_bina.tif")
#Apply Filter
filter = sitk.BinaryMorphologicalClosingImageFilter()
filter.SetKernelRadius([2, 2])
output_image = filter.Execute(image)
#Export Image
sitk.WriteImage(output_image, "C:/Users/Christian Nikolov/Desktop/STL/4_bina_itk.tif")
I set the kernel size to [2, 2], but you can use whatever unsigned integer size works best for you.
I am trying to read this tiff image with python. I have tried PIL to and save this image. The process goes smoothly, but the output image seems to be plain dark. Here is the code I used.
from PIL import Image
im = Image.open('file.tif')
imarray = np.array(im)
data = Image.fromarray(imarray)
data.save('x.tif')
Please let me know if I have done anything wrong, or if there is any other working way to read and save tif images. I mainly need it as NumPy array for processing purposes.
The problem is simply that the image is dark. If you open it with PIL, and convert to a Numpy array, you can see the maximum brightness is 2455, which on a 16-bit image with possible range 0..65535, means it is only 2455/65535, or 3.7% bright.
from PIL import Image
# Open image
im = Image.open('5 atm_gain 80_C001H001S0001000025.tif')
# Make into Numpy array
na = np.array(im)
print(na.max()) # prints 2455
So, you need to normalise your image or scale up the brightnesses. A VERY CRUDE method is to multiply by 50, for example:
Image.fromarray(na*50).show()
But really, you should use a proper normalisation, like PIL.ImageOps.autocontrast() or OpenCV normalize().
I am trying to edit this image:
However, when I run
im = Image.open(filename)
im.show()
it outputs a completely plain white image of the same size. Why is Image.open() not working? How can I fix this? Is there another library I can use to get non-255 pixel values (the correct pixel array)?
Thanks,
Vinny
Image.open actually seems to work fine, as does getpixel, putpixel and save, so you can still load, edit and save the image.
The problem seems to be that the temp file the image is saved in for show is just plain white, so the image viewer shows just a white image. Your original image is 16 bit grayscale, but the temp image is saved as an 8 bit grayscale.
My current theory is that there might actually be a bug in show where a 16 bit grayscale image is just "converted" to 8 bit grayscale by capping all pixel values to 255, resulting in an all-white temp image since all the pixels values in the original are above 30,000.
If you set a pixel to a value below 255 before calling show, that pixel shows correctly. Thus, assuming you want to enhance the contrast in the picture, you can open the picture, map the values to a range from 0 to 255 (e.g. using numpy), and then use show.
from PIL import Image
import numpy as np
arr = np.array(Image.open("Rt5Ov.png"))
arr = (arr - arr.min()) * 255 // (arr.max() - arr.min())
img = Image.fromarray(arr.astype("uint8"))
img.show()
But as said before, since save seems to work as it should, you could also keep the 16 bit grayscale depth and just save the edited image instead of using show.
you can use openCV library for loading images.
import cv2
img = cv2.imread('image file')
plt.show(img)
I am using code that is not throwing errors but it is not behaving as expected. I expect this code to save the pdf as grayscale images. It does save the images but they are still in color.
I know they are not in grayscale because I'm using numpy to convert them to pixel tabels (list of lists) and they contain the
[[255,255,255][255,255,255]]... structure of a color image.
from wand.image import Image as Img
with Img(filename=pdf_file, resolution=300) as img:
img.type = 'grayscale'
img.compression_quality = 99
img.save(filename=image_file)
I am opening the files with PIL using .convert('L'). I think I'm opening them as grayscale but that doesn't seem to be solving my problem either.
img = Image.open(first_page).convert('L')
I'm new to image processing. I just wanted to get a tiff image from raw format(NEF). I used rawpy module to get the desired output, yet the tiff image is RGB with 4 channels. I couldn't know why there is a fourth channel in the new image?
Can anyone please explain to me what is going on, and how I can get tiff image with three RGB channels?
import rawpy
import matplotlib.pylab as plt
raw_image = "DSC_0001.NEF"
raw = rawpy.imread(raw_image)
rgb = raw.postprocess()
plt.imsave("new.tiff", rgb )
image = plt.imread("new.tiff")
print(image.shape)
The array shape is : (2868, 4310, 4) !
Finally I found the reason:
plt.imsave saves the image in RGBA , while I can use skim age.io.imsave and it will save it as RGB.
Source: Github Issue entry