gpt4 book ai didi

c++ - 尝试使用 CImg 保存图像时出错

转载 作者:行者123 更新时间:2023-11-28 05:07:44 27 4
gpt4 key购买 nike

我正在尝试从我使用 libnoise 改编的世界生成器生成 map 图 block 。使用 libnoise 保存方法效果很好,但是当尝试使用 CImg 缩放和裁剪图像时,出现以下错误:

[CImg] *** CImgIOException *** cimg::fopen(): Failed to open file '~/tiles/0/0/0.bmp' with mode 'wb'.

相关方法:

void makeTile(CImg<unsigned char> image, int zoom, int x, int y, int depth, int argc, char* argv[]) {
std::string directoryBase = "mkdir -p ~/tiles/" + boost::lexical_cast<std::string>(zoom);
std::string directory = directoryBase + "/" + boost::lexical_cast<std::string>(x);
std::string filename = "~/tiles/" + boost::lexical_cast<std::string>(zoom) + "/" + boost::lexical_cast<std::string>(x) + "/" + boost::lexical_cast<std::string>(y) + ".bmp";

const char* file_o = cimg_option("-o", filename.c_str(), "Output file");
system(directoryBase.c_str());
system(directory.c_str());
std::cout << "Made required dirs\n";
CImg<unsigned char> imageClone = image.get_resize(256, 256, -100, -100, 1);

std::cout << "scaled image\n";
imageClone.save(file_o);

if (depth > 0) {
int smallX = x * 2;
int smallY = y * 2;
makeTile(image.get_crop(0, 0, image._width / 2, image._height / 2), zoom + 1, smallX, smallY, depth - 1, argc, argv);
makeTile(image.get_crop(image._width / 2, 0, image._width, image._height / 2), zoom + 1, smallX + 1, smallY, depth - 1, argc, argv);
makeTile(image.get_crop(0, image._height / 2, image._width / 2, image._height), zoom + 1, smallX, smallY + 1, depth - 1, argc, argv);
makeTile(image.get_crop(image._width / 2, image._height / 2, image._width, image._height), zoom + 1, smallX + 1, smallY + 1, depth - 1, argc, argv);
}
}

在第一个循环中发送到此方法的图像是从 libnoise 生成的 map 并使用 CImg 加载。

最佳答案

~/tiles/0/0/0.bmp 绝对不是有效的文件名。我怀疑您认为 ~ 会被您的 $HOME 的值替换,但事实并非如此(这种替换仅在编写 shell 命令时完成,例如bash).

关于c++ - 尝试使用 CImg 保存图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com