Saving an Image as an OIB File in Python - python

I want to save an image/an array as an OIB File.
I have tried using the oiffile library. I am able to open and read OIB files, but I want to save an image as an OIB File.

Since oiffile uses cv2 structure for opening/closing images (via numpy arrays), so you might be opening the image using imread(). Then you can use imwrite() for saving/writing the image file to a destination path.

Related

Python PIL: open many files and load them into memory

I have a dataset containing 3000 images in train and 6000 images in test. It's 320x320 rgb png files. I thought that I can load this entire dataset into memory (since it's just 100mb), but then I try to do that I'm getting "[Errno 24] Too many open files: ..." error. Code of loading looks like that:
train_images = []
for index, row in dataset_p_train.iterrows():
path = data_path / row.img_path
train_images.append(Image.open(path))
I know that I'm opening 9000 files and not closing them which isn't a good practice, but unfortunately for my classificator I heavily rely on PIL img.getcolors() method, so I really want to store that dataset in memory as list of PIL images and not as a numpy array of 3000x320x320x3 uint8 to avoid casting them into PIL image each time I need colors of image.
So, what should I do? Somehow increase limit of opened files? Or there is a way to make PIL images reside entirely in memory without being "opened" from disk?
Image.open is lazy. It will not load the data until you try to do something with it.
You can call the image's load method to explicitly load the file contents. This will also close the file, unless the image has multiple frames (for example, an animated GIF).
See File Handling in Pillow for more details.

How to find the original format of images (pages) present in a tiff file using python?

I have a multi-page tiff file (merged.tiff) out of which I need to extract individual images in their original format. PIL allows you to iterate through pages and writing them to disk in a format I need (png/jpg).
Ex:
from PIL import Image
img = Image.open('merged.tiff')
for i in range(img.n_frames):
try:
img.seek(i)
img.save(f'individual_{i}.jpg')
img.save(f'individual_{i}.png')
except EOFError:
break
But is there a way to know the original format of those images?
I have tried with tifffile and tiffany which allow me to convert the pages to a numpy array and then write to disk as an image, but they don't allow me to know the source format of the images contained in the TIFF file.
In the most general case, I believe this is impossible, because it is perfectly feasible to take, say, a JPEG image and include it in the TIFF file as an uncompressed RGB array.
Realistically, though, you should be able to look at some of the tags of the TIFF file, e.g. Compression, to make an educated guess about what the image used to be. Tools like tiffinfo and tiffdump (from the libtiff package) can be used to examine the TIFF file.

How to transfer a leica microscope lif file to a python numpy array?

I am working with confocal microscope which generates image files in .lif format.
People use to open these files using ImageJ and then convert them into TIFF files.
I want to be able to manipulate this files using python, but I want to skip having to depend on ImageJ.
My question is: Is there a way to open/ read lif leica files using python?
thank you

Changing of pixel values after writing the same image using imwrite opencv python function

import cv2
import numpy as np
im=cv2.imread('test.jpg')
cv2.imwrite('result.jpg',im)
Here test.jpg have size 19 KB and result.jpg have 41 KB even though they are same images.
I observed that there is change in the pixel values of these two images.
How to prevent this one ?
Re-writing or 'saving' an image in any library or tool will always create a new file or 'new pixel values'. This happens because the image is being processed for saving at a certain quality. The saved image quality and size depends on the library doing the save. So default values, depth, compression, etc. need to be provided.
If you just want to create a copy of the image in a new file, either copy the file directly via sys or binary read the whole file and write it to a new one - without using any image processing libs.

Open tiff image stack in python with gdal

I'm trying to open a tiff stack using gdal in python. It is a single file, within which are mutliple tiff files. When I open it in ImageJ, I can scroll through the stack. But doing "gdal.Open(file).ReadAsArray() only loads the first tiff file. Is there a way I can load the whole stack?
Iterate over GetSubDatasets and (in case all TIFF pages have the same dimensions) create a single numpy array:
import numpy, gdal, gdalconst
dataset = gdal.Open("multipage.tiff", gdalconst.GA_ReadOnly)
data = numpy.array([gdal.Open(name, gdalconst.GA_ReadOnly).ReadAsArray()
for name, descr in dataset.GetSubDatasets()])

Categories

Resources