【问题标题】:Magick++ trimming pixels that are not of backgroundColor (pixels same color on top and bottom of image)Magick++ 修剪不属于背景颜色的像素(图像顶部和底部的像素颜色相同)
【发布时间】:2013-01-14 22:29:20
【问题描述】:

以下尺寸为 1x9 的图像正在被修剪为 1x6,因为可能顶部的像素与底部的像素颜色相同,并且在修剪功能中,这些像素被识别为背景颜色,即使在执行修剪函数之前报告的背景颜色是#FFFFFF。

http://s1.postimage.org/a7r69yxsr/m_medium_bc.png

我唯一要做的就是对图像执行修剪。显式设置 backgroundColor 和/或 transparent() 没有区别。

  1. 为什么会发生这种情况?这是预期的行为吗?
  2. 可以通过配置/属性设置/不更改 Graphicsk 库代码来解决此问题吗?
  3. 如果没有,什么时候可以修复这个错误?您是否希望在接下来的几天内修复这种性质的错误?

代码如下:

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;

【问题讨论】:

  • 你能发布你正在使用的确切命令吗?
  • 这发生在GraphicsMagick的GetBoundingBox(我的头顶的名字?)函数中,并且可以相对容易地修复。该功能的结果就其意图/用途而言是特殊的。它将图像角落的颜色视为背景颜色。

标签: c++ graphicsmagick magick++


【解决方案1】:

我同意这种行为很奇怪,我不是 ImageMagick/Magick++ 的开发人员/维护人员,因此无法进一步评论这是错误还是“功能”。但是我遇到了同样的问题并创建了这个函数作为一种解决方法(注意这比手动迭代像素要快得多,即使有像素缓存也是如此):

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;
}

在您的特定情况下,您可以使用此函数,然后根据返回的几何图形设置画布大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2017-10-13
    • 2021-09-09
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多