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?

2
  • 1
    stackoverflow.com/questions/43001349/… Commented Mar 22, 2019 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 Commented Mar 22, 2019 at 11:32

3 Answers 3

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))
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the answer, but will it convert my RGB value?
great could you show me the process of conversion please? for loop, one line for loop???
Just note that I made a fix. Please accept the answer if it helped.
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)

Comments

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.

2 Comments

I've just tried your solution but I still the same shape with the 3 length array at the end. Thanks in advance
I added some details. If I run the code I get a shape of 100,32,32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.