Import Multipage TIFF as separate images in Python - python

I'm currently working on some image processing in python 2. I'm saving the images as a multipage .tif files, which contain 4 images of resolution 1920x1080. I've imported the tiff file using skimage.io.imread. Once I've done this, I'm left with an ndarray with shape (1080,1920,4). How should I break this into four separate 1920x1080 Numpy arrays that I could then use for image processing?

You can use numpy.dsplit():
arrays = numpy.dsplit(a, a.shape[2])
This will get you a list with the arrays you want.
Optionally you could just use slicing:
arrays = [a[:,:,n] for n in range(a.shape[2])]

Related

Convert Image to array and array to image using python, does the array contain metadata or other info?

Sorry for my english but it's not my first language.
I would like to create a program that:
Transform a jpeg or png image into an array (very important: I would like an array composed only of the values that the pixels of the image have and not metadata or other information. Where I can select each specific pixel of the image).
Save this array in a txt file.
Transform this array composed of only the pixel values of the image back into jpg or png image and save it in a file.
Requests:
Is the array I created with the program I wrote composed only of the pixel values of the image? is there also metadata or other information?
Is this a valid way to remove metadata from an image?
Is this a valid way to create the array representing that image pixel by pixel?
Is this a valid way to convert png images to jpeg or jpeg to png?
Thank you!
This is the program I created, any opinion?
import numpy as np
from PIL import Image
import sys
img_data = Image.open("imagea.jpeg")
img_arr = np.array(img_data)
np.set_printoptions(threshold=sys.maxsize)
print(img_arr.shape)
new_img = Image.fromarray(img_arr)
new_img.save("imageb.jpeg")
print("Image saved!")
file = open("file1.txt", "w+")
content = str(img_arr)
file.write(content)
file.close()
print("Finished!")
Loading an image and converting it to a Numpy array is a perfectly legitimate way of discarding all metadata including:
EXIF data, copyright data,
IPTC and XMP data,
ICC colour profile data
You can tell it's all gone by thinking about the Numpy array you hold and its dimensions and data type.
Note that you need to be careful with PNG palette images and images with an alpha channel.
Note that you can achieve this more simply on the command-line with ImageMagick using:
magick mogrify -strip IMAGE.JPG
Or with exiftool.
Note that you can achieve this by using a format that doesn't support metadata, such as NetPBM, with extension .ppm e.g.:
magick INPUT.JPG -strip -compress none RESULT.PPM # gives P3/plain ASCII file
magick INPUT.JPG -strip RESULT.PPM # gives P6/binary file
You can also read/write PPM files with PIL.

Reading and saving tif images with python

I am trying to read this tiff image with python. I have tried PIL to and save this image. The process goes smoothly, but the output image seems to be plain dark. Here is the code I used.
from PIL import Image
im = Image.open('file.tif')
imarray = np.array(im)
data = Image.fromarray(imarray)
data.save('x.tif')
Please let me know if I have done anything wrong, or if there is any other working way to read and save tif images. I mainly need it as NumPy array for processing purposes.
The problem is simply that the image is dark. If you open it with PIL, and convert to a Numpy array, you can see the maximum brightness is 2455, which on a 16-bit image with possible range 0..65535, means it is only 2455/65535, or 3.7% bright.
from PIL import Image
# Open image
im = Image.open('5 atm_gain 80_C001H001S0001000025.tif')
# Make into Numpy array
na = np.array(im)
print(na.max()) # prints 2455
So, you need to normalise your image or scale up the brightnesses. A VERY CRUDE method is to multiply by 50, for example:
Image.fromarray(na*50).show()
But really, you should use a proper normalisation, like PIL.ImageOps.autocontrast() or OpenCV normalize().

Importing large number of images into Python to convert to Numpy array

I am attempting to import a large number of images and convert them into an array to do similarity comparisons between images based on colors at each pixel and shapes contained within the pictures. I'm having trouble importing the data, the following code works for small numbers of images (10-20) but fails for larger ones (my total goal is to import 10,000 for this project).
from PIL import Image
import os,os.path
imgs=[]
path="Documents/data/img"
os.listdir(path)
valid_images =[".png"]
for f in os.listdir(path):
ext= os.path.splitext(f)[1]
if ext.lower() not in valid_images:
continue
imgs.append(Image.open(os.path.join(path,f)))
When I execute this I receive the following message
OSError: [Errno 24] Too many open files: 'Documents/data/img\81395.png'
Is there a way to edit how many files can be open simultaneously? Or possibly a more efficient way to convert these tables to arrays as I go and "close" the image? I'm very new to this sort of analysis so any tips or pointers are appreciated.
Don't store PIL.Image objects and just convert them into numpy arrays instead. For that you need to change the line where you append image to a list to this:
'''
imgs.append(np.asarray(Image.open(os.path.join(path,f))))
'''

Iterate Images through Folder

i have a folder of gray-scale images from 0 to 9 and they are around 2400, i need to load them in python so as to have all Zeors together as an array , then Ones together as an array, etc...
i used below code to load one image as an array but i don`t know how to load all images and group each number together.
i thought about iteration through the folder.
Do anyone know how to do it or is there any other idea?
import imageio
im = imageio.imread('Train/1.jpg')
You can do it in the following manner:
import imageio
for i in range(9):
im = imageio.imread('Train/'+str(i)+'.jpg')
You can also create a 3D variable whose third dimension is equal to the number of files to read the complete data in a single 3D array.

Read binary .pgm files with python and numpy

I need to read 64x64 pgm plain format image files and put the resulting values in a numpy matrix. I can do it only if I use Opencv and PIL functions to open the image, but the final numpy matrix yielded has 3-channel and the values returned are 0 and 255, instead of 0 and 1 (the image is binary). I also tried to use genfromtxt but it can't put the values in a numpy matrix.
I only want a 1 channel numpy matrix with 0 and 1's from the pgm image. How can I do that with python?
If PIL opens your image files as RGB but you want them in binary, I think your only choice is to convert after opening.
im = Image.open('imagefile').convert('1')

Categories

Resources