【问题标题】:imagemagick : saving MagickExportImagePixels's output blob to gray image file?imagemagick:将 MagickExportImagePixels 的输出 blob 保存到灰色图像文件?
【发布时间】:2018-03-14 03:22:23
【问题描述】:

zbar 引擎示例源 (zbarimg.c) 显示以下内容。

https://github.com/ZBar/ZBar/blob/master/zbarimg/zbarimg.c

size_t bloblen = width * height;
unsigned char *blobdata = malloc(bloblen);
MagickExportImagePixels(images, 0, 0, width, height, "I", CharPixel, blobdata);

我想查看 blobdata。 如何将 blobdata 保存到文件中?

我创建了 save_imgdata 函数来保存 blobdata。

int save_imgdata(char* imgf, int width, int height, char *raw)
{
    PixelWand *p_wand = NULL;
    PixelIterator *iterator = NULL;
    PixelWand **pixels = NULL;
    unsigned long x, y;
    char hex[128];

    //MagickWandGenesis();
    p_wand = NewPixelWand();
    PixelSetColor(p_wand, "gray");
    //PixelSetColor(p_wand, "white");
    MagickWand *m_wand = NewMagickWand(); //CORE_RL_wand_.lib;
    MagickSetImageDepth(m_wand, 8);
    MagickNewImage(m_wand, width, height, p_wand);
    // Get a new pixel iterator 
    iterator = NewPixelIterator(m_wand);
    for (y = 0; y<height; y++) {
        // Get the next row of the image as an array of PixelWands
        pixels = PixelGetNextIteratorRow(iterator, &x);
        // Set the row of wands to a simple gray scale gradient
        for (x = 0; x<width; x++) {
            sprintf(hex, "#%02x", *raw++);
            //sprintf(hex, "#%02%x02%x02x", *raw, *raw, *raw); raw++;
            PixelSetColor(pixels[x], hex);
        }
        // Sync writes the pixels back to the m_wand
        PixelSyncIterator(iterator);
    }
    MagickWriteImage(m_wand, imgf);
    DestroyMagickWand(m_wand);
    return 0;
}

save_imgdata("imgw.bmp", width, height, blobdata)的调用 保存 24bpp 图像。

save_imgdata 有什么问题? 我希望它保存 8bpp 灰度图像文件。

【问题讨论】:

    标签: imagemagick zbar


    【解决方案1】:

    不必费心迭代和构建动态颜色/像素值——这很慢而且需要大量资源。如果数据来自 export 方法,则使用 import 方法来恢复。

    int save_imgdata(char* imgf, int width, int height, void * raw)
    {
        MagickWand * wand;
        PixelWand * bgcolor;
    
        bgcolor = NewPixelWand();
        PixelSetColor(bgcolor, "WHITE");
        wand = NewMagickWand();
        MagickNewImage(wand, width, height, bgcolor);
        bgcolor = DestroyPixelWand(bgcolor);
        MagickSetImageDepth(wand, 8);
        MagickSetImageColorspace(wand, GRAYColorspace);
        MagickImportImagePixels(wand, 0, 0, width, height, "I", CharPixel, raw);
        MagickQuantizeImage(wand,
                            256,             // Reduce to 8bpp
                            GRAYColorspace,  // Match colorspace
                            0,               // Calculate optimal tree depth
                            MagickTrue,      // Use dither ? This changes in IM-7
                            MagickFalse);    // Messure Error
        MagickWriteImage(wand, imgf);
        wand = DestroyMagickWand(wand);
        return 0;
    }
    

    【讨论】:

    • 非常感谢。我在 MagickExportImagePixels 之后添加了 save_imgdata("mgf.bmp", width, height, blob) 和你的 save_imgdata。它保存 24 bpp 位图文件。我不明白为什么imagemagick保存24bpp?输入图像为24bpp或8bpp或1bpp时,节省24bpp。
    • 使用 MagickQuantizeImage 将颜色从 24 减少到 8bpp。这与使用文档中建议的-colors 256 相同。我将更新答案中的示例。
    • 很好,效果很好。 MagickExportImagePixels 总是导出 24bp 吗?
    • 没有。 24bpp 是bmp 编码格式的默认值。 Usage documentation 中有一些概述。
    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2010-11-23
    • 2015-01-02
    相关资源
    最近更新 更多