Last active
May 4, 2025 15:21
-
-
Save kabirnayeem99/4a2eac45fb4eadde609e68969ee27b6e to your computer and use it in GitHub Desktop.
Generate Blank very large image for Image Uploading Testing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# requires-python = ">=3.13.3" | |
# dependencies = [ | |
# "Pillow", | |
# "colorama", | |
# ] | |
# /// | |
import sys | |
import argparse | |
import random | |
import math | |
from typing import Tuple, Dict, Optional | |
from PIL import Image, ImageDraw, ImageFont | |
import os | |
from colorama import init, Fore, Style | |
init(autoreset=True) | |
def generate_pastel_color() -> Tuple[int, int, int]: | |
return tuple(random.randint(180, 255) for _ in range(3)) | |
def darker_color(rgb: Tuple[int, int, int], factor: float = 0.6) -> Tuple[int, int, int]: | |
return tuple(max(0, int(c * factor)) for c in rgb) | |
def rgb_to_hex(rgb: Tuple[int, int, int]) -> str: | |
return ''.join(f'{c:02X}' for c in rgb) | |
def get_translations() -> Dict[str, str]: | |
return { | |
"en": "Cars", # English | |
"ja": "車", # Japanese | |
"ur": "گاڑیاں", # Urdu | |
"bn": "গাড়ি", # Bengali | |
"hi": "कारें", # Hindi | |
"ro": "Mașini", # Romanian | |
"hy": "Մեքենաներ", # Armenian | |
"ko": "차들", # Korean | |
"ug": "ئاپتوموبىللار", # Uyghur | |
"ar": "سيارات", # Arabic | |
"fa": "خودروها", # Persian | |
"zh": "汽车", # Chinese | |
"ru": "Автомобили", # Russian | |
"kk": "Көліктер", # Kazakh | |
"tr": "Arabalar", # Turkish | |
} | |
def load_font(size: int) -> ImageFont.FreeTypeFont: | |
try: | |
return ImageFont.truetype("noto.ttf", size=size) | |
except: | |
print("⚠️ Arial font not found. Using default font — some characters may not render.") | |
return ImageFont.load_default() | |
def add_text_overlay(image: Image.Image, text: Dict[str, str], color: Tuple[int, int, int]) -> None: | |
draw = ImageDraw.Draw(image) | |
width, height = image.size | |
margin = 30 | |
for _ in range(4): | |
lang, translation = random.choice(list(text.items())) | |
label = f"{translation}" | |
font_size = random.randint(image.height // 100, image.height // 10) | |
font = load_font(font_size) | |
x = random.randint(margin, width - margin) | |
y = random.randint(margin, height - margin) | |
text_width, text_height = draw.textbbox((0, 0), label, font=font)[2:] | |
x = min(x, width - text_width - margin) | |
y = min(y, height - text_height - margin) | |
draw.text((x, y), label, font=font, fill=color) | |
def create_gradient(width: int, height: int, color1: Tuple[int, int, int], color2: Tuple[int, int, int]) -> Image.Image: | |
gradient = Image.new('RGB', (width, height), color1) | |
for y in range(height): | |
r = int(color1[0] + (color2[0] - color1[0]) * (y / height)) | |
g = int(color1[1] + (color2[1] - color1[1]) * (y / height)) | |
b = int(color1[2] + (color2[2] - color1[2]) * (y / height)) | |
for x in range(width): | |
gradient.putpixel((x, y), (r, g, b)) | |
return gradient | |
def add_random_layer(image: Image.Image, width: int, height: int) -> None: | |
draw = ImageDraw.Draw(image, "RGBA") | |
opacity = random.randint(50, 150) | |
x = random.randint(0, width) | |
y = random.randint(0, height) | |
radius = random.randint(10, 30) | |
color = tuple(random.randint(180, 255) for _ in range(3)) + (opacity,) | |
draw.ellipse([x - radius, y - radius, x + radius, y + radius], fill=color) | |
def add_random_details(image: Image.Image, width: int, height: int) -> None: | |
draw = ImageDraw.Draw(image, "RGBA") | |
for _ in range(random.randint(30, 50)): | |
x1, y1 = random.randint(0, width), random.randint(0, height) | |
x2, y2 = random.randint(0, width), random.randint(0, height) | |
color = tuple(random.randint(180, 255) for _ in range(3)) + (random.randint(100, 200),) | |
draw.line([x1, y1, x2, y2], fill=color, width=random.randint(1, 3)) | |
for _ in range(random.randint(5, 10)): | |
x1, y1 = random.randint(0, width), random.randint(0, height) | |
side = random.randint(10, 30) | |
color = tuple(random.randint(180, 255) for _ in range(3)) + (random.randint(100, 200),) | |
draw.rectangle([x1, y1, x1 + side, y1 + side], fill=color, outline=None) | |
def generate_images(count: int, target_mb: Optional[int] = None) -> None: | |
translations = get_translations() | |
for i in range(count): | |
size_mb = target_mb or random.randint(1, 10) | |
pastel_rgb = generate_pastel_color() | |
dark_rgb = darker_color(pastel_rgb) | |
hex_color = rgb_to_hex(pastel_rgb) | |
width, height = 3840, 2160 | |
print(f"{Fore.YELLOW} Creating ~{size_mb}MB PNG: {width}x{height}, Color: #{hex_color}") | |
gradient = create_gradient(width, height, pastel_rgb, darker_color(pastel_rgb)) | |
add_random_layer(gradient, width, height) | |
add_random_details(gradient, width, height) | |
add_text_overlay(gradient, translations, dark_rgb) | |
file_size = 0 | |
filename = f"test_image_{hex_color}.png" | |
while file_size < size_mb * 1024 * 1024: | |
gradient.save(filename, format="PNG", optimize=False) | |
file_size = os.path.getsize(filename) | |
percent = min(100, int(file_size / (size_mb * 1024 * 1024) * 100)) | |
bar = "█" * (percent // 4) + "-" * (25 - percent // 4) | |
print(f"{Fore.BLUE}[{bar}] {percent}% — {file_size / 1024 / 1024:.2f} MB", end='\r') | |
if file_size < size_mb * 1024 * 1024: | |
add_random_details(gradient, width, height) | |
add_text_overlay(gradient, translations, dark_rgb) | |
print(f"{Fore.GREEN} Saved: {filename}", end='\r') | |
def main() -> None: | |
parser = argparse.ArgumentParser( | |
description="Generate 4K pastel PNG images with multi-language text overlay and gradient backgrounds." | |
) | |
parser.add_argument("--size", type=int, help="Target image size in MB (default: random 1–10 MB)") | |
parser.add_argument("--count", type=int, default=10, help="Number of images to generate (default: 10)") | |
args = parser.parse_args() | |
generate_images(count=args.count, target_mb=args.size) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment