I am working on a Machine learning project. and the following are my requirements.
- Given a *.bytes or .asm file of different file sizes
- Generate a grayscale(rgb) image of the file, this should have values between 0 - 255
- 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
- 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.
- 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.