copying multiple images from one folder to other folder with conditions

12 ビュー (過去 30 日間)
Hong Thien Dang
Hong Thien Dang 2021 年 8 月 29 日
コメント済み: TADA 2021 年 8 月 29 日
Dear Matlab Users
I have a folder with 14 pictures (C:\DT3\Pictures), i need to selected picture smaller than 512 x 512 pixel and copy it to SmallPicture.txt folder.
Thanks for the help!

採用された回答

Walter Roberson
Walter Roberson 2021 年 8 月 29 日
編集済み: Walter Roberson 2021 年 8 月 29 日
sourcefolder = 'C:\DT3\Pictures';
destfolder = 'C:\DT3\Pictures\SmallPicture';
if ~isfolder(destfolder); mkdir(destfolder); end
dinfo = dir(sourcefolder);
dinfo([dinfo.isdir]) = []; %remove subfolders and . and ..
filenames = fullfile({dinfo.folder}, {dinfo,name});
nfiles = length(filenames);
for K = 1 : nfiles
thisfile = filenames{K};
try
info = imfinfo(thisfile);
if ~isempty(info) && isfield(info, 'Width') && isfield(info,'Height') && info.Width <= 512 && info.Height <= 512
copyfile(thisfile, destfolder);
end
catch ME
%not an image, or headers not readable or copyfile failed
end
end

その他の回答 (1 件)

TADA
TADA 2021 年 8 月 29 日
use dir to loop through all files in the folder you mentioned.
use imfinfo to check the width and height of each image file in that folder.
use copyfile to copy the relevant files and mkdir if needed to create the SmallPicture directory
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 8 月 29 日
... all of which I do in my Answer.
TADA
TADA 2021 年 8 月 29 日
Sir Roberson, your answer was posted about 2 hours ago, same as mine. Obviously, I wrote this answer while you wrote yours, hence i missed it. Honestly, this happens quite often and I fail to see why you reproach or tease me on that matter.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by