Carnez Davis
Carnez Davis

Reputation: 63

Reshaping Error in python

I am new to python, and I am trying to execute the following code, but I get the following error:

im[:,:,0] = f
ValueError: could not broadcast input array from shape (700,900,3) into shape (700,900)

Can someone assist me with it?

img = numpy.zeros((700, 900))

row_idx = 160
curve = []

count = 0
for i in range(0, 900):
                        contour.append((row_idx, i))


values, num_values = get_values(curve);


a = imread('20091016_seg1_26_18245948_1chop.png')
f = numpy.rot90(a, 2)
f = numpy.rot90(a, 2)

size_vec = numpy.shape(img)

im = numpy.zeros((size_vec[0],size_vec[1], 3));
im[:,:,0] = f
im[:,:,1] = f
im[:,:,2] = f


for i in range(0, num_values):
        im[values[i].y, values[i].x, 0] = 0.0
        im[values[i].y, values[i].x, 1] = 1.0
        im[values[i].y, values[i].x, 2] = 0.0

imsave('OUTPUT.png', im)

(eliminated semicolons)

Upvotes: 1

Views: 344

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 59005

It should work with:

im[..., 0] = f[..., 0]

The problem is that you were trying to put the whole f into im[..., 0], giving the ValeError due to the dimension incompatibilty.

Upvotes: 1

Related Questions