代码之家  ›  专栏  ›  技术社区  ›  Red Riding Hood

使用ImageSharp加载并保存不透明的8位PNG文件

  •  1
  • Red Riding Hood  · 技术社区  · 6 年前

    我正在尝试加载->直接操作字节数组->保存8位png图像。

    我想使用ImageSharp将其速度与我当前的库进行比较,但是在他们的代码示例中,他们需要定义像素类型(他们使用Rgba32):

    using SixLabors.ImageSharp;
    using SixLabors.ImageSharp.Processing;
    
    // Image.Load(string path) is a shortcut for our default type. 
    // Other pixel formats use Image.Load<TPixel>(string path))
    using (Image<Rgba32> image = Image.Load("foo.jpg"))
    {
        image.Mutate(x => x
             .Resize(image.Width / 2, image.Height / 2)
             .Grayscale());
        image.Save("bar.jpg"); // Automatic encoder selected based on extension.
    }
    

    我查看了像素类型: https://github.com/SixLabors/ImageSharp/tree/master/src/ImageSharp/PixelFormats

    1 回复  |  直到 6 年前
        1
  •  4
  •   James South    6 年前

    截至 1.0.0-beta0005 没有Gray8像素格式,因为我们无法决定从Rgb转换时使用什么颜色模型(我们内部需要)。ITU-R建议BT.709似乎是一个明智的解决方案,因为这是png支持的,也是我们在将图像保存为8位灰度png时使用的,所以它在我的待办事项列表中。

    https://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale

    所以。。。目前您需要使用 Rgb24 Rgba32 解码图像时。

    更新。

    截至 1.0.0-dev002094 现在这是可能的!我们有两种新的像素格式。 Gray8 Gray16 只携带像素亮度分量的。

    using (Image<Gray8> image = Image.Load<Gray8>("foo.png"))
    {
        image.Mutate(x => x
             .Resize(image.Width / 2, image.Height / 2));
    
        image.Save("bar.png");
    }
    

    PngEncoder 实例与 ColorType BitDepth 属性集。

    推荐文章