in c code: write a code in c to draw a 128x128 pixel image with each pixel value decided by the user and save the data in a png file, also the possibility to load the image and modify specific pixel in python

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <png.h>

#define IMG_WIDTH 128
#define IMG_HEIGHT 128

void drawImage(uint8_t* pixels) {
    // Create 24-bit RGB image
    uint8_t* image = (uint8_t*) calloc(IMG_WIDTH * IMG_HEIGHT * 3, sizeof(uint8_t));

    for(int i = 0; i < IMG_HEIGHT; i++) {
        for(int j = 0; j < IMG_WIDTH; j++) {
            int index = (i * IMG_WIDTH + j) * 3;

            // Set pixel color
            int pixel_val = pixels[i * IMG_WIDTH + j];
            image[index] = pixel_val;
            image[index + 1] = pixel_val;
            image[index + 2] = pixel_val;
        }
    }

    // Write image to PNG file
    FILE* fp = fopen("output.png", "wb");
    if(!fp) {
        fprintf(stderr, "Unable to open file for writing\n");
        exit(EXIT_FAILURE);
    }

    png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if(!png) {
        fprintf(stderr, "Unable to create PNG write struct\n");
        exit(EXIT_FAILURE);
    }

    png_infop info = png_create_info_struct(png);
    if(!info) {
        fprintf(stderr, "Unable to create PNG info struct\n");
        exit(EXIT_FAILURE);
    }

    if(setjmp(png_jmpbuf(png))) {
        fprintf(stderr, "Unable to write PNG image\n");
        exit(EXIT_FAILURE);
    }

    png_init_io(png, fp);

    png_set_IHDR(png, info, IMG_WIDTH, IMG_HEIGHT, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

    png_write_info(png, info);

    uint8_t** row_pointers = (uint8_t**) malloc(sizeof(uint8_t*) * IMG_HEIGHT);
    for(int i = 0; i < IMG_HEIGHT; i++) {
        row_pointers[i] = &image[i * IMG_WIDTH * 3];
    }

    png_write_image(png, row_pointers);

    png_write_end(png, NULL);

    // Clean up
    png_destroy_write_struct(&png, &info);
    free(row_pointers);
    free(image);
    fclose(fp);

    printf("Image saved to output.png\n");
}

int main(int argc, char** argv) {
    // Set pixel values
    uint8_t pixels[IMG_WIDTH * IMG_HEIGHT] = { 0 };
    for(int i = 0; i < IMG_HEIGHT; i++) {
        for(int j = 0; j < IMG_WIDTH; j++) {
            int index = i * IMG_WIDTH + j;
            pixels[index] = (i + j) % 256;
        }
    }

    // Draw image and save to file
    drawImage(pixels);

    return 0;
}
2313 chars
88 lines

To load and modify specific pixel values in Python, you can use the Pillow library. Here's an example code:

main.py
from PIL import Image

# Load image and get pixel values
im = Image.open("output.png")
pixels = im.load()

# Set specific pixel value
pixels[10, 10] = (255, 255, 255)

# Save modified image
im.save("modified.png")
214 chars
12 lines

This code loads the output.png image, gets its pixel values using the load() method, and sets the pixel at coordinates (10, 10) to white. Finally, it saves the modified image as modified.png.

gistlibby LogSnag