yuhang.tao
yuhang.tao

Reputation: 644

TypeError: Cannot handle this data type: (1, 1, 3), <f4

the temp_image is (600, 600, 3) with values ranging from 0 to 1.

def pro_process(temp_img, input_size):
    img = np.asarray(temp_img).astype('float32')
    img = np.array(Image.fromarray(img).resize((input_size, input_size)).convert(3))
    return img

It gives the following error:

Traceback (most recent call last):
  File "S:\Program Files\Python36\lib\site-packages\PIL\Image.py", line 2681, in fromarray
    mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 3), '<f4')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "H:\OneDrive\synchronization code\Developing collection\Python\MNet_DeepCDR-master\mnet_deep_cdr_ide\run\Step_3_MNet_test.py", line 56, in <module>
    temp_img = pro_process(Disc_flat, CDRSeg_size)
  File "S:\Program Files\Python36\lib\site-packages\mnet_deep_cdr\mnet_utils.py", line 18, in pro_process
    img = np.array(Image.fromarray(img).resize((input_size, input_size)).convert(3))
  File "S:\Program Files\Python36\lib\site-packages\PIL\Image.py", line 2683, in fromarray
    raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 3), <f4

project Link: https://github.com/HzFu/MNet_DeepCDR

What's the error and how to fix it?

according to this link:PIL TypeError: Cannot handle this data typeI have updated my code, but there still have an error

def pro_process(temp_img, input_size):
print(temp_img.shape)
img = np.asarray(temp_img).astype('float32')
img = np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert(3))
return img

error:

Traceback (most recent call last):
  File "H:\OneDrive\synchronization code\Developing collection\Python\MNet_DeepCDR-master\mnet_deep_cdr_ide\run\Step_3_MNet_test.py", line 56, in <module>
  temp_img = pro_process(Disc_flat, CDRSeg_size)
  File "S:\Program Files\Python36\lib\site-packages\mnet_deep_cdr\mnet_utils.py", line 18, in pro_process
  img = np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert(3))
  File "S:\Program Files\Python36\lib\site-packages\PIL\Image.py", line 995, in convert
  im = self.im.convert(mode, dither)
 TypeError: argument 1 must be str, not int

Upvotes: 43

Views: 96154

Answers (7)

Wahyu Bram
Wahyu Bram

Reputation: 1

def convert_cv2_to_image_in_memory(img: np.ndarray) -> Image:
    # logic to make sure pixel value match for PIL module
    if img.dtype == np.float32 or img.dtype == np.float64:
        # cek pixel value
        if img.max() <= 255 and img.min() >= 0:
            # if pixel value already between 0-255, we only need convert it to uint8
            img = img.astype(np.uint8)
        else:
            # If not, do it with scale 255
            img = (img * 255).astype(np.uint8)
    
    elif img.dtype != np.uint8:
        raise ValueError("image type not support, shouldbe uint8 type or float32/float64 data type.")

    success, encoded_image = cv2.imencode('.png', img)
    if not success:
        raise ValueError("Fail Encoded image with OpenCV.")

    # make stream data from image that already encoded
    image_stream = io.BytesIO(encoded_image.tobytes())

    # read image from stream using PIL
    image = Image.open(image_stream)
    return image

Upvotes: 0

S G
S G

Reputation: 802

This works for me

tensor.shape
# [1, 3, Height, Weight]
tensor.squeeze().shape
# [3, Height, Weight]
tensor.squeeze().permute(1, 2, 0).shape
# [Height, Weight, 3]

Values are between -1 and 1. Therefore, 1 is added and divided by 2 and multiplied by 255. After that, converted into unsigned int

values = (((tensor.squeeze().
            permute(1, 2, 0).numpy() + 
            1.0)/2.0)*255).astype(np.uint8)
   outImg = Image.fromarray(values)
   outImg.save('./image.jpg')

Upvotes: 0

Raj Kumar More
Raj Kumar More

Reputation: 89

This solved my problem

Image.fromarray((img * 1).astype(np.uint8)).convert('RGB')

Upvotes: 4

yuhang.tao
yuhang.tao

Reputation: 644

I have sent an email to the author, and thanks for all of your guys help. he told me the answer.

We find that this problem is because the original scipy.misc.imresize in SciPy 1.0.0 has been removed in SciPy 1.3.0. We replace it by using Image.resize which may cause some error. We have fixed this bug in Github. Moreover, due to the resize function is changed, the results based on original model (i.e., Model\_MNet\_REFUGE.h5) are different with the paper’s.

Upvotes: 2

Puneeth Raj
Puneeth Raj

Reputation: 346

The issue is with the float (0–1) type of the array. Convert the array to Uint (0–255). The following thread is related: PIL TypeError: Cannot handle this data type

im = Image.fromarray((x * 255).astype(np.uint8))

Upvotes: 25

Mark
Mark

Reputation: 1179

The error message seems to be complaining about the shape, but it is really about the data type. Multiplying by 255 and then changing to uint8 fixed the problem for me:

random_array = np.random.random_sample(content_array.shape) * 255
random_array = random_array.astype(np.uint8)
random_image = Image.fromarray(random_array)

Upvotes: 62

BenedictGrain
BenedictGrain

Reputation: 111

please try this code:

np.array(Image.fromarray((img * 255).astype(np.uint8)).resize((input_size, input_size)).convert('RGB'))

Upvotes: 8

Related Questions