How to read all types images from the folder regardless of file extension ?
Show older comments
The below is my code
clear all
close all
clc
Directory = 'Images_folder\';
% Read images from Images folder
Imgs = dir(fullfile(Directory,'*.bmp'));
for j=1:length(Imgs)
Img = imread(fullfile(Directory,Imgs(j).name)); % Read image
Im = Img(:,:,1);
figure
imshow(Im)
end
From this code I can read only .bmp images.But I want to read all types of images from the folder regardless of the file extension.
6 Comments
pavani
on 26 Apr 2023
Hi i need how to read all types of images from directory like jpg, jpeg.
Image Analyst
on 26 Apr 2023
img_dir = '\New folder (2)\';
File_list = [...
dir('*.jpg');
dir('*.jpeg');
dir('*.png');
dir('*.bmp');
dir('*.tif');
dir('*.dcm');
];
fileNames = sort({File_list.name}');
for i = 1:length(file_list)
% get the file name
filename = [img_dir File_list(i).name];
% read the image
img = imread(filename);
Y = rgb2gray(img);
end
i tried this. but it didn't worked for me. In my folder i am having png, jpg,jpeg images. I want to read all these images using for loop. but it is getting error like
Error using imread>get_full_filename
File "\New folder (2)\II.jpg" does not exist.
Error in imread (line 372)
fullname = get_full_filename(filename);
Stephen23
on 28 Apr 2023
@pavani: you are telling IMREAD to look in a specific directory, but you are not telling DIR to look in the same directory... so by default it will look in the current directory. Most likely the filenames differ between those two directories, thus the error.
The solution is to give both DIR and IMREAD the same directory path. You should also use FULLFILE rather than concatenating strings together:
P = '\New folder (2)';
S = [... You need to give DIR the same directory path:
dir(fullfile(P,'*.jpg'));
dir(fullfile(P,'*.jpeg'));
dir(fullfile(P,'*.png'));
dir(fullfile(P,'*.bmp'));
dir(fullfile(P,'*.tif'));
dir(fullfile(P,'*.dcm'));
];
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
I = imread(F);
Y = rgb2gray(I);
end
Walter Roberson
on 28 Apr 2023
img_dir = '\New folder (2)\';
That is an absolute directory name relative to the "current" drive (probably C:) .
It is likely that C:\New folder (2) does not exist.
pavani
on 27 Sep 2023
Thank you. This code is working.
Accepted Answer
More Answers (3)
KSSV
on 31 Oct 2017
Files=[dir('*.jpg');dir('*.png')]
specify all possible image extensions.
7 Comments
Thilak Babu Sayila
on 20 Mar 2019
it's not working!
KSSV
on 20 Mar 2019
It is working in my case...what you have tried?
Thilak Babu Sayila
on 20 Mar 2019
only jpeg images are reading, png images are not reading
Thilak Babu Sayila
on 20 Mar 2019
if you dont mind, can you give me full code?
Walter Roberson
on 20 Mar 2019
what happened when you tried the code in my answer ?
Thilak Babu Sayila
on 21 Mar 2019
actually, total 20 images are in my folder, after executing the code 'Imgs' contains 22 contents and extra two are dummy (nothing is present). but i need exactly 20 images of various formats.
Walter Roberson
on 21 Mar 2019
In my code, Imgs (capital I) will contain directory information for everything in the folder, including the . and .. folders. It is the loop with the try/catch that determines whether each entry is an image file that MATLAB is able to deal with, so you should record the filename information for those cases.
It is easier when there are a list of specific extensions you want to be able to handle, instead of wanting "everything that MATLAB turns out to be able to process" no matter how obscure.
Image Analyst
on 19 Jan 2022
For multiple file extensions in a single folder, try this:
fileList = [...
dir('*.jpg');
dir('*.jpeg');
dir('*.png');
dir('*.bmp');
dir('*.tif');
dir('*.dcm');
]
fileNames = sort({fileList.name}')
% read all the images in my giant pile of forum garbage
% includes JPG, GIF, TIFF, PNG, BMP, ICO, MPO, WEBP
% amongst hundreds of non-image files
% includes I, IA, RGB, RGBA, logical, indexed, and multiframe images
ST = mimread('forum_junk/*','quiet'); % one line
ST is a cell array containing all of the images.
- If a JPG image has EXIF orientation data, it will be rectified.
- Multiframe GIFs will be read as a multiframe 4D RGB image
- Multi-image TIFF/CUR/ICO files will also be read in whole
- Single-frame WEBP images are supported
- If an image is indexed color, it is converted to RGB.
- If an image has transparent content, it will have an attached alpha channel (IA/RGBA).
Is it a good idea to blindly load perhaps hundreds of images into memory simultaneously? Possibly not, but since the original question is about how to create an unmanageable forest of figure windows, I figured that good ideas weren't a requirement.
Jokes aside, mimread() isn't the most flexible thing to use if you want to have a lot of control over managing the path expression or if you have a bunch of elaborate TIFFs or something. It's also not useful if you want to incrementally process the files one at a time. It's intended to be a simple-to-use tool for ad-hoc use. You just point it at a directory or a bunch of easily-isolated filenames and grab them.
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!