Reputation: 577
i'm generating image histograms reading jpeg files into a 24 bit array and the converting it to a 32 bit numpy one for processing with numpy.histogram, following this approach:
img=imread(file,'RGB')
by=img.tobytes()
struct.iter_unpack('<3B',by)
int.from_bytes(c, byteorder='big')
the problem with this approach and others i've tried (reshaping to w*h,3, etc.)is the iterator delay, my question is:
is there a direct way to do this in python without iterators, or should i write a simple C function to do it?
def jpeg2colors(fnme):
return np.asarray(
[int.from_bytes(c, byteorder='big')
for c in struct.iter_unpack('<3B', imread(fnme).tobytes())])
the other slower implementations:
def conversionSLOW(fnme): # reshape to (w*h,3)
img = imread(fnme, mode='RGB')
return np.array([int.from_bytes(c, byteorder='big')
for c in img.reshape((img.shape[0] * img.shape[1], 3))])
def conversionbyte01(fnme): # int iteration -> closer to fastests
by = imread(fnme, mode='RGB').tobytes()
return np.asanyarray([int.from_bytes(by[c:c + 3], byteorder='big')
for c in range(0, len(by) - 3, 3)],
dtype=np.int32)
def conversionbyte02(fnme):
img = imread(fnme)
return np.array([int.from_bytes(c, byteorder='big') for c in img.reshape(img.shape[0] * img.shape[1], 3)])
def conversionbyte03(fnme):
img = imread(fnme)
return np.array([int.from_bytes(img.reshape(img.shape[0] * img.shape[1], 3), byteorder='big')])
EDIT1:
found a solution creating a (w,h,4) numpy array and copying read image, rest is simple and fast (x40 improvement from fastest iteration solution)
def fromJPG2_2int32_stbyst(fnme): # step by step
img = imread(fnme)
# create a (w,h,4) array and copy original
re = np.zeros((img.shape[0], img.shape[1], 4), dtype=np.uint8)
re[:, :, :-1] = img
# lineup to a byte structure ready for ' frombuffer'
re1 = re.reshape(img.shape[0] * img.shape[1] * 4)
by = re1.tobytes()
# got it just convert to int
cols = np.frombuffer(by, 'I')
return cols
def fromJPG2_2int32_v0(fnme): # more compact & efficient
img = imread(fnme)
re = np.zeros((img.shape[0], img.shape[1], 4), dtype=np.uint8)
re[:, :, :-1] = img
return np.frombuffer(re.reshape(img.shape[0] * img.shape[1] * 4).tobytes(), 'I')
def fromJPG2_2int32(fnme): # even better using numpy.c_[]
img = imread(fnme)
img = np.c_[img, np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)]
return np.frombuffer(img.reshape(img.shape[0] * img.shape[1] * 4).tobytes(), 'I')
Upvotes: 0
Views: 665
Reputation: 577
solution:
def fromjpg2int32(fnme): # convert jpg 2 int color array
img = imread(fnme)
return np.frombuffer(
np.insert(img, 3, values=0, axis=2).
reshape(img.shape[0] * img.shape[1] * 4).tobytes(), 'I')
Upvotes: 1