0

I am working on a Machine learning project. and the following are my requirements.

  1. Given a *.bytes or .asm file of different file sizes
  2. Generate a grayscale(rgb) image of the file, this should have values between 0 - 255
  3. Resize all images to N x N pixels.

I am trying to do the image generation in C# ( as Im more familiar with it than Python) . I have this code below which attempts to create the image but i have two issues with it

  1. It uses the length of the byte[] to determine the width and height, meaning I have to find the square root of the byte[] to determine the length and width. I dont want this,i want it to create the image based on whatever size i give it.
  2. Im not sure if the generated image is correct, because i fed it with a byte[] from a grayscale image but the resulting image was nothing close to the original image.
    static void Main(string[] args)
        {
            string fileRead = @"C:\Users\User\source\repos\ExeTobinary\Testing\grayscale.jpg";
            string fileSave = @"C:\Users\User\source\repos\ExeTobinary\Testing\Test.jpg";

            Random r = new Random();
            int width = 1000;
            int height = 1000;
            byte[] pixelValues = new byte[width * height];
         /*   for (int i = 0; i < pixelValues.Length; ++i)
            {
                //Just create random pixel values
                pixelValues[i] = (byte)r.Next(0, 255);
            }*/

            pixelValues = File.ReadAllBytes(fileRead);


            GetBitmap(pixelValues, width, height, 1);

        }

Heres the Method

   public static Bitmap  GetBitmap(byte[] _rawDataPresented, int _width, int _height, double scalingFactor)
        {
            Bitmap image = new Bitmap(_width, _height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);  //Format8bppIndexed

            // konwersja palety idexed na skale szarosci
            ColorPalette grayPalette = image.Palette;
            Color[] entries = grayPalette.Entries;
            for (int i = 0; i < 256; i++)
            {
                Color grayC = new Color();
                grayC = Color.FromArgb((byte)i, (byte)i, (byte)i);
                entries[i] = grayC;
            }
            image.Palette = grayPalette;

            // wrzut binary data do bitmapy
            BitmapData dataR = image.LockBits(new Rectangle(Point.Empty, image.Size), ImageLockMode.WriteOnly, image.PixelFormat);
            Marshal.Copy(_rawDataPresented, 0, dataR.Scan0, _rawDataPresented.Length);
            image.UnlockBits(dataR);

            // skalowanie wielkosci
            Size newSize = new Size((int)(image.Width * scalingFactor), (int)(image.Height * scalingFactor));
            Bitmap scaledImage = new Bitmap(image, newSize);

            string fileSave = @"C:\Users\User\source\repos\ExeTobinary\Testing\Test.jpg";
            scaledImage.Save(fileSave, ImageFormat.Jpeg);

            return scaledImage;
        }

I have used a grayscale image here to test the validity of the method, but the input file would be either *.bytes or .asm file. Can someone who has done something similar help in this direction... To recap, I would like to generate a grayscale image from .bytes or .asm file, this files need to be of same dimension eventually. I would prefer the code sample in C#, but if i get a python version i wont mind.

Here is the grayscale image i used in testing Test Image

Heres my result Resulting Image

  • The is a header on the jpg file that has the ascii name and the original size. You must read the header and get info. See : https://en.wikipedia.org/wiki/JPEG – jdweng Sep 14 '19 at 10:38
  • `Color grayC = new Color();` ← Don't do this. You create a new object and never use it, since the next line immediately replaces it. Just do `entries[i] = Color.FromArgb(i, i, i);` directly. The arguments for `Color.FromArgb` are `Int32` anyway. – Nyerguds Sep 27 '19 at 08:09
  • Note, your current method ignores the image stride. Since [all lines in an image are rounded up to a multiple of 4 bytes in .Net](https://stackoverflow.com/q/2185944/395685), you will lose bytes to that padding behind each line on any image with a width that is not an exact multiple of 4. Copy per line to avoid this. See [my answer to the above-linked question](https://stackoverflow.com/a/43967594/395685) for code that builds an image from bytes. – Nyerguds Sep 27 '19 at 08:13

0 Answers0