File size: 1,228 Bytes
9116984
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Editing inputs: independent noise and unchanged RGBA pixels."""
import math
from pathlib import Path

from PIL import Image, ImageOps

DEFAULT_SEEDS = (1000042, 1000123)


def validate_seeds(cases, seeds):
    if (not seeds or len(set(seeds)) != len(seeds)
            or any(type(s) is not int or not 0 <= s < 2**63 for s in seeds)):
        raise ValueError('Use distinct integer seeds in [0, 2**63)')
    source_seeds = {c.get('source_seed') for c in cases} - {None}
    if source_seeds.intersection(seeds):
        raise ValueError('Editing seeds must differ from every known source seed (noise replay)')
    return list(seeds)


def load_input(path):
    with Image.open(path) as image:
        image = ImageOps.exif_transpose(image)
        has_alpha = 'A' in image.getbands() or 'transparency' in image.info
        return image.convert('RGBA' if has_alpha else 'RGB')


def edit_dimensions(size, resolution):
    if resolution < 32 or resolution % 32 or min(size) <= 0:
        raise ValueError('Positive image size and resolution divisible by 32 required')
    ratio = size[0] / size[1]
    width = math.sqrt(resolution**2 * ratio)
    return max(32, round(width / 32) * 32), max(32, round(width / ratio / 32) * 32)