0
def binaire2image(binaires):
    r = 100
    while True:
        
        try:
            c = len(binaires) // 8 // r
            v = [binaires[i:i+8] for i in range(0, len(binaires), 8)]
            d = [int(n, 2) for n in v]
            arr = np.array(d).reshape((r, c))
            image = Image.fromarray(arr.astype(np.uint8), mode='L')
            return image
        except ValueError:
            r += 1

Please how can I get the original image from the binary string , without any information about the size .This code gives an unclear image . If I put a value close to the r "row" of the original image which is 180 , it gives the correct result , but if I don't have any idea about the image ?

1 Answers1

0

You can't know for sure. Any more than if someone gives you 24 apples in a bag whether they were previously arranged on the supermarket shelf as 6x4, or 4x6, or 3x8, or 8x3, or 12x2 and so on.

The best you can do is use heuristics. Firstly, the height and width must be factors of the number of pixels. Second, images tend to be somewhat square so the side-lengths nearer the square root of the total number of pixels are probably more likely than those at the extremes. And finally, rows of an image tend to be fairly similar to rows above - imagine a sky and horizon landscape type of photo.

That said, there can be all sorts of complications - headers at the start of the data, footers at the end of the data, not knowing whether RGB or grey, 8-bit vs 16-bit data, compression, endianness, planar packing, interlacing and so on.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432