Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

read image byte by byte doesn't work python

I have this function that writes image to a file byte by byte:

(The function stores in the first 2 bytes the amount of the rows in the image, the next 2 bytes it stores the amount of columns in the image and then in every byte the pixels rows by rows.)

import cv2
import numpy as np
from matplotlib.image import imread

def write_pic(pic_name):
    pic = imread(pic_name)
    height = pic.shape[0]
    width = pic.shape[1]

    with open("Resources/out.bin", "wb") as outfile:
        outfile.write(height.to_bytes(2, byteorder='big'))
        outfile.write(width.to_bytes(2, byteorder='big'))
        for x in pic:
            outfile.write(x.tobytes())

Then I have this function that reads an image byte by byte:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def read_pic(pic_name):
    with open(pic_name, "rb") as f:
        rows = int.from_bytes(f.read(2), byteorder='big')
        columns = int.from_bytes(f.read(2), byteorder='big')
        mat_pic = np.empty([rows, columns])
        print(rows)
        print(columns)
        while byte := f.read(1):
            for i in range(rows):
                for j in range(columns):
                    mat_pic[i][j] = int.from_bytes(byte, byteorder='big')
            if i == 287 and j == 511:
                break;

    cv2.imshow("The picture", mat_pic)
    cv2.waitKey(0)

But when I show the image, it shows white picture, anyone knows why?
Also, I could not figure out why this while loop while byte := f.read(1): looped infinite times so I added this line

if i == 287 and j == 511: break;

I run this with these lines:

write_pic("Resources/pic.jpg")
read_pic("Resources/out.bin")

>Solution :

If that’s a JPEG, then you’re probably getting an X by Y by 3 array, one for each color component. You need to handle that.

Why do you write the height and width big-endian? That’s not natural any more. Almost all systems you encounter today are little-endian.

This works:

import cv2
import numpy as np
from matplotlib.image import imread

def write_pic(pic_name):
    pic = cv2.imread(pic_name)
    print(pic.shape)
    height = pic.shape[0]
    width = pic.shape[1]

    with open("out.bin", "wb") as outfile:
        outfile.write(height.to_bytes(2, byteorder='big'))
        outfile.write(width.to_bytes(2, byteorder='big'))
        pic.tofile( outfile )

def read_pic(pic_name):
    with open(pic_name, "rb") as f:
        rows = int.from_bytes(f.read(2), byteorder='big')
        columns = int.from_bytes(f.read(2), byteorder='big')
        mat_pic = np.fromfile( f, dtype=np.uint8 ).reshape( (rows, columns, 3) )

    cv2.imshow("The picture", mat_pic)
    cv2.waitKey(0)

write_pic( 'walk.jpg' )
read_pic( 'out.bin' )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading