Page 1 of 1
Read images from a folder in c++
Posted: 2008-08-14T11:10:05-07:00
by imagetester
Hello, i need a c++ code to retrieve images from a specific folder (knowing it's directory) and to store the images in a vector using c/c++.
In other words i need to be able to create a loop and to do vect.push_back(image).
For a given vector<Image> vect.
If it is too difficult just give me a way to read the images from a folder and i'll format it later.
Thanks in advance
Re: Read images from a folder in c++
Posted: 2008-08-14T17:38:50-07:00
by anthony
That is a C++ system thing. and nothing to do with IM itself.
However generally you do a opendir() system call, then loop on a readdir(), and finish with a closedir(). Check your man pages and C++ manuals (and web guides) for more information.
Re: Read images from a folder in c++
Posted: 2008-08-15T07:14:48-07:00
by imagetester
Yes indeed i believe that's pure c++.
Anyway I've been doing some documentation and i think this may be very interesting and should be included with the Magick++ lib
Code: Select all
#include <Magick++.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <shlobj.h>
using namespace Magick;
using namespace std;
int main(int argc,char * argv[]){
string path = "C:\\Program Files\\ImageMagick-6.4.2-Q16\\Magick++_Demo\\dir\\";
string searchPattern = "*.jpg";
string fullSearchPath = path + searchPattern;
WIN32_FIND_DATA FindData;
HANDLE hFind;
hFind = FindFirstFile( fullSearchPath.c_str(), &FindData );
if( hFind == INVALID_HANDLE_VALUE )
{
cout << "Error searching directory\n";
return -1;
}
do
{
string filePath = path + FindData.cFileName;
try{
// do stuff with the file here
cout<<filePath.c_str()<<endl;
Image im;
im.read(filePath.c_str());
im.write("c:\\Program Files\\ImageMagick-6.4.2-Q16\\hi.jpg");
}
catch(...){
cerr<<"Unable to do stuff with the file\n";
}
}
while( FindNextFile(hFind, &FindData) > 0 );
if( GetLastError() != ERROR_NO_MORE_FILES )
{
cout << "Something went wrong during searching\n";
}
system("pause");
return 0;
}