How to read all types images from the folder regardless of file extension ?
42 views (last 30 days)
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
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.
Accepted Answer
Walter Roberson
on 31 Oct 2017
Directory = 'Images_folder';
% Read images from Images folder
Imgs = dir(Directory);
for j=1:length(Imgs)
thisname = Imgs(j).name;
thisfile = fullfile(Directory, thisname);
try
Img = imread(thisfile); % try to read image
Im = Img(:,:,1);
figure
imshow(Im)
title(thisname);
catch
end
end
Remember, image files are defined by their content, not by their extension, so the only sure way to determine if any given file can be read as an image is to try reading it as an image.
Myself, I would at the very least filter out directories before making the attempt, but I do not know Windows well enough to be certain that it would refuse to read a directory as an image.
This code is deficient in that it can only read the image types handled by MATLAB, not all types of images as you had asked for.
4 Comments
Walter Roberson
on 19 Jan 2022
For multiple folders you can use
Directory = 'Images_parent_folder';
% Read images from Images parent folder
Imgs = dir( fullfile(Directory, '*') );
for j=1:length(Imgs)
thisname = Imgs(j).name;
thisfile = fullfile(Imgs(folder), thisname);
try
Img = imread(thisfile); % try to read image
Im = Img(:,:,1); % was so in original code
figure
imshow(Im)
title(thisname);
catch
end
end
Image Analyst
on 19 Jan 2022
Try this for multiple files and multiple folders under a top level folder.
topLevelFolder = pwd; %'C:\Images_parent_folder';
% Read images from top level folder and also subfolders of it.
Imgs = dir( fullfile(topLevelFolder, '**\*') ); % Use ** - special wildcard added a few years ago.
fprintf('Found %d files.\n', length(Imgs))
% Throw out directories
itsAFolder = [Imgs.isdir];
Imgs(itsAFolder) = [];
fprintf('After throwing out folders, there are now %d files.\n', length(Imgs))
for k = 1 : length(Imgs)
thisBaseFileName = Imgs(k).name;
thisFolder = Imgs(k).folder;
fullFileName = fullfile(thisFolder, thisBaseFileName);
try
fprintf('Reading %s\n', fullFileName)
% Img = imread(thisfile); % try to read image
% Im = Img(:,:,1); % was so in original code
% figure
% imshow(Im)
% title(thisname);
% drawnow;
catch
end
end
More Answers (3)
KSSV
on 31 Oct 2017
Files=[dir('*.jpg');dir('*.png')]
specify all possible image extensions.
7 Comments
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}')
0 Comments
DGM
on 28 Apr 2023
Edited: DGM
on 5 Jul 2024
% 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.
0 Comments
See Also
Categories
Find more on Low-Level File I/O 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!