简体   繁体   English

是否有用于加载图像文件夹的 Magick++ function?

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

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.我一直在将 Magick++ 用于与图像相关的程序,但我一直无法找到一种方法来从文件夹中批量加载图像并进行处理。

If there is not a way to do this within the library is there a way to do this with <filesystem> ?如果在库中没有办法做到这一点,有没有办法用<filesystem>做到这一点? (By getting the filenames within a directory?) (通过获取目录中的文件名?)

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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM