You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Python多集合排列结果生成3×3网格全量组合PNG图片

Generate 3x3 Grid Images from Permutations of A/B/C

First, you’ll need the Pillow library for image manipulation—install it if you haven’t already:

pip install pillow

Step-by-Step Implementation

Here’s how to turn your permutation list into actual 3x3 grid images that meet your requirements:

  1. Load Base Images: First, we’ll load your A.png, B.png, and C.png into memory. Ensure these files are in the same folder as your script (or adjust the file paths if they’re stored elsewhere).
  2. Calculate Canvas Dimensions: Based on your specs (1000x1000 images with 1000px horizontal gaps between them), each grid will be:
    • Width: 3 * 1000 + 2 * 1000 = 5000px (3 images plus 2 gaps between them)
    • Height: 3 * 1000 = 3000px (assuming no vertical gaps—we’ll make this configurable if you need it)
  3. Iterate Over Permutations: For each permutation in your list:
    • Split the 9-element permutation into 3 rows of 3 elements each.
    • Create a blank canvas with the calculated dimensions.
    • Paste each image into its correct position on the canvas.
    • Save the canvas as a unique PNG file (e.g., grid_0001.png to grid_1680.png to keep files ordered).

Full Code Example

from sympy.utilities.iterables import multiset_permutations
from PIL import Image

# Configuration (tweak these values as needed)
IMAGE_SIZE = 1000
HORIZONTAL_GAP = 1000
VERTICAL_GAP = 0  # Set to 1000 if you want vertical gaps between rows
BASE_IMAGES = {
    'A': Image.open('A.png'),
    'B': Image.open('B.png'),
    'C': Image.open('C.png')
}

# Calculate final canvas size
canvas_width = 3 * IMAGE_SIZE + 2 * HORIZONTAL_GAP
canvas_height = 3 * IMAGE_SIZE + 2 * VERTICAL_GAP

# Get all valid permutations of A/B/C (3 each)
permutations = list(multiset_permutations(['A','A','A','B','B','B','C','C','C']))

# Generate and save each grid image
for idx, perm in enumerate(permutations, start=1):
    # Create blank canvas (use 'RGBA' instead of 'RGB' if your images have transparency)
    canvas = Image.new('RGB', (canvas_width, canvas_height), color='white')
    
    # Split permutation into 3 rows
    rows = [perm[0:3], perm[3:6], perm[6:9]]
    
    # Paste each image into its position
    for row_num, row in enumerate(rows):
        for col_num, img_key in enumerate(row):
            # Calculate x/y coordinates for pasting
            x = col_num * (IMAGE_SIZE + HORIZONTAL_GAP)
            y = row_num * (IMAGE_SIZE + VERTICAL_GAP)
            canvas.paste(BASE_IMAGES[img_key], (x, y))
    
    # Save with padded filename to maintain order
    filename = f'grid_{idx:04d}.png'
    canvas.save(filename, optimize=True)  # optimize=True reduces file size
    print(f'Saved {filename}')

print('All 1680 grid images generated successfully!')

Key Customization Tips

  • Transparency: If your base images have transparent backgrounds, change Image.new('RGB', ...) to Image.new('RGBA', ...) to preserve transparency in the final grids.
  • Vertical Gaps: To add vertical spacing between rows, set VERTICAL_GAP = 1000 (or any value you prefer)—the canvas height will adjust automatically.
  • File Size: The optimize=True flag in save() compresses the PNG files to save disk space (critical for 1680 images!).
  • Background Color: Replace color='white' with any other color (e.g., 'black') or use a tuple like (255,255,255) for custom RGB values.

内容的提问来源于stack exchange,提问作者mfg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:02:32