I have a byte[]
and I want to create a gray-scale image using ImageSharp library.
That is how I am doing it currently:
byte[] arr = MnistReader.ReadTestData(); //this gives me byte[] with 784 pixel values
ImageSharp.Image image = new ImageSharp.Image(28, 28);
for (int i = 0; i < image.Height; i++)
{
for (int j = 0; j < image.Width; j++)
{
int curPixel = j + i * image.Width;
image.Pixels[curPixel].R = arr[curPixel];
image.Pixels[curPixel].G = arr[curPixel];
image.Pixels[curPixel].B = arr[curPixel];
}
}
I wonder if there is a more elegant way to do it ?
Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter). Finally, Write the image to using the write() method of the ImageIo class.
You could do it in this way:
var image = Image.Load<Rgba32>(byteArray);
image.Mutate(x => x.Grayscale());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With