Python Forum
Error for convert image PIL to CV2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error for convert image PIL to CV2
#1
Hi everyone,

I am working with Python and ia habe a problem with convert the format PIL to CV2, my code is:

from PIL import Image
import numpy as np

img = Image.open('C:/Users/XYZ/example.png')

#Format PIL to CV2
cv_img = np.asarray(img)[:,:,::].copy()
The error is:

Error:
cv_img = np.asarray(img)[:,:,::].copy() Traceback (most recent call last): File "<ipython-input-81-422b4aff8d72>", line 1, in <module> cv_img = np.asarray(img)[:,:,::].copy() IndexError: too many indices for array
Please our help!

Thank,
karlo.
Reply
#2
Which dimensions has the picture?
I tried it with a some pic I found on my drive and I didn't recive the error.
Do you need this part: [:,:,::] ? you are copying the whole picture, therefore
cv_img = np.asarray(img).copy()
should work. The picture I used had three dimensions: x, y, colors. In my case 202 x 202 x 3. Maybe you have a greyscale image or so? so that the last dimension is not there. Because it seems only 1 or 2 dimensions should be there, but you want to copy three. Try without [:,:,::] and check the dimensions :)
Reply
#3
Thank you very much for your answer, I applied the following line and it works for me now (no error):

img = Image.open('C:/Users/XYZ/example.png').convert('RGB')
... however, I will continue to do tests with what you recommend.

At the moment I have another problem, the decoding I am working on is not good ... I will open a new theme with this problem.

Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error sending and receiving a base 64 encoded image to an API lagarcia 0 1,964 Jun-24-2020, 05:55 AM
Last Post: lagarcia
  Convert Image API piedrucci 1 2,071 Apr-03-2019, 10:57 PM
Last Post: Larz60+
  Problema with convert image to string karlo123 1 2,773 May-16-2018, 10:44 PM
Last Post: karlo123
  Error: ValueError: could not convert string to float: 'L200 1.6 D/C' Jaarroy 2 6,563 Jan-18-2018, 02:00 PM
Last Post: Jaarroy
  Error while running python script to get pixel points from google static map image python_newbee 3 4,838 Sep-15-2017, 06:25 AM
Last Post: python_newbee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020