Reputation:
First are tiff's grayscale? As I understand how tiff's work they can accept floating point numbers as most other image types cannot. I am attempting to do a linear interpolation for smooth coloring and need to put the floating point rgb array into this tiff image. What is the proper way to do this? As of right now I am using PIL and two for loops to iterate over the width and height of the new created tiff to put the pixels there and then converting to CMYK format, but when opening in a program such as Ifran it shows an apparently grayscale image and it doesnt look like the full image got generated, as in only half of it appears in the full image.
Upvotes: 2
Views: 31145
Reputation: 520
import cv2
import numpy as np
image = cv2.imread('tif_file.tif')
image = np.asarray(image,dtype = np.float64)
Upvotes: 2
Reputation: 301
The TIFF file format is very versatile. Yes, it can store pixels in float format, it can even store complex-valued pixels. In general. On the other hand, as far as I know, PIL is not directly able to handle float-valued image data. Even more, saving image data with float-valued pixels is somewhat exotic to most of the world so that only few programs are able to that. And PIL is not one of those. Even numpy / scipy might not be able to store arrays of e.g. dtype=float32 as TIFF. One trick would be to use the TIFFlib directly, e.g. via cython or one could use vigra numpy (http://ukoethe.github.io/vigra/doc-release/vigranumpy/index.html). But, as you have to stick to PIL, all these might not be an option.
On the other hand: Why is it necessary to store the pixels as float? I understand, your program generates pixel values. The common solution here is to scale your float values to the range of 0..255. Let's say, the minimum float value and maximum float value of you red channel are stored in
rmin, rmax
Then apply the following scaling:
ri = int(255 * (r - rmin) / (rmax - rmin) )
assuming that r is your float value. The same for the green and blue channels. This way, you generate an int-valued image.
Finally, I would recommend to store the image using PNG image format which is portable, uses lossless compression and can store 24bit colors (so called 'true color').
Upvotes: 1
Reputation: 5405
As far as I understand TIFF files can be colorful too, there is no restriction on that side.
Here's the definition -- "TIFF (Tag Image File Format) is a common format for exchanging raster graphics (bitmap) images between application programs, including those used for scanner images."
TIFF files can be in any of several classes, including gray scale, color palette, or RGB full color, and can include files with JPEG, LZW, or CCITT Group 4 standard run-length image compression.
This is an example of TIFF image. (link)
Download this image from the link above.
Here's a small Python code that takes a tiff image and converts it to a numpy array for further processing.
from PIL import Image
image_tiff = Image.open('a_image.tif')
image_tiff.show() # opens the tiff image. this rainbow color tiff
To convert this to a numpy array, we do this
import numpy as np
imarray = np.array(image_tiff)
imarray
If you print imarray, it will give you something like this
array([[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
...,
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246],
[ 0, 1, 2, ..., 244, 245, 246]], dtype=uint8)
Upvotes: 0