如何在 Ubuntu 上永久缩放 Image Magick++ 图像?
How to permanently scale an ImageMagick++ image on Ubuntu?
我有一个 ImageMagick++ 图像,我想缩放到我喜欢的任何宽度和高度,然后我想访问这个新缩放图像的像素。
我希望缩放操作永久改变图像。到目前为止,有四种 ImageMagick++ 方法听起来可以实现这种缩放操作:resize()、sample()、scale() 和 transform(),但我几乎可以使用的唯一方法是 sample()。
问题是,当我调用 tMyImage.sample() 然后读取像素值时,图像仅缩放到我传递给它的高度,然后根据原始宽高比设置宽度比率.
这是我使用的代码:
Magick::Image tMyIMage(iOriginalWidth, iOriginalHeight, "BGRA", Magick::CharPixel, (void *)pSourceData);
try { tImage.sample(Magick::Geometry(200, 50, 0, 0)); }
catch { Magick::Exception &eException)
{
// This never happens...
}
tImage.modifyImage();
size_t nNewWidth = tImage.columns();
size_t nNewHeight = tImage.rows();
// At this point I would expect that nNewWidth is 200 and nNewHeight is 50
// Instead, nNewHeight is 50 but nNewWidth is scaled according to the original aspect ratio
// Note here that tImage.baseColumns() and tImage.baseRows() both return 0
tImage.type(Magick::TrueColorType);
const Magick::PixelPacket *pPixels = tImage.getConstPixels(0, 0, nNewWidth, nNewHeight);
// Here I would expect that pPixels points to pixel data of a 200x50 pixel image
我是 ImageMagick++ 的新手,所以我确定我一定是做错了什么。任何帮助是极大的赞赏!谢谢!
我找到了缩放不起作用的原因;显然,ImageMagick++ 中的默认设置是在生成的宽高比发生变化时不采用您正在使用的确切尺寸 -_-
在创建几何图形时,您必须明确告诉 ImageMagick++ 采用准确的尺寸:
char szGeometry[64];
memset(&(szGeometry[0]), 0, sizeof(szGeometry));
snprintf(szGeometry, sizeof(szGeometry) - 1, "%ix%i!", iWidth, iHeight);
Magick::Image tScaled(iOriginalWidth, iOriginalHeight, "BGRA", Magick::CharPixel, (void *)pSourceData);
try { tScaled.sample(Magick::Geometry(szGeometry)); }
catch (Magick::Exception &eException)
{
...
}
我有一个 ImageMagick++ 图像,我想缩放到我喜欢的任何宽度和高度,然后我想访问这个新缩放图像的像素。
我希望缩放操作永久改变图像。到目前为止,有四种 ImageMagick++ 方法听起来可以实现这种缩放操作:resize()、sample()、scale() 和 transform(),但我几乎可以使用的唯一方法是 sample()。
问题是,当我调用 tMyImage.sample() 然后读取像素值时,图像仅缩放到我传递给它的高度,然后根据原始宽高比设置宽度比率.
这是我使用的代码:
Magick::Image tMyIMage(iOriginalWidth, iOriginalHeight, "BGRA", Magick::CharPixel, (void *)pSourceData);
try { tImage.sample(Magick::Geometry(200, 50, 0, 0)); }
catch { Magick::Exception &eException)
{
// This never happens...
}
tImage.modifyImage();
size_t nNewWidth = tImage.columns();
size_t nNewHeight = tImage.rows();
// At this point I would expect that nNewWidth is 200 and nNewHeight is 50
// Instead, nNewHeight is 50 but nNewWidth is scaled according to the original aspect ratio
// Note here that tImage.baseColumns() and tImage.baseRows() both return 0
tImage.type(Magick::TrueColorType);
const Magick::PixelPacket *pPixels = tImage.getConstPixels(0, 0, nNewWidth, nNewHeight);
// Here I would expect that pPixels points to pixel data of a 200x50 pixel image
我是 ImageMagick++ 的新手,所以我确定我一定是做错了什么。任何帮助是极大的赞赏!谢谢!
我找到了缩放不起作用的原因;显然,ImageMagick++ 中的默认设置是在生成的宽高比发生变化时不采用您正在使用的确切尺寸 -_- 在创建几何图形时,您必须明确告诉 ImageMagick++ 采用准确的尺寸:
char szGeometry[64];
memset(&(szGeometry[0]), 0, sizeof(szGeometry));
snprintf(szGeometry, sizeof(szGeometry) - 1, "%ix%i!", iWidth, iHeight);
Magick::Image tScaled(iOriginalWidth, iOriginalHeight, "BGRA", Magick::CharPixel, (void *)pSourceData);
try { tScaled.sample(Magick::Geometry(szGeometry)); }
catch (Magick::Exception &eException)
{
...
}