ImageMagick API - 导出具有正确方向的像素

时间:2014-10-03 13:20:14

标签: imagemagick magickwand

我一直在使用此方法使用MagickWand API导出像素:

MagickExportImagePixels

但是,这似乎不遵守图像中的方向exif数据。无论如何以正确的方向提取像素,所以我可以用不同的格式写出它们(不使用MagickWriteImage)?基本上,我想要转换的自动定向选项的行为。

谢谢!

3 个答案:

答案 0 :(得分:2)

我是ImageMagick的开发人员之一,似乎我们忘了将此方法添加到Wand中。 ImageMagick的下一个版本(6.8.9-9)将包含以下可用于自动定向图像的方法:

MagickAutoOrientImage(magick_wand);

答案 1 :(得分:1)

当带有MagickAutoOrientImage()的6.8M+版本的ImageMagick不可用时,我使用以下代码自动定位图像:

void auto_orient_image(MagickWand* image) {
  PixelWand* pwand=0;
  switch(MagickGetImageOrientation(image))
  {
    case UndefinedOrientation:
    case TopLeftOrientation:
    default:
      break;
    case TopRightOrientation:
      MagickFlopImage(image);
      break;
    case BottomRightOrientation:
      pwand=NewPixelWand();
      MagickRotateImage(image, pwand, 180.0);
    case BottomLeftOrientation:
      MagickFlipImage(image);
      break;
    case LeftTopOrientation:
      MagickTransposeImage(image);
      break;
    case RightTopOrientation:
      pwand=NewPixelWand();
      MagickRotateImage(image, pwand, 90.0);
      break;
    case RightBottomOrientation:
      MagickTransverseImage(image);
      break;
    case LeftBottomOrientation:
      pwand=NewPixelWand();
      MagickRotateImage(image, pwand, 270.0);
      break;
  }
  if (pwand) DestroyPixelWand(pwand);
  MagickSetImageOrientation(image, TopLeftOrientation);
}

可能需要添加错误处理。

答案 2 :(得分:0)

看起来这样做的唯一方法是伪手动,方法是获取图像方向并相应地旋转图像。这并不困难,但我希望API内置更简洁的解决方案。一个不完整的例子如下:

OrientationType t = MagickGetImageOrientation(magick_wand);
if (t == RightTopOrientation) {
    MagickRotateImage(mw, pw, 90);
}
...