I want to read pgm image in python. I use cv2.imread('a.pgm') but it returns wrong results. In Matlab, I use imread and get the right result which is a single channel 16-bit image. But cv2.imread in python returns a 3-channel image and the pixel values are also wrong.
Why it happens?
How should I read the 16-bit pgm images in python?
And what libraries?
Thanks in advance.
I got it.
cv2.imread('a.pgm',-1)
works.
You can also use skimage.io library
from skimage.io import imread
image = imread("a.pgm")
Related
I want to save an image without any channel, so the dimension would only be 2. Thus, is there a way I do it in matplotlib?
I have already tried using
matplotlib.pyplot.imsave('img.png', image, cmap='gray')
but when I read it using
matplotlib.pyplot.imread('img.png')
The dimension is 3. So I'm confusing how. I know maybe I can't use imread but what can I do instead?
If you have opencv installed, you can try:
cv2.imread('1.png', cv2.IMREAD_GRAYSCALE)
Also, you can also try PIL.
from PIL import Image
Image.fromarray(array)
I didn't see this one on the internet, but this one works! thanks to my teacher!
skimage.io.imsave('1.png', np.around(image*255).astype(np.uint8))
To use this, you have to have skimage preinstalled.
pip3 install scikit-image
Thanks #Cris Luengo in the comment above to point out that
"From the docs, it looks like matplotlib only saves RGB or RGBA format
image files. You’ll have to use a different package to save a
gray-scale image. OpenCV as suggested below is just one option. There
are many. Try PIL."
Give him an upvote when you saw it!
Is there any way to convert .jpeg to .tiff file?
If yes, then how to do that?
There are many library in Python that can convert file from one format to another.
But, I have not found anything for this problem.
Thanks in advance!
see this
from PIL import Image
im = Image.open('yourImg.jpg')
im.save("pathToSave/hello.tiff", 'TIFF')
You can use PIL (Python Imaging Library) for this:
import Image
im = Image.open('test.jpg')
im.save('test.tiff') # or 'test.tif'
Also, this was the first result for your problem on Google, make sure you google extensively first.
According to OpenCV docs for functions used for image and video reading and writing imread does support JPEG files and imwrite can save TIFF files, though with some limitations:
Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function.
I am getting a ValueError in numpy when performing operations on images. The problem seems to be that the images edited by Paint.NET are missing the RGB dimension when opened using PIL and converted to a numpy array.
If PIL is giving you a 861x1091 image when you are expecting an 861x1091x3 image, that is almost certainly because it is a palette image - see here for explanation.
The simplest thing to do, if you want a 3-channel RGB image rather than a single channel palette image is to convert it to RGB when you open it:
im = Image.open(path).convert('RGB')
I am using skimage.color.rgb2gray to convert image to grayscale. But the method isn't working. What am I missing?
I am having to do a lot of vision related work in Python lately, and I am facing a lot of difficulties switching between formats. When I read an image using Mahotas, I cannot seem to get it to cv2, though they are both using numpy.ndarray. SimpleCV can take OpenCV images easily, but getting SimpleCV image out for legacy cv or mahotas seems to be quite a task.
Some format conversion syntaxes would be really appreciated. For example, if I open a greyscale image using mahotas, it is treated to be in floating point colour space by default, as I gather. Even when I assign the type as numpy.uint8, cv2 cannot seem to recognise it as an array. I do not know how to solve this problem. I am not having much luck with colour images either. I am using Python 2.7 32bit on Ubuntu Oneiric Ocelot.
Thanks in advance!
I have never used mahotas. But I'm currently working on SimpleCV. I have just sent a pull request for making SimpleCV numpy array compatible with cv2.
So, basically,
Image.getNumpy() -> numpy.ndarray for cv2
Image.getBitmap() -> cv2.cv.iplimage
Image.getMatrix() -> cv2.cv.cvmat
To convert cv2 numpy array to SimpleCV Image object,
Image(cv2_image) -> SimpleCV.ImageClass.Image
With only experience in cv2 and SimpleCV, to convert from SimpleCV to cv2:
cv2_image = simplecv_image.getNumpyCv2()
To convert from cv2 to SimpleCV:
simplecv_image = Image(cv2_image.transpose(1, 0, 2)[:, :, ::-1])