Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save raw data as tif

I need to analyze a part of an image, selected as a submatrix, in a tif file. I would like to have the image in raw format, with no frills (scaling, axis, labels and so on)... How could I do that?

This is the code I am using now:

 submatrix = im[x_min:x_max, y_min:y_max]
 plt.imshow(submatrix)
 plt.savefig("subplot_%03i_%03i.tif" % (index, peak_number), format = "tif")
like image 328
albus_c Avatar asked Oct 30 '13 04:10

albus_c


People also ask

How do I save a file as TIF?

Right-click on the image and select “Save as Picture.” 5. Name file and change “Save as type” to “Tag Image File Format (*. tif).”

Is TIF a RAW file?

Do TIFFs keep RAW data? No. While TIFF is a lossless format, it only stores the processed image. That's why it has less post-processing flexibility than a RAW file.

Is TIFF similar to RAW?

Unlike TIFF, a RAW file first needs to be processed or developed using Image Data Converter or other compatible software. The benefit of this format is that you can adjust various attributes such as contrast, saturation, sharpness, white balance, and others without degrading the image.

Which is better TIF or TIFF?

TIF vs TIFF Well, to cut to the point, there is no difference between TIF and TIFF. They both are extensions used by the Tagged Image File Format (TIFF), which is used in storing images like photos. The appearance of TIF and TIFF is not actually related to the format itself but to limitations imposed by file systems.


1 Answers

First off, if you're just wanting to store the raw values or a grayscale representation of the raw values, it's easiest to just use PIL for this.

For example, this will generate a 10x10 grayscale tif file:

import numpy as np
import Image

data = np.random.randint(0, 255, (10,10)).astype(np.uint8)
im = Image.fromarray(data)
im.save('test.tif')

As far as your question about why the matplotlib version has more pixels, it's because you implictly told it to. Matplotlib figures have a size (in inches) and a dpi (by default, 80 on-screen and 100 when saved). Also, by default imshow will interpolate the values in your array, and even if you set interpolation to nearest, the saved image will still be the size you specified for the figure.

If you want to use matplotlib to save the figure at one-value-to-one-pixel (for example, to allow easy use of colormaps), do something similar to this:

import numpy as np
import matplotlib.pyplot as plt

dpi = 80 # Arbitrary. The number of pixels in the image will always be identical
data = np.random.random((10, 10))

height, width = np.array(data.shape, dtype=float) / dpi

fig = plt.figure(figsize=(width, height), dpi=dpi)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')

ax.imshow(data, interpolation='none')
fig.savefig('test.tif', dpi=dpi)
like image 141
Joe Kington Avatar answered Oct 13 '22 12:10

Joe Kington