1

When I read a raster and then save it with rasterio the pixels are sligthly modified. Code example:

import rasterio

inputFile = './band3.jp2'
with rasterio.open(inputFile) as b3:
    v3 = b3.read(1)
    print('Before: ',v3)
    profile3 = b3.profile

##Saving raster
output='./test.jp2'
with rasterio.open(output, 'w', **profile3) as dst:
    dst.write(v3, 1)

##Reading the raster I just saved
with rasterio.open(output) as b3:
    v3 = b3.read(1)
    print('After: ',v3)

This outputs:

Before:  
         [[  0   0   0 ... 665 632 661]
         [  0   0   0 ... 728 687 704]
         [  0   0   0 ... 703 702 700]
         ...
         [  0   0   0 ... 613 605 603]
         [  0   0   0 ... 610 600 605]
         [  0   0   0 ... 608 606 607]]

Warning 6: driver JP2OpenJPEG does not support creation option TILED #I don't think this is important

After:  
        [[ 11   0   0 ... 663 629 655]
        [  0   0   0 ... 717 680 687]
        [  0   0   0 ... 689 693 687]
        ...
        [  1   0   0 ... 604 597 587]
        [  3   0   0 ... 604 595 593]
        [  0   0   0 ... 594 594 589]]

I'm not sure if it is important because I didn't modify it but the profile looks like:

{'driver': 'JP2OpenJPEG', 'dtype': 'uint16', 'nodata': None, 'width': 10980, 'height': 10980, 'count': 1, 'crs': CRS.from_epsg(32615), 'transform': Affine(10.0, 0.0, 600000.0,
   0.0, -10.0, 1600020.0), 'blockxsize': 1024, 'blockysize': 1024, 'tiled': True}
1
  • 4
    JPEG2000 is a lossy format, use something lossless like TIFF if you need to maintain the exact pixel values
    – mikewatt
    Mar 17, 2020 at 19:41

2 Answers 2

2

As @mikewatt commented changing the driver to 'GTiff' works:

profile3['driver']='GTiff'
2

JPEG2000 can be lossy or lossless. The GDAL JP2OpenJPEG driver is using the lossy method by default unless a lossless compression is explicitly requested with the creation option "REVERSIBLE=YES". See the driver documentation https://gdal.org/drivers/raster/jp2openjpeg.html

REVERSIBLE=YES/NO : YES means use of reversible 5x3 integer-only filter, NO use of the irreversible DWT 9-7. Defaults to NO (unless the dataset is made of a single band with color table, in which case reversible filter is used).

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.