1

I am trying to read and image using OpenCV and after reading that image I have got some data which I have to save in a CSV file using numpy. Here is the program:-

import cv2 as cv
import numpy as np
import os

img1 = cv.imread('C:/Users/sbans/Pictures/bird.jpg')
dataA1 = os.path.basename('C:/Users/sbans/Pictures/bird.jpg')
height, width, channels = img1.shape
dataA2 = height
dataA3 = width
dataA4 = channels
a = int(height/2)
b = int(width/2)
px1  = img1[a,b]
dataA5 = px1[0]
dataA6 = px1[1]
dataA7 = px1[2]
a = np.array([dataA1, dataA2, dataA3, dataA4, dataA5, dataA6, dataA7])

img2 = cv.imread('C:/Users/sbans/Pictures/cat.jpg')
dataB1 = os.path.basename('C:/Users/sbans/Pictures/cat.jpg')
height, width, channels = img2.shape
dataB2 = height
dataB3 = width
dataB4 = channels
a = int(height/2)
b = int(width/2)
px2 = img2[a,b]
dataB5 = px2[0]
dataB6 = px2[1]
dataB7 = px2[2]
b = np.array([dataB1, dataB2, dataB3, dataB4, dataB5, dataB6, dataB7])
np.savetxt("stats.csv", np.stack((a,b)), delimiter=",", fmt='%s')

This error is coming:-

Traceback (most recent call last):

File "C:\Users\sbans\Documents\demo_opencv.py", line 32, in np.savetxt("stats.csv", np.stack((a,b)), delimiter=",", fmt='%s')

File "<array_function internals>", line 6, in stack

File "C:\Users\sbans\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\shape_base.py", line 425, in stack

raise ValueError('all input arrays must have the same shape') ValueError: all input arrays must have the same shape

4
  • 1
    the variable px and the csv file names are same so may be its over written check/change them and try one more time Commented Oct 29, 2019 at 6:41
  • 1
    Make one array of all data, then save it to a file Commented Oct 29, 2019 at 6:41
  • I want to save the all the data in a single csv file not multiple csv files Commented Oct 29, 2019 at 6:43
  • You're not making multiple files. Your second savetxt is overwriting the old one Commented Oct 29, 2019 at 6:50

2 Answers 2

1

You could simplify the code a bit by defining a function

def get_array(file):
    img = cv.imread(file)
    basename = os.path.basename(file)
    height, width, channels = img.shape
    h = int(height/2)
    w = int(width/2)
    px  = img[h,w]
    return np.array([basename, height, width, channels, px[0], px[1], px[2]]) 

Then savetxt can accept a list of same-sized 1D arrays

a = get_array('C:/Users/sbans/Pictures\bird.jpg')
b = get_array('C:/Users/sbans/Pictures\cat.jpg') 

np.savetxt("stats.csv", (a, b), delimiter=",", fmt='%s')
Sign up to request clarification or add additional context in comments.

2 Comments

This error is coming:- Traceback (most recent call last): File "C:\Users\sbans\Documents\demo_opencv.py", line 32, in <module> np.savetxt("stats.csv", np.stack((a,b)), delimiter=",", fmt='%s') File "<__array_function__ internals>", line 6, in stack File "C:\Users\sbans\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\shape_base.py", line 425, in stack raise ValueError('all input arrays must have the same shape') ValueError: all input arrays must have the same shape
Both arrays seem to be 1 by 7 to me... I'm not sure I understand the error
0

The default behavior of np.savetxt method is to replace the existing file with a new data.

If you want to sequentially write the data to a file then you need to have a reference for that file then use it for np.savetxt.

For your case:

f = open('stats.csv','w')

...
np.savetxt(f, np.row_stack(np.column_stack((dataA1, dataA2, dataA3, dataA4, dataA5, dataA6, dataA7))), delimiter=",", fmt='%s')


...
np.savetxt(f, np.row_stack(np.column_stack((dataB1, dataB2, dataB3, dataB4, dataB5, dataB6, dataB7))), delimiter=",", fmt='%s')


f.close()

Comments

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.