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])
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'm trying to save a 32-bit floating point image (stored as a Numpy array) as a TIFF file using tifffile.py.
import numpy as np
import tifffile
image = np.random.rand(500, 500, 3).astype(np.float32)
tifffile.imsave('image.tiff', image)
However, when viewing the output of the above code in Eye of Gnome, the image is entirely blank.
I think the problem is that not all tools support multi-channel TIFFs with 32-bits per channel. For example, as far as I can tell Python's PIL library does not. But I think tifffile.py does, because if I use your code I get a TIFF that opens, and looks reasonable, in GIMP:
From what I read, Photoshop can read 32-bit TIFFs too. So I think the TIFF file contains your image, but whether it works for you or not depends on what you want to do with it next.
This question might be relevant too, although it's about using 16-bit integers not floats: Python: Read and write TIFF 16 bit , three channel , colour images
I used the convert Imagemagik utility to create the grayscale jpg images. Need help in finding the equivalent utility in Python. I tried PIL but this create png images.
I thought of calling the "convert" utility through python. Is there any better solution than this.
I want to feed the images in tensorflow. The issue is I am using tensorflow 1.1.
Huh? This works fine and creates a grey JPEG:
from PIL import Image
image=Image.open('start.png')
grey=image.convert('L')
grey.save("result.jpg")
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")