example: Read from file#
Relevant documentation links:
#include <string>
#include <filesystem>
#include <algorithm>
#include <execution>
#include <compressed/image.h>
#include <compressed/ranges.h>
auto main() -> int
{
std::string name = "uv_grid_2048x2048.jpg";
auto path = std::filesystem::current_path() / "images" / name;
// Read the image in chunks without having to load the whole file into memory, this is roughly as fast
// or only slightly slower than reading the image data raw through OpenImageIO while taking only a fraction
// of the memory.
auto image = compressed::image<uint8_t>::read(path);
// Get references to the channels as std::tuple<compressed::channel ...>
auto [r, g, b] = image.channels("R", "G", "B");
// Now iterate all of them together, first by their chunks, then by their pixels within the chunks.
// If not all channels are the same size we will iterate to the lowest common denominator!
for (auto [chunk_r, chunk_g, chunk_b] : compressed::ranges::zip(r, g, b))
{
auto chunk_gen = compressed::ranges::zip(chunk_r, chunk_g, chunk_b);
std::for_each(std::execution::par_unseq, chunk_gen.begin(), chunk_gen.end(), [](auto pixels)
{
auto& [r_pixel, g_pixel, b_pixel] = pixels;
r_pixel = 12;
g_pixel = 12;
b_pixel = 12;
});
}
}
Relevant documentation links:
import os
import compressed_image as compressed
import numpy as np
filepath = os.path.join(os.path.dirname(__file__), "images/uv_grid_2048x2048.jpg")
# Read the image in chunks without having to load the whole file into memory, this is roughly as fast
# or only slightly slower than reading the image data raw through OpenImageIO while taking only a fraction
# of the memory.
image = compressed.Image.read(np.uint8, filepath, subimage=0)
# Get references to the channels, to get a list of all channels use get_channel_names
r = image.channel("R")
g = image.channel("G")
b = image.channel("B")
# Now iterate e.g. channel r and modify its value (it is perfectly valid to modify multiple
# channels at once but for demonstration purposes we only modify one).
for chunk_index in range(r.num_chunks()):
chunk_r = r.get_chunk(chunk_index)
chunk_r[:] = 25
r.set_chunk(chunk_index, chunk_r)