1

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

O.rka
  • 29,847
  • 68
  • 194
  • 309

2 Answers2

0

JPEG is a compressed image format, therefore you cannot directly get the rgb bitmap from the file contents. You have to pass it through a decoder first. The imread() call does this when it loads the file, using the decoder provided by PIL.

If you want a simpler image file format that is easy to parse, take a look at the answer to this question: What is the simplest RGB image format?

Community
  • 1
  • 1
Craig
  • 4,605
  • 1
  • 18
  • 28
0

You just have to change the shape accordingly. The actual steps are as follows:

1) Read the image using scipy's misc.imread().
2) Create a raw file using the image array from step-1
3) Now, use np.fromfile for creating actual image from raw file
4) Assign the shape information from misc.imread

Code Example:

import numpy as np
from scipy import misc

# Read image using `imread`
suza = misc.imread('suza.jpg') 
suza.shape
# (1005, 740, 3)

# create raw file
suza.tofile('suza.raw')

# image from raw file
suza_from_raw = np.fromfile('suza.raw', dtype=uint8)
suza_from_raw.shape
# (2231100, )

# Assign the same shape from `misc.imread`
suza_from_raw.shape = (1005, 740, 3)
kmario23
  • 57,311
  • 13
  • 161
  • 150
  • What if you didn't load `suza` in through `misc.imread` and did not have the exact shape of `suza` and did not want to store as a raw file. If you just had the vector `A`, could you use this method with `io.BytesIO` to get the RGB array? – O.rka Mar 12 '17 at 17:20