以下尺寸为1x9的图像被修剪为1x6,因为可能顶部的像素与底部像素和修剪函数的颜色相同,这些像素被识别为背景颜色,即使在执行trim函数之前报告的backgroundColor是#FFFFFF。
http://s1.postimage.org/a7r69yxsr/m_medium_bc.png
我唯一要做的就是在Image上执行trim。明确设置backgroundColor和/或transparent()没有区别。
以下是代码:
Magick::Image tempImage;
tempImage.read(name);
std::cout<<"size:"<<tempImage.columns()<<","<<tempImage.rows()<<std::endl;
temp=tempImage.backgroundColor();
std::cout<<"bg:"<<(std::string)temp<<std::endl;
tempImage.trim();
std::cout<<"size:"<<tempImage.columns()<<","<<tempImage.rows()<<std::endl;
答案 0 :(得分:0)
我同意这种行为很奇怪,我不是ImageMagick / Magick ++的开发者/维护者,因此无法进一步评论这是一个错误还是一个&#39;功能&#39;。但是我遇到了同样的问题并创建了这个函数作为一种解决方法(注意这比手动迭代像素快得多,即使有一个像素缓存):
Magick::Geometry CalculateImageMagickBoundingBox( const Magick::Image & image, const Magick::Color & borderColor )
{
// Clone input image.
Magick::Image clone( image );
// Remember original image size.
const Magick::Geometry originalSize( image.columns( ), image.rows( ) );
// Extend geometry by two in width and height (one pixel border).
Magick::Geometry extendedSize( originalSize.width( ) + 2, originalSize.height( ) + 2 );
// Extend cloned canvas (center gravity so 1 pixel border of user specified colour).
clone.extent( extendedSize, borderColor, Magick::CenterGravity );
// Calculate bounding box (will use border colour, which we have set above).
Magick::Geometry boundingBox = clone.boundingBox( );
// We added 1 pixel border, so subtract this now.
boundingBox.xOff( boundingBox.xOff( ) - 1 );
boundingBox.yOff( boundingBox.yOff( ) - 1 );
// Clamp (required for cases where entire image is border colour, and therefore the right/top borders
// that we added are taken into account).
boundingBox.width( std::min( boundingBox.width( ), originalSize.width( ) ) );
boundingBox.height( std::min( boundingBox.height( ), originalSize.height( ) ) );
// Return bounding box.
return boundingBox;
}
在您的特定情况下,您可以使用此功能,然后根据返回的几何图形设置画布大小。