1

I have a numpy array of images in that shape:

(50000, 32, 32, 3)
  • 50000 is the number of images
  • 32, 32 are the height and width
  • 3 are the RGB values with a range of 0-1

I would like to convert it to a 2D shape of:

(50000, 1024)

Here I would have 50000 images represented in one row, the RGB value would be converted to let's say an hexadecimal value I've went through a lot of conversion processes into stack overflow and I've found some. I know that if my array was a 3D array with an already converted value I could easily use reshape()function to convert it to 2D. Now what I'm searching is the easiest way to convert RGB values and reshape my array

Would this be possible in 1 or two lines or should I use an external function?

JSmith
  • 4,519
  • 4
  • 29
  • 45
  • 1
    https://stackoverflow.com/questions/43001349/convert-numpy-array-of-rgb-values-to-hex-using-format-operator – taras Mar 22 '19 at 11:30
  • @taras thanks didn't find this one that helps but doesn't resolve all my problem as I would like to see the best code for the whole process. I'm new to python so I'm afraid by playing with the arrays alone I won't take the best way possible – JSmith Mar 22 '19 at 11:32

3 Answers3

2

First convert the RGB values in the last dimension to the HEX value using whatever function you like. This SO answer may help.

Reshape then works on any number of dimensions:

import numpy as np

def rgb2hex(r, g, b):
    return '#%02x%02x%02x' % (r, g, b)

vfunc = np.vectorize(rgb2hex)

a = (np.random.uniform(0,1,(10,5,5,3))*255).astype(int)

c = vfunc(a[:,:,:,0], a[:,:,:,1], a[:,:,:,2])

c.reshape((10,25))
Jurgen Strydom
  • 3,540
  • 1
  • 23
  • 30
1

The following combines the RGB values into a single value

x=np.zeros((100,32,32,3))
x[:,:,:,0] = np.trunc(x[:,:,:,0]) + np.trunc(x[:,:,:,1] *256) + np.trunc(x[:,:,:,2] *65535)
y=x[:,:,:,0]
print(y.shape)

The resulting shape of y: (100, 32, 32)

Next you can use the reshape function on y.

dwjbosman
  • 906
  • 9
  • 33
1

In order to do so, you firstly need to reshape the ndarray (np.reshape):

a = np.random.randint(1,10,(500, 32, 32, 3))
a_r = np.reshape(a, (500, 1024, 3))
print(a_r.shape)
# (500, 1024, 3)

Now, in order to convert the RGB values along the last dimension to hexadecimal representation as you suggest, you could define a function that returns a hexadecimal representation of the three values with a simple string formatting:

def rgb_to_hex(x):
    return '#{:02X}{:02X}{:02X}'.format(*rgb.reshape(3))

In order to apply the conversion along all rows in the last axis, you can use np.apply_along_axis:

a_new = np.apply_along_axis(rgb2hex, axis=-1, arr=a_r).shape
print(a_new.shape)
# (500, 1024)
yatu
  • 86,083
  • 12
  • 84
  • 139