简体   繁体   中英

How to give folder of images as input to Magick++ api?

I am need of passing a folder of images as input to Magick++ api. It can be done using mogrify in commandline as shown in post " ImageMagick script to resize folder of images ". Reading a single image could be done through api call as

Image image(inputimage)

But how could we do the same for a folder of images? Can anyone help me with the respective api call?

That feature is not included in the Magick++ API. You will need to iterate the directory yourself and then use the Magick++ API to read and write the image. You can find an example on how to iterate through a folder in C/C++ in the following Stack Overflow post: How can I get the list of files in a directory using C or C++? .

I believe you would be responsible of reading the directory. The C library dirent.h is the first thing I think of, but I'm sure there's better C++/system/framework techniques.

#include <iostream>
#include <vector>
#include <dirent.h>
#include <Magick++.h>

int main(int argc, const char * argv[]) {

    std::vector<Magick::Image> stack;           // Hold images found
    DIR * dir_handler = opendir("/tmp/images"); // Open dir
    struct dirent * dir_entry;

    if (dir_handler != NULL)
    {
        // Iterate over entries in directory
        while ( (dir_entry = readdir(dir_handler)) != NULL ) {
            // Only act on regular files
            if (dir_entry->d_type == DT_REG) {
                // Concatenate path (could be better)
                std::string filename("/tmp/images/");
                filename += dir_entry->d_name;
                // Read image at path
                stack.push_back(Magick::Image(filename));
            }
        }
        closedir(dir_handler); // House keeping
    } else {
        // Handle DIR error
    }

    // Append all images into single montage
    Magick::Image output;
    Magick::appendImages(&output, stack.begin(), stack.end());
    output.write("/tmp/all.png");

    return 0;
}

There's also ExpandFilenames(int *,char ***) in the MagickCore library.

// Patterns to scan
int pattern_count = 1;
// First pattern
char pattern[PATH_MAX] = "/tmp/images/*.png";
// Allocate memory for list of patterns
char ** dir_pattern = (char **)MagickCore::AcquireMagickMemory(sizeof(char *));
// Assign first pattern
dir_pattern[0] = pattern;
// Expand patterns
Magick::MagickBooleanType ok;
ok = MagickCore::ExpandFilenames(&pattern_count, &dir_pattern);

if (ok == Magick::MagickTrue) {
    std::vector<Magick::Image> stack;
    // `pattern_count' now holds results count
    for ( int i = 0; i < pattern_count; ++i) {
        // `dir_pattern' has been re-allocated with found results
        std::string filename(dir_pattern[i]);
        stack.push_back(Magick::Image(filename));
    }
    Magick::Image output;
    Magick::appendImages(&output, stack.begin(), stack.end());
    output.write("/tmp/all.png");
} else {
    // Error handle
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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