-1

I have an image with shape (1120,1472,4), am trying change this to (3000, 3000, 4) the code i have written is like this.

pad_shape = (3000, 3000)
i = np.array('test.tif')
result = np.zeros(pad_shape,dtype=int)
result[:i.shape[0], :i.shape[1]] = i
print(result)

It generates

ValueError: could not broadcast input array from shape (1120, 1472, 4) into shape (3000, 3000)
P. Camilleri
  • 12,664
  • 7
  • 41
  • 76
Anup Panwar
  • 293
  • 4
  • 11

1 Answers1

0

Here is answer

import numpy as np
from numpy import dtype

pad_shape = np.array((3000, 3000, 4))
i = np.zeros((1120, 1472, 4), dtype=np.int)

result = np.zeros(pad_shape, dtype=np.int)

x, y = 10, 10
shape = i.shape
print shape

rr = result[y:y+shape[0], x:x+shape[1]]
print rr

The x and y variables are for padding. The error you received because of padding.

Kewin Rather
  • 135
  • 1
  • 12
  • The error received was because OP missed a 4 in result shape. – P. Camilleri Nov 18 '16 at 07:41
  • But be careful to for x and y variables. If you cross the bound of the image then you will get that error again. – Kewin Rather Nov 18 '16 at 07:41
  • The error definitely about the boundary of the image not OP. If you test my code you will see P.Camilleri – Kewin Rather Nov 18 '16 at 07:43
  • P.Camilleri after adding 4 on pad_shape code works. Thanks both of you – Anup Panwar Nov 18 '16 at 07:44
  • No, the error comes from the missing 4 in pad_shape. The x and y variables are useless, this answer is misleading. – P. Camilleri Nov 18 '16 at 07:45
  • Yes but I fixed already it in my code. The main problem here boundary and He did not recognize about it but with this code everthing is more clear. – Kewin Rather Nov 18 '16 at 07:47
  • The main problem here is not the boundary, you can remove the x and y lines and just do results[:shape[0], :shape[1]] = i – P. Camilleri Nov 18 '16 at 07:50
  • Kewin Rather thanks for your help, I understood the issue but what is the role of x, y i didn't understood – Anup Panwar Nov 18 '16 at 07:51
  • The x and y are the padding variables for your image. Apply it to your code and see what is going on. – Kewin Rather Nov 18 '16 at 07:52
  • @AnupPanwar these variables are not necessary ! result[:shape[0], :shape[1]] = i is enough if you change your pad shape to (3000, 3000, 4) – P. Camilleri Nov 18 '16 at 07:54
  • İt is not important to solve the current problem it is important to solve current and future problems. Thats what ı do. – Kewin Rather Nov 18 '16 at 10:45
  • @KewinRather How are you solving a problem? You are creating one adding 2 useless variables! – P. Camilleri Nov 18 '16 at 12:35
  • Do you know What variables are doing ? Of course no. With these two variables the image inside the other image can be move through the main image boundary. This is a very useful feature cause he can maybe want to to this in the future. Did you know understand what I mean ???? – Kewin Rather Nov 18 '16 at 12:51