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



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

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

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

所以......目前你需要在解码图像时使用Rgb24Rgba32

更新。

从 1.0.0-dev002094 开始,这现在可以了!我们有两种新的像素格式。 仅携带像素亮度分量的Gray8Gray16

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 编码器将以输入颜色类型和位深度保存图像。如果要以不同的颜色类型对图像进行编码,则需要使用ColorTypeBitDepth属性集新建一个PngEncoder实例。

最新更新