DeveloperBreeze

Generate and Save Multiple Randomly Colored Grids with Unique IDs

import random
import uuid
from PIL import Image, ImageDraw

# Constants for image and rectangle dimensions
IMAGE_SIZE = (2000, 2000)
RECTANGLE_SIZE = (400, 400)
GRID_SIZE = (5, 5)  # Number of rectangles in rows and columns
OUTPUT_DIR = './'  # Directory to save the images

for _ in range(5):  # Generate 5 images
    # Generate a unique run ID
    run_id = uuid.uuid1()
    print(f'Processing run_id: {run_id}')

    # Create a blank image
    image = Image.new('RGB', IMAGE_SIZE)
    draw_image = ImageDraw.Draw(image)

    # Calculate rectangle dimensions
    rect_width, rect_height = RECTANGLE_SIZE
    grid_cols, grid_rows = GRID_SIZE

    # Draw the grid of rectangles with random colors
    for i in range(grid_cols):
        for j in range(grid_rows):
            x = i * rect_width
            y = j * rect_height
            rectangle_shape = [
                (x, y),  # Top-left corner
                (x + rect_width, y + rect_height)  # Bottom-right corner
            ]
            draw_image.rectangle(
                rectangle_shape,
                fill=(
                    random.randint(0, 255),  # Random red
                    random.randint(0, 255),  # Random green
                    random.randint(0, 255)   # Random blue
                )
            )

    # Save the generated image with the run ID as the filename
    output_file = f'{OUTPUT_DIR}{run_id}.png'
    image.save(output_file)
    print(f'Image saved as {output_file}')

Related Posts

More content you might like

Tutorial
javascript

Data Visualization with D3.js and JavaScript: A Comprehensive Guide

Complete Code Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>D3.js Bar Chart</title>
    <script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>
    <div id="chart"></div>
    <script>
        const data = [25, 20, 30, 35, 40];
        const width = 500;
        const height = 300;

        const svg = d3.select("#chart")
            .append("svg")
            .attr("width", width)
            .attr("height", height);

        const xScale = d3.scaleBand()
            .domain(d3.range(data.length))
            .range([0, width])
            .padding(0.1);

        const yScale = d3.scaleLinear()
            .domain([0, d3.max(data)])
            .range([height, 0]);

        svg.selectAll("rect")
            .data(data)
            .enter()
            .append("rect")
            .attr("x", (d, i) => xScale(i))
            .attr("y", d => yScale(d))
            .attr("width", xScale.bandwidth())
            .attr("height", d => height - yScale(d))
            .attr("fill", "steelblue");
    </script>
</body>
</html>

Aug 30, 2024
Read More
Tutorial

Getting Started with ApexCharts

var options = {
    chart: {
        type: 'line'
    },
    series: [{
        name: 'Sales',
        data: [10, 20, 15, 30, 25, 40, 35]
    }],
    xaxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    },
    title: {
        text: 'Monthly Sales Data',
        align: 'center'
    },
    tooltip: {
        enabled: true,
        theme: 'dark'
    },
    legend: {
        position: 'top',
        horizontalAlign: 'right'
    }
}

var chart = new ApexCharts(document.querySelector("#chart"), options);

chart.render();

ApexCharts supports various chart types. Let’s explore some of them.

Aug 21, 2024
Read More
Code
python

Batch File Renaming Using os Module

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Create and Save Random Color Grid as PNG Image

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!