How to rescale the image after rio-color operations?
See original GitHub issueAccording to documentation, rio-color operations only take rasters of dimensions (3, x, y) for processing, in 0 to 1 range for pixel values.
I’m facing some issues getting the image into the same state as doing rio color --co photometric=rgb stack.tiff landsat8_color.tiff sigmoidal RGB 20 0.2 -j 1
The code I’ve used is as follows.
# initializing and reading the bands
import rasterio as rio
import numpy as np
from rio_color import operations, utils
R10 = '/Users/shivashis.ext/felicette-data/LC81390462020136'
b4 = rio.open(R10+'/LC81390462020136-b4.tiff')
b3 = rio.open(R10+'/LC81390462020136-b3.tiff')
b2 = rio.open(R10+'/LC81390462020136-b2.tiff')
# getting the bands in the range of [0..1]
r = b4.read(1)
g = b3.read(1)
b = b2.read(1)
norm_r = np.linalg.norm(r)
norm_g = np.linalg.norm(g)
norm_b = np.linalg.norm(b)
r = r / norm_r
g = g / norm_g
b = b / norm_b
# making and processing image
img = np.array([r,g,b])
img = operations.sigmoidal(img, 20, 0.2)
# from matplotlib import pyplot as plt
norm_r = img[0]
norm_r = utils.scale_dtype(img[0], np.uint16)# np.interp(norm_r, (norm_r.min(), norm_r.max()), (0, 65535))
norm_g = img[1]
norm_g = utils.scale_dtype(img[1], np.uint16)#np.interp(norm_g, (norm_g.min(), norm_g.max()), (0, 65535))
norm_b = img[2]
norm_b = utils.scale_dtype(img[2], np.uint16)#np.interp(norm_b, (norm_b.min(), norm_b.max()), (0, 65535))
# writing back to file
out_tiff = R10 + '/stack_prog_color_2.tiff'
with rio.open(out_tiff,'w',driver='Gtiff', width=b4.width, height=b4.height,
count=3,crs=b4.crs,transform=b4.transform, dtype=np.uint16, photometric="RGB") as rgb:
rgb.write(norm_r.astype(np.uint16),1)
rgb.write(norm_g.astype(np.uint16),2)
rgb.write(norm_b.astype(np.uint16),3)
rgb.close()
As one can see, I’ve used both in-house scale_dtype and Numpy’s linear interpolation to scale the array back, without success.
Also, I planned to save the numpy response of sigmoid function as a pickle file, and debug keeping it as reference, but since the job is parallel by rio-mucho, it got too complex in very short time.
I am almost sure that I’m scaling the image back wrong, because size of the the output tiffs with a) command line and b) Python API are same. (Both use np.uint16 to store data)
Please let me also know, if any other detail is required to understand/debug/help this issue.
Thank you for your time in advance!
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)

Top Related StackOverflow Question
@plant99 I appreciate the offer of documentation help! If there is anything incorrect in the readme, we’d love to fix it. However, the team maintaining this package is occupied with other work and doesn’t have the time to review major changes.
Thank you @sgillies , what you described solved this issue.
Do you also feel that Python implementation’s documentation has to be improved a bit in README.md? Please let me know if I can help with a PR.
Have a nice day!