4

I've imported a 10 band raster tif using rasterio as ds. I'm attempting to pad the image but keep getting errors with the transform.

print(ds.transform)
pilpadimg = rasterio.pad(ds.read(), transform=ds.transform, pad_width=((0,0),(1,2),(3,4)), mode='reflect')

| 15.00, 0.00, 344117.00|
| 0.00,-15.00, 5082412.00|
| 0.00, 0.00, 1.00|
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-77-9526c3dd4939> in <module>
      1 print(ds.transform)
----> 2 pilpadimg = rasterio.pad(ds.read(), transform=ds.transform.identity(), pad_width=((0,0),(1,2),(3,4)), mode='reflect')

~\AppData\Local\Continuum\anaconda3\envs\pytorch\lib\site-packages\rasterio\__init__.py in pad(array, transform, pad_width, mode, **kwargs)
    279     padded_array = np.pad(array, pad_width, mode, **kwargs)
    280     padded_trans = list(transform)
--> 281     padded_trans[2] -= pad_width * padded_trans[0]
    282     padded_trans[5] -= pad_width * padded_trans[4]
    283     return padded_array, Affine(*padded_trans[:6])

TypeError: can't multiply sequence by non-int of type 'float'

It works great with numpy but there is no transform..

>>> x = np.ones((8,3,3))
>>> np.pad(x, pad_width=((0,0),(1,2),(3,4)), mode='constant', constant_values=0)

pad_width has to be an int and the transform is a float. I converted the transform list to int and still get the same error

EDIT: Fixed by simply using numpy to pad instead of rasterio

pilpadimg = np.pad(ds.read(),pad_width=[(0,0),(1,2),(3,4)], mode='reflect')
3

Rasterio's docs for pad say:

pad_width: int
    number of pixels to pad array on all four sides

So you need to pass a single value, not numpy's sequence of before/after values:

pilpadimg = rasterio.pad(ds.read(), transform=ds.transform, pad_width=42, mode='reflect')
  • Ahh. Based on the numpy documentation I assumed it worked similarly and would take a tuple. Thank you – mkmitchell Dec 5 '18 at 14:10
  • I decided to just use numpy. pilpadimg = np.pad(ds.read(),pad_width=[(0,0),(1,2),(3,4)], mode='reflect') – mkmitchell Dec 5 '18 at 14:17
  • You can always recalculate the new origin of the affine if you still need georeferencing. – Marc Pfister Dec 5 '18 at 14:44
  • That's exactly what I plan to do Marc! Thanks. Time to learn something new. – mkmitchell Dec 6 '18 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.