0

I have a numpy array with the dtype.names as:

('s0', 's1', 's2', 's3')

What steps would I follow to revert the numpy array dtype to np.float32?

I obtain the numpy array ('s0', 's1', 's2', 's3') from:

    import pyopencl as cl
    import pyopencl.array as cl_array
    import numpy as np
    from scipy.misc import imread, imsave
    import math
    import os
    import PIL.Image
    import cv2

    os.environ['PYOPENCL_COMPILER_OUTPUT'] = '1'

    #produces numpy array of the structures
    # -> ('s0', 's1', 's2', 's3') and shape (image_width,image_height, image_depth). 
    def process_image(path_to_image, padding):
        rgb_image = PIL.Image.open(path_to_image)
        rgba_image = rgb_image.convert('RGBA')
        im_src = np.array(rgba_image).astype(dtype=np.float32)
        print(im_src.shape)
        return im_src.astype(dtype=cl_array.vec.float4)

dtype=cl_array.vec.float4 bundles 4 np.float32 into (np.float32,np.float32,np.float32,np.float32)

The image_depth is 4:

I tried this:

vector_float4= process_image('image.jpg',0)
result = np.array(vector_float4.tolist())

result has a shape of (width, height, 4, 4). I expected (width, height, 4).

I am looking at this answer: Convert structured array to regular NumPy array

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Gakuo
  • 845
  • 6
  • 26
  • 2
    For numpy arrays, the `.astype` method is usually sufficient to convert between the numpy `dtypes`, e.g. `float`, `np.float16`, `np.uint8`. This doesn't necessarily apply to `cl/cv` types. For example, I don't know how the `cl_array.vec.float4` relates to numpy dtypes. numpy `tolist()` creates a list (nested) of Python scalar elements (floats, integers). `np.array` can process most such lists (you may want to add a `dtype`). The `tolist` route may be most robust, but it tends to be slowest. – hpaulj Jun 07 '19 at 23:23
  • Thanks, this worked. – Gakuo Jun 07 '19 at 23:38

0 Answers0