Translate CLI ImageMagick to Magick++: threshold and depth

允我心安 提交于 2021-02-11 15:20:49

问题


I am converting a system call to Magick++ in my code, but I am having some trouble converting the threshold and depth.

The original:

convert /foo/bar.ppm -crop WxH+X+Y -threshold 50% -depth 1 /foo/out.ppm

My current C++ version is:

Magick::InitializeMagick(*argv);
Magick::Image img;
img.read("/foo/bar.ppm");
Magick::Image temp_img(img);
temp_img.chop(Magick::Geometry(X,Y);
temp_img.crop(Magick::Geometry(W,H));
temp_img.threshold(.50);
temp_img.depth(1);
temp_img.write("/foo/out.ppm");

The chop and crop behaves like I expect, but the rest does not. The threshold and depth commands take a double and a size_t, respectively. So what I have written in there seems like it would work. However, if either one of these lines are enabled, the result image comes out nearly all white.

Is there a more correct way of doing this?


回答1:


Mark Setchell's comment is correct. Maigck::Image.threshold's argument must be scaled by the QuantumRange (provided by a C macro definition).

temp_img.threshold(QuantumRange * 0.5);

This scaling is expected for most arguments that are a percent ratio.



来源:https://stackoverflow.com/questions/63125871/translate-cli-imagemagick-to-magick-threshold-and-depth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!