How open a stack of .tif images with python - python

i have a file with 300 .tif images, i want to open it with python and save them into an array, list or any similar structure. I´ve tried the code below but only appears 3 images. ¿Any solution?. Thanks.
raw = cv2.imread('sen_15Hz_500Hz_6_6A.tif')
print(np.shape(raw))
I´ve tried with PIL and cv2 too.

If you really have one single TIFF file with 300 images in it, the simplest is to open it with tifffile.
with TiffFile('temp.tif') as tif:
for page in tif.pages:
image = page.asarray()
If you want to use PIL look at the section "Image Sequences" here.

Related

Extracting small region of image from BMP file in Python

I have a BMP image and I want to extract a small portion of it and save it as a new BMP image file.
I was able to load image and read it however I was not able to extract the small portion of BMP image as I am new to manipulating BMP image with python and also it not same as reading text file.
Things I have to do is
load image.
extract small portion of image.
eg. I have to extract 40X40 pixel image from 900X900 image file
then save extracted image as new file. eg new.bmp
I am trying to do this for last 3 days also I have searched a lot in the net but got solution which uses Pillow library however I need it to do this without using any external module of Python. Stackoverflow is my last hope I need some guidance from a expert people present here, please provide my some guidance.

Opening Tif file with PIL "PIL.UnidentifiedImageError: cannot identify image file"

I've a huge TIF file of shape (39906, 30365, 4). I want to use it on PyQt5, however when I use PIL to open the image it gives the error "PIL.UnidentifiedImageError: cannot identify image file".
I've searched and it seems that PIL can open TIF/TIFF files, but it has to be 8bit and my image is 8bit.
What could I do fix it? Is the file to large to be open by PIL? Is there another option to open a huge TIF to be used with PyQt5?
It's not necessary to open the whole image. Actually, if I could open a 25% scaled version of the original would be better.
Pillow version = 7.2.0
If you are having issues handling very large images, consider using libvips, either in your Python code or in the Terminal. It is very fast and frugal with memory.
Here's an example for Terminal:
vipsthumbnail BIGBOY.TIF --size 10000x -o small.tif # reduce width to 10000px
And see usingVIPSandShrink() here.

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.

PIL(low) and multi-page TIFFS

This should be relatively easy, but who knows...
Is there a way to retrieve the number of frames in a multi-page TIFF file using PIL/Pillow, without iterating through the whole stack until seek raises an error?
Try using n_frames:
from PIL import Image
tiffstack = Image.open('my_img.tiff')
tiffstack.load()
print(tiffstack.n_frames)

Convert DICOM to TIFF

I'm new to Python so forgive my ignorance If I don't have all the info correct. I'm trying raster through a directory and convert all the DICOM files within to TIFF files. I have gotten the search functionality to work, but I am having a hard time saving the images as TIFFs. I'm using the pydicom libraries to read in the DICOM and manipulate the header information. Also, I have tried using the save_as function in pydicom to save to TIFF, but I would rather use the save function in PIL to properly set the compression of the TIFF. I think the problem is that I can't/don't understand how to extract the actual image data from a DICOM and place it in a new image.Any Help would be greatly appreciated ... Cheers
Python 2.7
PIL 1.1.7
Pydicom 0.9.6
Found an answer online to the same query sometime back, although I don't remember the site but as I applied it to my code, sharing it here for others as well :
import pylab
import dicom
ImageFile=dicom.read_file(<SourceFilePath>) #Path to "*.dcm" file
pylab.imshow(ImageFile.pixel_array,cmap=pylab.cm.bone) #to view image
or if you want to save the image then instead use:
pylab.imsave('<DestinationFilePath>',ImageFile.pixel_array,cmap=pylab.cm.bone)
The imsave will by default save the image in .png format though. You can specify the desired format in the imsave() if it is supported.
Hope it is useful.
If you know how to use PIL to save image data as .tiff, this example should help you to pass image data from pydicom to PIL (there is more here in the comments).

Categories