Read images from a folder in c++

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
imagetester

Read images from a folder in c++

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Read images from a folder in c++

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
imagetester

Re: Read images from a folder in c++

Post 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;
}
Post Reply