1
img = cv2.imread('img/dream.jpg')
(b,g,r)=cv2.split(img)
color = ('b','g','r')
b = cv2.calcHist([img],[0],None,[256],[0, 256])
g = cv2.calcHist([img],[1],None,[256],[0, 256])
r = cv2.calcHist([img],[2],None,[256],[0, 256])
     
for i, col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0, 256])
    plt.plot(histr, color = col)
    plt.xlim([0, 256])   
    
with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow('BGR')
    for row in rows:
        writer.writerow(row)

I am trying to read in a picture and show its RGB counts of each pixel. It's all cool until I tried to write my data into a csv file. Here is what it look like:

B,G,R
[0.],[0.],[631788.]
[0.],[0.],[418355.]
[0.],[0.],[105815.]

All I need is to remove the '[' and ']' from my array.

I am new to python I am not sure whether the array is tuple dictionary or something else.

I have tried the replace() method however it appear an error message 'numpy.ndarray' object has no attribute 'replace'

10
  • To discover the type of the object, you can use the type(obj). Commented Feb 21, 2019 at 15:44
  • 4
    Where did you specify rows? Commented Feb 21, 2019 at 15:44
  • Please update your question with a small amount of text of the csv file, rather than a link to an image. Commented Feb 21, 2019 at 15:45
  • You could try writer.writerow(row[0]) Commented Feb 21, 2019 at 15:47
  • @quamrana do you mean to post my csv file? Commented Feb 21, 2019 at 16:00

2 Answers 2

1

Every value in your array is within its own list. In python you access elements of a list using an index.

In your case, you can convert your array of lists (with each list having only one value i[0]) using the following:

delisted_array = np.array([i[0] for i in your_array_of_lists])

This list comprehension says: "take the value from first index of each (list) found in this array and put those values in a list".

Eg.

In [70]: a = np.array([[1],[2]])

In [71]: a
Out[71]: 
array([[1],
       [2]])

In [72]: delisted_array = np.array([i[0] for i in a])

In [73]: delisted_array
Out[73]: array([1, 2])
Sign up to request clarification or add additional context in comments.

Comments

1

What you need is to convert a list to String by

list_string = ''.join(list_name)

as well if you have a matrix

you will need

for item in list_outer:
  item = ''.join(item)

list_outer = ''.join(list_outer)

9 Comments

i have converted it to list but still appears error could you explain it more clearly thanks for your reply
you mean convert it to string , what error you have ?
imgur.com/a/0Yhcx91 this is what happen when i convert it to string and do the same implement as i did above
could you update the code with where exactly you convert the list to string , and which one
@Paga, i have update the answer , if you have a matrix
|

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.