Color of a tif image is changed when loaded and saved

I am loading a tif file with rasterio to cut it to pieces and save the pieces. However, the saved pieces look different from the loaded file. The file is ortho photo map shot from a plane and has 4 layers – R, G, B, NIR. The input file is a high resolution raster with some 15×15 cm per pixel, see sample image:

When I tried to just load it to python and save it as is the image changes to this:

I have no idea what might be wrong, can it be due to the memory constraint? I have tried many different “fixes” as are added in code but none helped. The original file has 129MB and the saved from raster.io 476MB – why is that? All chunks have about 15-18MB and look exactly as faded as the second image.

Here is my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import rasterio
from rasterio.windows import Window
import os
# Function to split raster
def split_raster(image_path, output_folder, chunk_width, chunk_height):
with rasterio.open(image_path) as src:
# Get the dimensions of the image
img_width = src.width
img_height = src.height
# Calculate the number of chunks in both dimensions
num_chunks_x = (img_width + chunk_width - 1) // chunk_width # Ensures all parts are included
num_chunks_y = (img_height + chunk_height - 1) // chunk_height # Ensures all parts are included
# Ensure the output directory exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Loop over chunks and save them
for i in range(num_chunks_x):
for j in range(num_chunks_y):
# Define the window to crop
window = Window(i * chunk_width, j * chunk_height, chunk_width, chunk_height)
# Adjust window size if it exceeds image bounds (handling edge cases)
window = window.intersection(Window(0, 0, img_width, img_height))
# Read the data from the window
chunk_data = src.read(window=window)
# Define the metadata for the chunk
chunk_transform = src.window_transform(window)
chunk_meta = src.meta.copy()
chunk_meta.update({
"driver": "GTiff",
"height": window.height,
"width": window.width,
"transform": chunk_transform,
"compress": "lzw", # Using LZW compression to preserve color integrity
"count": src.count, # Ensure all channels are included
"dtype": src.dtypes[0] # Ensure the data type is preserved
})
if src.nodata is not None:
chunk_meta.update({"nodata": src.nodata})
chunk_meta.update({
"photometric": src.profile.get("photometric", "RGB") # Ensure correct color interpretation
})
# Save the chunk
chunk_name = f"chunk_{i}_{j}.tif"
chunk_path = os.path.join(output_folder, chunk_name)
with rasterio.open(chunk_path, "w", **chunk_meta) as dst:
dst.write(chunk_data)
print(f"Saved {chunk_path}")
if j==4:
break
break
# Define parameters
image_path = '../Code/ZnH/B6-1.tif' # Path to the large raster image
output_folder = "../Code/ZnH/splits" # Folder to save the chunks
chunk_width = 512 # Width of each chunk
chunk_height = 512 # Height of each chunk
# Split the raster
split_raster(image_path, output_folder, chunk_width, chunk_height)
</code>
<code>import rasterio from rasterio.windows import Window import os # Function to split raster def split_raster(image_path, output_folder, chunk_width, chunk_height): with rasterio.open(image_path) as src: # Get the dimensions of the image img_width = src.width img_height = src.height # Calculate the number of chunks in both dimensions num_chunks_x = (img_width + chunk_width - 1) // chunk_width # Ensures all parts are included num_chunks_y = (img_height + chunk_height - 1) // chunk_height # Ensures all parts are included # Ensure the output directory exists if not os.path.exists(output_folder): os.makedirs(output_folder) # Loop over chunks and save them for i in range(num_chunks_x): for j in range(num_chunks_y): # Define the window to crop window = Window(i * chunk_width, j * chunk_height, chunk_width, chunk_height) # Adjust window size if it exceeds image bounds (handling edge cases) window = window.intersection(Window(0, 0, img_width, img_height)) # Read the data from the window chunk_data = src.read(window=window) # Define the metadata for the chunk chunk_transform = src.window_transform(window) chunk_meta = src.meta.copy() chunk_meta.update({ "driver": "GTiff", "height": window.height, "width": window.width, "transform": chunk_transform, "compress": "lzw", # Using LZW compression to preserve color integrity "count": src.count, # Ensure all channels are included "dtype": src.dtypes[0] # Ensure the data type is preserved }) if src.nodata is not None: chunk_meta.update({"nodata": src.nodata}) chunk_meta.update({ "photometric": src.profile.get("photometric", "RGB") # Ensure correct color interpretation }) # Save the chunk chunk_name = f"chunk_{i}_{j}.tif" chunk_path = os.path.join(output_folder, chunk_name) with rasterio.open(chunk_path, "w", **chunk_meta) as dst: dst.write(chunk_data) print(f"Saved {chunk_path}") if j==4: break break # Define parameters image_path = '../Code/ZnH/B6-1.tif' # Path to the large raster image output_folder = "../Code/ZnH/splits" # Folder to save the chunks chunk_width = 512 # Width of each chunk chunk_height = 512 # Height of each chunk # Split the raster split_raster(image_path, output_folder, chunk_width, chunk_height) </code>
import rasterio
from rasterio.windows import Window
import os


# Function to split raster
def split_raster(image_path, output_folder, chunk_width, chunk_height):
    with rasterio.open(image_path) as src:
        # Get the dimensions of the image

        img_width = src.width
        img_height = src.height

        # Calculate the number of chunks in both dimensions
        num_chunks_x = (img_width + chunk_width - 1) // chunk_width  # Ensures all parts are included
        num_chunks_y = (img_height + chunk_height - 1) // chunk_height  # Ensures all parts are included

        # Ensure the output directory exists
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)

        # Loop over chunks and save them
        for i in range(num_chunks_x):
            for j in range(num_chunks_y):
                # Define the window to crop
                window = Window(i * chunk_width, j * chunk_height, chunk_width, chunk_height)

                # Adjust window size if it exceeds image bounds (handling edge cases)
                window = window.intersection(Window(0, 0, img_width, img_height))

                # Read the data from the window
                chunk_data = src.read(window=window)

                # Define the metadata for the chunk
                chunk_transform = src.window_transform(window)
                chunk_meta = src.meta.copy()
                chunk_meta.update({
                    "driver": "GTiff",
                    "height": window.height,
                    "width": window.width,
                    "transform": chunk_transform,
                    "compress": "lzw",  # Using LZW compression to preserve color integrity
                    "count": src.count,  # Ensure all channels are included
                    "dtype": src.dtypes[0]  # Ensure the data type is preserved
                })

                if src.nodata is not None:
                    chunk_meta.update({"nodata": src.nodata})

                chunk_meta.update({
                    "photometric": src.profile.get("photometric", "RGB")  # Ensure correct color interpretation
                })

                # Save the chunk
                chunk_name = f"chunk_{i}_{j}.tif"
                chunk_path = os.path.join(output_folder, chunk_name)

                with rasterio.open(chunk_path, "w", **chunk_meta) as dst:
                    dst.write(chunk_data)

                print(f"Saved {chunk_path}")
                if j==4:
                    break
            break


# Define parameters
image_path = '../Code/ZnH/B6-1.tif'  # Path to the large raster image
output_folder = "../Code/ZnH/splits"  # Folder to save the chunks
chunk_width = 512  # Width of each chunk
chunk_height = 512  # Height of each chunk

# Split the raster
split_raster(image_path, output_folder, chunk_width, chunk_height)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật