0

I'm using rasterio to analyze satellite imagery stored in JPEG2000 format and am having the problem that pixel values change when opening and then saving the same file again. Any ideas why this happens?

with rasterio.open('/tmp/in.jp2') as infile:
    with rasterio.open('/tmp/out.jp2', 'w', count=infile.count, dtype=infile.dtypes[0], height=infile.height, width=infile.width, crs=infile.crs, transform=infile.transform) as outfile:
        outfile.write(infile.read())
with rasterio.open('/tmp/in.jp2') as f:
    data = f.read()
    print(data.min(), data.max())
    # output: 0 19614

with rasterio.open('/tmp/out.jp2') as f:
    data = f.read()
    print(data.min(), data.max())
    # output: 0 19596

Pierrick Rambaud
  • 2,788
  • 1
  • 14
  • 49

1 Answers1

0

My humble guess as I don't have the input file to try is that you are relying on some default parameter when writing the ou.jp2 file.

could you try to copy all the metadata when writing the file instead:

with rasterio.open('/tmp/in.jp2') as infile:
kwargs = infile.profile.copy()

with rasterio.open('/tmp/out.jp2', 'w', **kwargs) as outfile:
    outfile.write(infile.read())

Pierrick Rambaud
  • 2,788
  • 1
  • 14
  • 49