Load a tiff stack in a numpy array with python - python

I am having a little issue with .tif files. I am sure it is only a minor problem that I can´t get around (keep in mind, I am a relatively new programmer).
Basically: I have prepared .tif files that are 64x64xn in size (n up until 1000). The image is only a single file that contains all of this slices. I would like to load the image into a (multidimensional) numpy array. I have tried:
from PIL import Image as pilimage
file_path=(D:\luca\test\test.tif)
print("The selected stack is a .tif")
dataset = pilimage(file_path)
tiffarray = np.array(dataset)
expim = tiffarray.astype(np.double);
print(expim.shape)
and other things (like tifffile). I only seem to be able to read the first slice of the stack. Is it possible for "expim" to contain all information that is saved in the tiff stack?

I am not sure if there is a way to get PIL to open multiple slices of a tiff stack.
If you are not bound to using PIL, however, an alternative is scikit-image, which opens multiple slices from a tiff stack by default. Here is some sample code of how to load a tiff stack into a Numpy array using scikit-image:
>>> from skimage import io
>>> im = io.imread('an_image.tif')
>>> print(im.shape)
(2, 64, 64)
Note that the imread function loads the image directly into a Numpy array. Also, the dimensions of the resulting array are ordered (z, y, x) where z represents the depth, y represents the height, and x represents the width. Thus, to get a single slice from the stack all you have to do is:
>>> print(im[1].shape)
(64, 64)

PIL has a function seek to move to different slices of a tiff stack.
from PIL import Image
file_path=(D:\luca\test\test.tif)
print("The selected stack is a .tif")
dataset = Image.open(file_path)
h,w = np.shape(dataset)
tiffarray = np.zeros((h,w,dataset.n_frames))
for i in range(dataset.n_frames):
dataset.seek(i)
tiffarray[:,:,i] = np.array(dataset)
expim = tiffarray.astype(np.double);
print(expim.shape)

Related

Convert python wand hdr image to numpy array and back

Python wand supports converting images directly to a Numpy arrays, such as can be seen in related questions.
However, when doing this for .hdr (high dynamic range) images, this appears to compress the image to 0/255. As a result, converting from a Python Wand image to a np array and back drastically reduces file size/quality.
# Without converting to a numpy array
img = Image('image.hdr') # Open with Python Wand Image
img.save(filename='test.hdr') # Save with Python wand
Running this opens the image and saves it again, which creates a file with a size of 41.512kb. However, if we convert it to numpy before saving it again..
# With converting to a numpy array
img = Image(filename=os.path.join(path, 'N_SYNS_89.hdr')) # Open with Python Wand Image
arr = np.asarray(img, dtype='float32') # convert to np array
img = Image.from_array(arr) # convert back to Python Wand Image
img.save(filename='test.hdr') # Save with Python wand
This results in a file with a size of 5.186kb.
Indeed, if I look at arr.min() and arr.max() I see that the min and max values for the numpy array are 0 and 255. If I open the .hdr image with cv2 however as an numpy array, the range is much higher.
img = cv2.imread('image.hdr'), -1)
img.min() # returns 0
img.max() # returns 868352.0
Is there a way to convert back and forth between numpy arrays and Wand images without this loss?
As per the comment of #LudvigH, the following worked as in this answer.
img = Image(filename='image.hdr'))
img.format = 'rgb'
img.alpha_channel = False # was not required for me, including it for completion
img_array = np.asarray(bytearray(img.make_blob()), dtype='float32')
Now we much reshape the returned img_array. In my case I could not run the following
img_array.reshape(img.shape)
Instead, for my img.size was a (x,y) tuple that should have been an (x,y,z) tuple.
n_channels = img_array.size / img.size[0] / img.size[1]
img_array = img_array.reshape(img.size[0],img.size[1],int(n_channels))
After manually calculating z as above, it worked fine. Perhaps this is also what caused the original fault in converting using arr = np.asarray(img, dtype='float32')

Insert edited image patch back into original image in Python

I am fairly new to Python, so excuse any blindingly obvious oversights.
I have indexed part of an image, performed some processing on the indexed patch (not shown), and now need to reinsert the processed patch back into the original image. Ideally, the transition between original image and processed patch would be smooth and not introduce artifacts.
Here's what I have so far:
import numpy
import rasterio as rio
import imageio
# read image to be processed
img = rio.open('path/to/image')
img = img.read(1) # convert to array for processing
# define image patch to be processed (original image size: 19,968px x 19,968px)
ind = 1536 # first index position
L = 16898 # length of image
img = img[ind:ind+L,ind:ind+L]
## PROCESSING OF PATCH DONE HERE ##
# convert to uint8 to match original image
img_done = img_done.astype(np.uint8)
# what I tried so far
img_done[ind:ind+L,ind:ind+L] = img
The final line gives the broadcast error ValueError: could not broadcast input array from shape (16898,16898) into shape (15362,15362)
How do I go about inserting the processed patch correctly?

How to resize images with imageio to get proper ICO files?

