2

我正在尝试加载-> 直接操作字节数组-> 保存 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

但是没有灰度 8 位像素类型。

4

1 回答 1

4

1.0.0-beta0005 开始,没有 Gray8 像素格式,因为我们无法决定在从 Rgb 转换时使用什么颜色模型(我们在内部需要它)。ITU-R Recommendation BT.709 似乎是明智的解决方案,因为这是 png 支持的内容,也是我们在将图像保存为 8 位灰度 png 时使用的内容,因此它在我的 TODO 列表中。

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

笔记。默认情况下,png 编码器会将图像保存为输入颜色类型和位深度。如果您想以不同的颜色类型对图像进行编码,您将需要新建一个具有和属性集的PngEncoder实例。ColorTypeBitDepth

于 2018-09-24T14:56:06.610 回答