S
S
SOTVM2020-01-27 11:26:38
linux
SOTVM, 2020-01-27 11:26:38

How to convert any file to a png image?

1) let 'weight of the file in bytes' = b , then 'size of the square picture' r=√(b÷4) (round to the nearest integer)
divide the number of bytes by 4 (1st byte =Red , 2nd byte =Green ,3rd = Blue, 4th = Alpha) = this is one pixel.
2) create a picture in .png format with size rxr
first pixel top-left (first four bytes of the source file),
last pixel bottom-right (last four bytes of the source file)
(if there were not enough bytes from the file, leave FF or 00)
PS
link to future (for myself)
structure PNG file

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
twobomb, 2020-01-27
@sotvm

Here I messed up on C #

namespace ConsoleApplication1{
    class Program{
        static void Main(string[] args){
            var file = File.Open(@"D:\myfile.exe", FileMode.Open);
            var byteLen = file.Length;
            int size = (int)Math.Floor(Math.Sqrt(byteLen / 4));
            Bitmap bm = new Bitmap(size,size);
            for (int y = 0; y < size ; y++){
                for (int x= 0; x < size ; x++){
                    byte[] rgba = new byte[]{ 0xFF,0xFF,0xFF, 0xFF};
                    short cnt = 0;
                    while (cnt < 4 && file.CanRead){
                        byte[] buff= new byte[1];
                        file.Read(buff,0,1);
                        rgba[cnt++] = buff[0];
                    }
                    bm.SetPixel(x, y, Color.FromArgb(rgba[0], rgba[1], rgba[2], rgba[3]));
                }    
            }
            file.Close();
            var saveFile = File.Open(@"D:\myimg.png", FileMode.Create);
            bm.Save(saveFile,ImageFormat.Png);
            saveFile.Close();
        }
    }
}

File 907Kb

5e2eb0bfacc6c816460507.png

PS I made a web version

M
mayton2019, 2020-01-27
@mayton2019

The author is trying to do steganography. Ie in the picture to hide information. Here the choice of PNG is useful because the infa is dense and in the case of a "smooth" nature of the information is compressed. Like an archiver.
SVG - not suitable because vectorial and wasteful.
JPG - also not suitable. corrupts information. It cannot then be extracted from the lossless file.
Concerning addition of a file to the size multiple to length of a line (padding). There is no 00 no FF is not suitable. Since the original file can also contain these constants and the algorithm will fail. It is necessary to read how it is done in cryptography. There is a special workaround. If it is not implemented correctly, then when decoding the picture back into a file, we can get a false lengthening of the file by the size of the tail of the last row of pixels. How big is the damage for the source file - HZ. But of course it's better not to leave it so that the reverse decoding is reliable in terms of file length.

G
Griboks, 2020-01-27
@Griboks

So what's the problem? Open the png specification and generate an image from it. Unfortunately, I only know the jpeg format, so I can't be more specific about png.
If we are talking about jpg, then you need to create rle+huffman encoded cosine coefficients in each chunk. For simplicity, you can take a ready-made large image and replace the existing chunks. You can even not decode the coefficients, but stupidly rewrite the already compressed huffman code. Then each byte of your file will give not 1/3, but, for example, 10/3 pixels.

V
Victor Taran, 2020-01-27
@shambler81

use the svg format
1. it is readable as html, in addition it also supports CSS and it will not be easy to deal with it, but just completely easy.
2. it is raster vector
3. it is supported by all browsers.
4. it weighs very little
5. it has the ability to be transparent
6. it can be adaptive.
7. you can even draw what you need, and then replace it with variables in the code.
https://svg-edit.github.io/svgedit/releases/latest...
8. supports both fonts and individual letters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question