Skip to content

Latest commit

 

History

History
150 lines (110 loc) · 5.86 KB

imageio.NET.md

File metadata and controls

150 lines (110 loc) · 5.86 KB

imageio.NET

The library provides functionality for reading and saving images from/to a file.

Dependencies

  • System.FloatingPoint
  • warnings.NET

API reference

namespace System

  • namespace IO

    • staticclass imageio
      • method static imread(string uri) -> byte[,,] static Reads an image from a file at the specified path uri. Returns: 3-d standard byte array with pixels in HWC RGB format.
      • method static imread(Stream s) -> byte[,,] static Reads an image from stream s. Returns: 3-d standard byte array with pixels in HWC RGB format.
      • method static imwrite(string uri, Array im, string format = null, params object[] kwargs) -> void static Saves the image from the array im to the file with specified uri in the format format with the specified quality (for jpg). im parameter should be 3-dimensional array of integer or floating point data type.
      • method static imwrite(Stream s, Array im, string format = null, params object[] kwargs) -> void static Saves the image from the array im to the stream s in the format format with the specified quality (for jpg). im parameter should be 3-dimensional array of integer or floating point data type.
      • property static __version__ -> string static readonly Returns: current imageio version as string.
      • method static show_formats() -> void static Outputs supported image formats to the console.

Code samples

Note: Android users need to specify full file paths. The full path usually starts with "/storage/emulated/0/". Only short file names are specified in the examples.

Generate random noise and save it in png and jpeg with 75 quality.

using System;
using System.IO;

namespace imageio1
{

    public static class Program
    {

        public static void Main()
        {
            var img = new byte[512, 512, 3];
            var rand = new Random();
            for(int y = 0; y < 512; y++)
            {
                for(int x = 0; x < 512; x++)
                {
                    img[y, x, 0] = (byte)rand.Next(0, 255);
                    img[y, x, 1] = (byte)rand.Next(0, 255);
                    img[y, x, 2] = (byte)rand.Next(0, 255);
                }
            }
            imageio.imwrite("Noise.png", img);
            imageio.imwrite("Noise.jpg", img, kwargs: 75);
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }

    }

}

Generate mosaic and save to bmp.

using System;
using System.IO;

namespace imageio2
{

    public static class Program
    {

        public static void Main()
        {
            var img = new byte[512, 512, 3];
            var rand = new Random();
            for(int y = 0; y < 512; y++)
            {
                for(int x = 0; x < 512; x++)
                {
                    unchecked
                    {
                        img[y, x, 0] = (byte)((2 * x - y) % 255);
                        img[y, x, 1] = (byte)((x - 3 * y) % 255);
                        img[y, x, 2] = (byte)((x + y) % 255);
                    }
                }
            }
            imageio.imwrite("Mosaic.bmp", img);
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }

    }

}

Open a color image 1.jpg, convert to black and white and save as 2.png.

using System;
using System.IO;

namespace imageio3
{

    public static class Program
    {

        public static void Main()
        {
            var img = imageio.imread("1.jpg");
            for(int y = 0; y < img.GetLength(0); y++)
            {
                for(int x = 0; x < img.GetLength(1); x++)
                {
                    var grayscale = (byte)((img[y, x, 0] + img[y, x, 1] + img[y, x, 2]) / 3);
                    img[y, x, 0] = grayscale;
                    img[y, x, 1] = grayscale;
                    img[y, x, 2] = grayscale;
                }
            }
            imageio.imwrite("2.png", img);
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }

    }

}

Third-party codes as part of imageio.NET

As a backend, imageio.NET uses a pure reimplementation of the STB(C language) library for C#.

In accordance with the license of the original STB library, as well as those used in imageio.NET StbImageSharp and StbImageWriteSharp codes, the source codes were refactored and included in the imageio.NET library which is an integral part of the System.AI package.