DeveloperBreeze

Create and Save Random Color Grid as PNG Image


import numpy as np
from PIL import Image

# Generate a 10x10 grid of random RGB colors
grid_size = (10, 10)
grid = np.random.randint(0, 256, (*grid_size, 3), dtype=np.uint8)
print("Generated a random 10x10 grid of colors.")

# Convert the grid into a PIL Image
image = Image.fromarray(grid)
print("Converted the grid into a PIL Image.")

# Scale up the image to 512x512 pixels using Nearest Neighbor interpolation
scaled_size = (512, 512)
image = image.resize(scaled_size, resample=Image.NEAREST)
print(f"Scaled up the image to {scaled_size} pixels.")

# Save the resulting image as 'result.png'
output_file = 'result.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

Step 2: Set Up the SVG Canvas

D3.js primarily uses SVG (Scalable Vector Graphics) to create visualizations. We need to set up an SVG canvas where the chart will be drawn.

Aug 30, 2024
Read More
Tutorial

Getting Started with ApexCharts

ApexCharts are responsive by default, but you can further customize how charts behave on different screen sizes.

var options = {
    chart: {
        type: 'line',
        height: '100%',
        width: '100%'
    },
    series: [{
        name: 'Sales',
        data: [10, 20, 15, 30, 25, 40, 35]
    }],
    xaxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
    },
    responsive: [{
        breakpoint: 480,
        options: {
            chart: {
                width: '100%'
            },
            legend: {
                position: 'bottom'
            }
        }
    }]
}

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

chart.render();

Aug 21, 2024
Read More
Code
python

Generate and Save Multiple Randomly Colored Grids with Unique IDs

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!