Save 32-bit floating point TIFF image - python

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

Related

how to convert jpeg to tiff file in python

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.

How can I efficiently simulate image compression artifacts in Python?

I'm training a neural net using simulated images, and one of the things that happens in real life is low quality JPEG compression. It fuzzes up sharp edges in a particular way. Does anyone have an efficient way to simulate these effects? By that I mean create a corrupted version of a clean input. The images are grayscale, stored as numpy arrays.
Thanks to the answers in the comments, here is a solution which saves the image as JPEG and reads it back in, all in memory using standard python libraries.
import io
import imageio
# Image is 2D numpy array, q is quality 0-100
def jpegBlur(im,q):
buf = io.BytesIO()
imageio.imwrite(buf,im,format='jpg',quality=q)
s = buf.getbuffer()
return imageio.imread(s,format='jpg')
In my function I also pre- and post-scaled the image to convert from float64 to uint8 and back again, but this is the basic idea.

cvtColor "code" for 16-bit grayscale images

I have unsigned 16-bit grayscale tiff images as numpy arrays. I want to do some image processing on these images using OpenCV. I am converting the numpy array to Mat format using cv2.cvtColor(src, code). As far as the documentation goes, I am having a hard time finding the right code argument to correctly convert 16-bit grayscale images without losing any information.
Previously, I read the images directly using cv2.imread(src, cv2.IMREAD_UNCHANGED). However, I don't have the original image files now, only the pickled numpy array. I am looking for the code in cvtColor which does a similar thing as cv2.IMREAD_UNCHANGED
Your question is tough to follow. All you appear to have are some files containing pickled Numpy arrays, correct?
If so, you don't need any imread(), you just need to unpickle the files and you will have Numpy arrays of the type that OpenCV uses to hold images. Check their dtype is np.uint16 and their shape appears correct.

How we can read 16 un signed integer(16 uint) jpeg files in python

How we can read the 16 uint jpeg images in python
please suggest me the libraries which can read the these type of files in python.
i tried matplotlib, scipy, scikit-image, medpy ,Pil ,opencv, numpy libraries.
when we are using these libraries i am getting the out put as:
raise IOError("cannot identify image file")
IOError: cannot identify image file
please help me
find the file from the link
https://drive.google.com/file/d/0B4l5GiM7kBXraDEyMXdseENfUlE/edit?usp=sharing
Having 16-bit JPEG images sounds a bit strange, as the JPEG standard does not support 16-bit images. It has 12-bit images, though. Unfortunately, most readers only support the usual 8-bits/pixel RGB images, so even with the 12-bit images this may be a bit challenging.
One useful test could be to try:
hdr = open("myimage.jpeg", "rb").read(2)
print "{:02x} {:02x}".format(ord(hdr[0]), ord(hdr[1]))
If your file is a JPEG file, it should start with:
ff d8
If you do not get those, then the file is something else. Is there any program you can use to open the file? Which program produced the files?
This is the standard for 16bits grayscale lossless jpeg (recommendations ITU-T T.81). Now replaced with JPEG-LS and JPEG-2000.
This specific type of JPEG has single channel grayscale on a 16bits wide word, unlike 3 components RGB one on a 24bits/8 bits per channel.
Try using thorfdbg's libjpeg as it supports this type of old jpeg standard: https://github.com/thorfdbg/libjpeg

Making a 3 Colour FITS file using aplpy

I am trying to make a three colour FITS image using the $aplpy.make_rgb_image$ function. I use three separate FITS images in RGB to do so and am able to save a colour image in png, jpeg.... formats, but I would prefer to save its as a FITS file.
When I try that I get the following error.
IOError: FITS save handler not installed
I've tried to find a solution in the web for a few days but was unable to get any good results.
Would anyone know how to get such a handler installed, or perhaps any other approach I could use to get this done?
I don't think there is enough information for me to answer your question completely; for example, I don't know what call you are making to perform the "image" "save", but I can guess:
FITS does not store RGB data like you wish it to. FITS can store multi-band data as individual monochromatic data layers in a multi-extension data "cube". Software, including ds9 and aplpy, can read that FITS data cube and author RGB images in RGB formats (png, jpg...). The error you see comes from PIL, which has no backend to author FITS files (I think, but the validity of that point doesn't matter).
So I think that you should use aplpy.make_rgb_cube to save a 3 HDU FITS cube based your 3 input FITS files, then import that FITS cube back into aplpy and use aplpy.make_rgb_image to output RGB compatible formats. This way you have the saved FITS cube in near native astronomy formats, and a means to create RGB formats from a variety of tools that can import that cube.

Categories