Question

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")
Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top