I've been experimenting with binary files and the concept of bytes. One task that I am stuck on is once I read a file in as bytes in integer form, I can't figure out how to convert it to its RGB tuple form? For example, when I use np.fromfile
I read the file in as bytes in a base 10 integer representation. If I read the file with scipy.misc.imread
it reads the file in as rgb tuples.
How can I go from the output of np.fromfile
vector representation, to the RGB pixel representation from scipy.misc.imread
?
import numpy as np
from scipy import misc
path = "./Data/image.jpg"
# Read image as bytes
A = np.fromfile(path, dtype=np.uint8, count=-1)
A.shape
#(54021,)
A.min(), A.max()
# (0, 255)
A
# array([255, 216, 255, ..., 100, 255, 217], dtype=uint8)
# Read image as RGB tuples
B = misc.imread(path)
B.shape
(480, 480, 3)
B.min(), B.max()
# (0, 255)
B
# array([[[ 28, 27, 23],
# [ 15, 14, 10],
# [ 14, 13, 9],
# ...,
# [ 31, 26, 20],
# [ 29, 24, 20],
# [ 33, 28, 24]],
This is the test image I was using below: https://i.stack.imgur.com/zHiNp.jpg