I'm using it in a loop (sometimes, thousand of images) then I get a "Too many files open" error eventually. It only happens when I'm reading .webp files.
Code: Select all
#include <exception>
#include <iostream>
#include <boost/filesystem.hpp>
#include <Magick++.h>
using namespace std;
namespace fs = boost::filesystem;
int convert(std::string inpath, std::string outpath, std::string libpath) {
Magick::InitializeMagick(libpath.c_str());
Magick::Image i;
try {
i.read(inpath);
if (i.isValid()) {
fs::path p = outpath;
p.replace_extension(".png");
i.debug(false);
i.quality(100);
i.write(p.string());
} else {
cerr << inpath << " is not a valid image.\n";
}
} catch (std::exception &e) {
cerr << "Error: " << e.what() << "\n";
return 1;
}
Magick::TerminateMagick();
return 0;
}
int main(int argc, char** argv) {
string srcDir = argv[1];
string libpath = argv[2];
fs::directory_iterator srcIt(srcDir), endSrcIt;
for (; srcIt != endSrcIt; ++srcIt) {
fs::path filePath = (*srcIt).path();
try{
convert(filePath.string(), filePath.string(), libpath);
} catch(std::exception &e){
std::cout<< e.what();
return 1;
}
}
return 0;
}