Image21-INT4 / scripts /editing_protocol.py
ixim's picture
Release verified Image21-INT4 conversion
9116984 verified
Raw
History Blame Contribute Delete
1.23 kB
"""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)