Importing all pictures from the directory - python

In this directory "C:\Users\KG\Documents\R\data" I have 40 folders that are named from s1 to s40, where in each of the folder there are 10 pictures (.png) of faces named as (1,2,..10). How to import collection of pictures - faces as a flattened array? I use the code below, but it provide me with the mistake (does not download pictures):
from skimage import io
ic = io.ImageCollection('C:/Users/KG/Documents/R/data/*/*.png')
ic = np.array(ic)
ic_flat = ic.reshape((len(ic), -1))

You can use PIL library :
from PIL import Image
import numpy as np
ic = []
for i in folders:
for j in images:
image = Image.open(i + j)
ic.append(np.asarray(image))
ic = np.array(ic)
where folders and images are arrays of string with names

Give this code a try:
import os
from skimage import io
import numpy as np
folder = 'C:/Users/KG/Documents/R/data'
images = [os.path.join(root, filename)
for root, dirs, files in os.walk(folder)
for filename in files
if filename.lower().endswith('.png')]
ic = []
for img in images:
ic.append(io.imread(img).flatten())

Related

How to save tiff images into a new npy file?

I would like to save some tiff images I have into a new npy file.
My data are saved in 5 different files (tiff format). I want to access to each one of them, convert them in narray and then save them in a new npy file (for deep learning classification).
import numpy as np
from PIL import Image
import os
Data_dir = r"C:\Desktop\Université_2019_2020\CoursS2_Mosef\Stage\Data\Grand_Leez\shp\imagettes"
Categories = ["Bouleau_tiff", "Chene_tiff", "Erable_tiff", "Frene_tiff", "Peuplier_tiff"]
for categorie in Categories:
path = os.path.join(Data_dir, categorie) #path for each species
for img in os.listdir(path):
path_img = os.path.join(path,img)
im = Image.open(os.path.join(path_img)) #load an image file
imarray = np.array(im) # convert it to a matrix
imarray = np.delete(imarray, 3, axis=2)
np.save(Data_dir, imarray)
Problem: It's only return me the last observation of my last category "Peuplier_tiff", also it's saved into the name imagette, I don't know why.
Last but not least, I have a doubt for my targets, how I can be sure that my categories are correctly assign to the corresponding arrays.
A lot of questions,
thanks in advance for your help.
S.V
Thanks for your response. Its working with this code :
import numpy as np
from PIL import Image
import os
new_dir = "dta_npy"
directory = r"C:\Desktop\Université_2019_2020\CoursS2_Mosef\Stage\Data\Grand_Leez\shp\imagettes"
Data_dir = os.path.join(directory, new_dir)
os.makedirs(Data_dir)
print("Directory '%s' created" %Data_dir)
Categories = ["Bouleau_tif","Chene_tif", "Erable_tif", "Frene_tif", "Peuplier_tif"]
for categorie in Categories:
path = os.path.join(directory,categorie) #path for each species
for img in os.listdir(path):
im = Image.open(os.path.join(path,img)) #load an image file
imarray = np.array(im) # convert it to a matrix
imarray = np.delete(imarray, 3, axis=2)
unique_name=img
unique_name = unique_name.split(".")
unique_name = unique_name[0]
np.save(Data_dir+"/"+unique_name, imarray)
Now my objective is to format my data, for each of my class, in this way : (click on the link)
format goal

Processing and then saving images with a for loop in python

I'm new in programming and I'd like to ask you how can I write my code so that it read all the pic whitin a directory, process it one by one, and then save the output images in another directory.
%pylab
%matplotlib inline
import cv2
import glob
import os
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
filename = [img for img in glob.glob("outp/*.png")]
flist=sorted(filename)
images = []
for img in flist:
n = cv2.imread(img, 0)
images.append(n)
blur = cv.GaussianBlur(n, (9,9), 0)
cv.imwrite(flist, blur) #definitly wrong!! but idk how to do it
You don't need to create a list of all the pixel data of all the images in memory, that is just wasteful of memory. So, remove the lines:
images = []
and
images.apoend(...)
Then change your imwrite() to overwrite the current image:
cv2.imwrite(img,blur)
Here's a more complete version:
#!/usr/bin/env python3
import cv2
import glob
import os
import numpy as np
import cv2
# Go to where the images are
os.chdir('outp')
# Get list of filenames to convert
files = glob.glob("*.png")
for file in files:
n = cv2.imread(file, 0)
blur = cv2.GaussianBlur(n, (9,9), 0)
cv2.imwrite('blurred_' + file, blur)
Try this way:
cv2.imwrite("mypicture.jpg", gray[y:y+h,x:x+w])

Exporting image intensities from multiple files in multiple folders to excel in Python

