Is there a Magick++ function for loading a folder of images?

91 Views Asked by At

I have been using Magick++ for image-related programs but I haven't been able to find a way to bulk load images from a folder and process them.

If there is not a way to do this within the library is there a way to do this with <filesystem>? (By getting the filenames within a directory?)

1

There are 1 best solutions below

1
user17041628 On

I believe I have found a solution but be careful, I am a beginner, the below code may not be up to standards. If there is a better and more modular way of doing this, please share.

#include <string>
#include <iostream>
#include <filesystem>
#include <Magick++.h>

using namespace Magick;
namespace fs = std::filesystem;

int main(int argc,char **argv) {
    InitializeMagick(*argv);
    //sample number
    Image image[10];

    std::string doc = fs::current_path();
    int i = 0;

    for (const auto & entry : fs::directory_iterator(doc)){
        try{
        i++;
        std::cout << entry.path() << std::endl;
        image[i].read(entry.path());
        image[i].display();
        }
        //These below exceptions occur if the file is not an image, it will skip if the file is not an image
        catch(Magick::ErrorMissingDelegate &error){
            continue;
        }
        catch(Magick::ErrorCorruptImage &error){
            continue;
        }
    }


}