I want to save ICO files from images. imageio is working perfectly, but for horizontal images, it gave me an error.
This is my code:
import imageio
image = imageio.imread('image.png')
imageio.imwrite("image.ico", image)
I assume, you have problems opening the resulting ICO files, because the software you use simply expects square images of certain size(s) when opening ICO files!? Unfortunately, it seems that imageio.imwrite saves ICO files with only setting the larger dimension to default 16, 24, ... pixels, when feeding a non-square image. Suppose, we have such an image:
And, we have some test code like this:
import imageio
# Read image
img_io = imageio.imread('image.png')
# Write ICO image
imageio.imwrite('image.ico', img_io)
The resulting image.ico has six images with dimensions 16 x 13, 24 x 19, and so on.
If you want to resolve that, you should properly resize your image beforehand to get a square image. Actually, resizing images can be quite difficult when using imageio. Please, see this recent Q&A for some details. If you simply want to have a square image without keeping the aspect ratio, you might want to use skimage.transform.resize:
import imageio
from skimage.transform import resize
# Read image
img_io = imageio.imread('image.png')
# Resize image
img_io = resize(img_io, (200, 200))
# Write ICO image
imageio.imwrite('image.ico', img_io)
Now, the resulting image.ico has six images with dimensions 16 x 16, 24 x 24, and so on.
If you want to keep the aspect ratio of your image, you'd need to add proper borders to your image. There's this helpful Q&A on that issue. You could also add fancy, transparent borders solely using NumPy:
import imageio
import numpy as np
# Read image
img_io = imageio.imread('image.png')
# Add transparent borders to image
h, w = img_io.shape[:2]
img = np.zeros((w, w, 4), np.uint8)
img[30:h+30, :, :3] = img_io
img[30:h+30, :, 3] = 255
# Write ICO image
imageio.imwrite('image.ico', img)
Now, the resulting image.ico even has seven images with dimensions 16 x 16, ..., 256 x 256, since the modified image is large enough.
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
imageio: 2.9.0
NumPy: 1.19.5
scikit-image: 0.18.1
----------------------------------------
You can use reshape() so your code could become something like
import imageio
image = imageio.imread('image.png')
M, N, C = image.shape
image = image.reshape((N, M, C)) # new shape here, I inverted M and N
imageio.imwrite("image.ico", image)
Watch out, however, the number of pixels cannot change with reshape, that is the total number, in this case, is M * N, once you reshape, the new number of pixels must be the same.
If you want to change it, you can use slicing, there are examples in the documentation

How to shuffle pixels in an image using Python Pillow?

My goal is to shuffle all pixels in a 512x512 Python Pillow image. Also, I need the time performance to be relatively good. What I've tried:
from PIL import Image
import numpy as np
orig = Image.open('img/input2.jpg')
orig_px = orig.getdata()
np_px = np.asarray(orig_px)
np.random.shuffle(np_px)
res = Image.fromarray(np_px.astype('uint8')).convert('RGB')
res.show()
The Preview app gives me the following error:
The file “tmp11g28d6z.PNG” could not be opened.
It may be damaged or use a file format that Preview doesn’t recognise.
I cannot figure out, what went wrong. I would be grateful for any suggestions about fixing this code or trying a different approach to solving this problem.
Main problem that getdata provide you 1d array, and fromarray requires 2d or 3d array. see corrected code. You maybe notice two reshapes. So first reshape make array of pixels. Each pixel has 3 values. Than shuffle them, than reshape in image. If you comment np.random.shuffle(orig_px) you will get original image as is.
from PIL import Image
import numpy as np
orig = Image.open('test.jpg')
orig_px = orig.getdata()
orig_px = np.reshape(orig_px, (orig.height * orig.width, 3))
np.random.shuffle(orig_px)
orig_px = np.reshape(orig_px, (orig.height, orig.width, 3))
res = Image.fromarray(orig_px.astype('uint8'))
res.save('out.jpg')

Convert PythonMagick Image object to numpy array (for OpenCV) and then to PIL image object

I want to convert a PythonMagick Image Object to a NumPy array that can be used in OpenCV, and then I want to convert it into a PIL image object. I have searched Google but cannot find any sources explaining how to do this. Can someone show me how to convert image objects between these different modules?
The fastest way that I've found consist in saving and opening it:
import PythonMagic
import cv2
# pm_img is a PythonMagick.Image
pm_img.write('path/to/temporary/file.png')
np_img = cv2.imread('path/to/temporary/file.png')
I haven't found any satisfactory way to convert PythonMagick images to NumPy arrays without saving them, but there is a slow way that involves using python loops:
import PythonMagick
import numpy as np
pm_img = PythonMagick.Image('path/to/image.jpg')
h, w = pm_img.size().height(), pm_img.size().width()
np_img = np.empty((h, w, 3), np.uint16) # PythonMagick opens images with 16 bit depth
# It seems to store the same byte twice (weird)
for i in range(h):
for j in range(w):
# OpenCV stores pixels as BGR
np_img[i, j] = (pm_img.pixelColor(j, i).quantumBlue(),
pm_img.pixelColor(j, i).quantumGreen(),
pm_img.pixelColor(j, i).quantumRed())
np_img = np_img.astype(np.uint8)
Converting NumPy arrays to PIL images is easier:
from PIL import Image
pil_img = Image.fromarray(np_img[:, :, ::-1].astype(np.uint8))
Since PIL stores images in RGB but OpenCV stores them in BGR it's necessary to change the order of the channels ([:, :, ::-1]).
Image.fromarray() takes a NumPy array with dtype np.uint8.

Categories