1

I have a TIFF file of 7378x6923 (widthXheight) resolution. I resized it to 1200x1200 pixels using Image module in Python.

img = Image.open('path to tif file')
img = img.resize((1200,1200))

But after resizing, the pixel values show unusual values. Before resizing, a certain pixel's value was 125.28. Now, after resizing, that pixel's value has turned to 1123715645. I am reading the pixel value with:

with rasterio.open('path to tif file') as dataset:
    band = dataset.read(1)
    print(type(band))
    value_of_interest = band[row_of_interest, column_of_interest]
    print(value_of_interest)

Why does this happen?

How can I preserve the height values in same pixel position?

3

1 Answer 1

3

Pillow isn't a geospatial library, I recommend looking at rasterio resampling

For example:

import rasterio
from rasterio.enums import Resampling

# Register GDAL format drivers and configuration options with a
# context manager.
with rasterio.Env():

    with rasterio.open('/path/to/input.tif') as dataset:
        data = dataset.read(1, out_shape=(1200, 1200), resampling=Resampling.bilinear)
        # scale image transform
        transform = dataset.transform * dataset.transform.scale(
            (dataset.height / data.shape[0]),  # rows
            (dataset.width / data.shape[1])  # cols
        )

        profile = dataset.profile
        profile.update(transform=transform, width=data.shape[1], height=data.shape[0])

    with rasterio.open('/path/to/output.tif', 'w', **profile) as dataset:
        dataset.write(data, 1)
6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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