I'm currently trying to 'cheat' a GeoTiff by rotating the image but keeping the georeference the same, I.E. I just want the actual visual image rotated.

Currently I'm reading in the image using rasterio and clipping it using the mask method. When I print the shape of the output image from mask I get (1, 32, 32).

I then run:

out_image = transform.rotate(out_image, 90).astype(np.float32)

and still get (1, 32, 32) when printing the size.

However when I write out the GeoTiff and look at the image I only get a band of pixels rather than the rotated image as shown below:

Original: Original

Rotated & Clipped: Rotated

Why is it giving me just a band of pixels?

After Ipdudley's comment:

def create_crop(raster):
    affine = raster.affine
    point_x, point_y, radius = get_blob_point(raster)
    out_image, out_transform = mask(raster,
                                    [get_point_crop_polygon(Point(affine * (point_x, point_y)), radius)],
    out_meta = raster.meta.copy()


    out_image = np.rot90(out_image, k=1)


    out_meta.update({"driver": "GTiff",
                     "height": out_image.shape[1],
                     "width": out_image.shape[2],
                     "transform": out_transform})

    with rasterio.open("test.tif", "w", **out_meta) as dest:

I'm not sure why you are only getting a single line of pixels, perhaps you could post more of the code that is producing this result?

However this can be easily done with the numpy.rot90 function. Here's with gdal / numpy:

from osgeo import gdal
import numpy as np

dataset = gdal.Open("/path/to/image", gdal.GA_Update)

# Get desired band
band = dataset.GetRasterBand(1)

array = band.ReadAsArray()

# k is how many times the array is to be rotated (anti-clockwise)
rotated = np.rot90(band, k=1)

# write new data to band

band = dataset = None
  • Edited my post with my code, with your answer I then get ValueError: Source shape is inconsistent with given indexes – Ciaran Evans Mar 3 '17 at 15:30
  • I now also get back (32, 1, 32) for the shape – Ciaran Evans Mar 3 '17 at 15:59
  • Hey @CiaranEvans, I think it's rotating the wrong axes, so maybe try fiddling that, i.e., numpy.rot90(m, k=1, axes=(1, 2)). – Alex Leith Mar 5 '17 at 21:00
  • 1
    @AlexLeith That's brilliant thanks, sorted out the issue with the line of pixels, something so obvious now I think of it! Thanks again! - Also managed to keep the georeferencing of the original GeoTiff too! – Ciaran Evans Mar 6 '17 at 9:03

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.