I need to export lists of pixel intensities derived from multiple images in multiple folders to an excel spreadsheet. Each folder contains a list of tiff files each representing a certain time point in a timelapse. I've managed to obtain the pixel intensities of each folder subset, but I'm struggling with the output to excel using DataFrames with pandas. The data frame only displays the list of values from the last folder, and I need the spreadsheet to display each list in a separate row. Here is what I have:
import os
import matplotlib.pyplot as plt
import skimage.external.tifffile as tiff
import pandas as pd
from pandas import DataFrame
#to read images in each folder
def load_images_from_folder(folder):
images=[]
for filename in os.listdir(folder):
if any([filename.endswith(x) for x in ['.tif']]):
img=tiff.imread(os.path.join(folder, filename))
if img is not None:
images.append(img)
return images
folders = [
'path to folder1',
'path to folder2',
'path to folder3',
]
for folder in folders:
images=load_images_from_folder(folder)
#ratio the mean green to red signal in each image
ratios = [image[..., 1].mean() / image[..., 0].mean() for image in
images]
plt.plot(range(len(images)), ratios)
plt.show()
df=DataFrame({'Ratios':ratios})
df.to_excel('Ratios.xlsx', sheet_name='sheet1', index=0)
Printing out the ratios gives:
Folder1:
[list of values]
Folder2:
[list of values]
Folder3:
[list of values
etc.
But the data displayed by df (DataFrame) is only from the list in Folder3. So, what do I need to do differently to export values derived from multiple folders into Excel? I also made sure that each image is read as a ndarray and the type=uint8.
Replacing the assignment sentence inside the for loop by a call to the extend list method should fix the issue:
images = []
for folder in folders:
images.extend(load_images_from_folder(folder))
import tkinter
from tkinter import filedialog
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askdirectory
import os
import matplotlib.pyplot as plt
import skimage.external.tifffile as tiff
import pandas as pd
from pandas import DataFrame
import numpy as np
def load_images_from_folder(folder):
images=[]
for filename in os.listdir(folder):
if any([filename.endswith(x) for x in ['.tif']]):
img=tiff.imread(os.path.join(folder, filename))
if img is not None:
images.append(img)
return images
tkinter.Tk().withdraw()
dirname = askdirectory(initialdir="/", title='Please select a directory')
os.chdir(dirname)
data = pd.DataFrame([])
ratiodata = []
foldernames = []
for folder in os.listdir(dirname):
if not folder.endswith('.xlsx'):
images=load_images_from_folder(folder)
green=[image[..., 1].mean() for image in images]
red=[image[..., 0].mean() for image in images]
ratios= [img[..., 1].mean() / img[..., 0].mean() for img in images]
#PLOTS
temp = pd.DataFrame({folder + " ratios" : ratios})
data = pd.concat([data,temp],axis=1)
data.to_excel('test.xlsx', sheet_name='sheet1')

Read set of images into 4D Numpy array with dimension (num_img,channel, dim1, dim2)

I have a set of 1000 gray scale images (28x28), I want to read them into 4D numpy array (number of images, 1, img_dim1,img_dim2). Following is my code but it doesn't work properly. Any idea how I can fix the issue in the code?
from PIL import Image
import numpy as np
import os
mypath=os.path.dirname('path/to/directory/')
def load_dataset( ) :
data =np.zeros((1000,1,28,28), dtype=np.float64)
for fname in os.listdir(mypath):
pathname = os.path.join(mypath, fname)
img = Image.open(pathname)
data = np.dstack((data, img))
return data
data=load_dataset()
print(data.shape)
The problem has been solved by using append and adding a new axis np.newaxis
from PIL import Image
import numpy as np
import os
mypath=os.path.dirname('path/to/directory/')
def load_dataset( ) :
data =[]
for fname in os.listdir(mypath):
pathname = os.path.join(mypath, fname)
img = Image.open(pathname)
img1 = img[np.newaxis,:,:]
data.append(img1)
return data
data= load_dataset()
data_x=np.array(data)
print data_x.shape

Image to matrix using python

I am required to access all images in a folder and store it in a matrix. I was able to do it using matlab and here is the code:
input_dir = 'C:\Users\Karim\Downloads\att_faces\New Folder';
image_dims = [112, 92];
filenames = dir(fullfile(input_dir, '*.pgm'));
num_images = numel(filenames);
images = [];
for n = 1:num_images
filename = fullfile(input_dir, filenames(n).name);
img = imread(filename);
img = imresize(img,image_dims);
end
but I am required to do it using python and here is my python code:
import Image
import os
from PIL import Image
from numpy import *
import numpy as np
#import images
dirname = "C:\\Users\\Karim\\Downloads\\att_faces\\New folder"
#get number of images and dimentions
path, dirs, files = os.walk(dirname).next()
num_images = len(files)
image_file = "C:\\Users\\Karim\\Downloads\\att_faces\\New folder\\2.pgm"
im = Image.open(image_file)
width, height = im.size
images = []
for x in xrange(1, num_images):
filename = os.listdir(dirname)[x]
img = Image.open(filename)
img = im.convert('L')
images[:, x] = img[:]
but I am getting this error:
IOError: [Errno 2] No such file or directory: '10.pgm'
although the file is present.
I'm not quite sure what your end goal is, but try something more like this:
import numpy as np
import Image
import glob
filenames = glob.glob('/path/to/your/files/*.pgm')
images = [Image.open(fn).convert('L') for fn in filenames]
data = np.dstack([np.array(im) for im in images])
This will yield a width x height x num_images numpy array, assuming that all of your images have the same dimensions.
However, your images will be unsorted, so you may want to do filenames.sort().
Also, you may or may not want things as a 3D numpy array, but that depends entirely on what you're actually doing. If you just want to operate on each "frame" individually, then don't bother stacking them into one gigantic array.

Categories