Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Given a set of images from the project thisartworkdoesnotexist.com, this script generates 16 imagens combining symmetries extracts from the image's quadrants.
# Author: Berin
# Sketches repo: https://github.com/berinhard/sketches
# requirements: pillow tqdm
import os
from multiprocessing import Pool
from PIL import Image, ImageOps
from pathlib import Path
from tqdm import tqdm
import shutil
W, H = 800, 800
DIMENSION = (W, H)
def gen_quadrant_symmetry(base_quadrant):
w, h = base_quadrant.size
positions = [
(0, 0),
(w, 0),
(0, h),
(w, h),
]
output = Image.new("RGB", (w * 2, h * 2))
for i, pos in enumerate(positions):
w, h = pos
quadrant_symmetry = base_quadrant.copy()
if h > 0:
quadrant_symmetry = quadrant_symmetry.transpose(Image.FLIP_TOP_BOTTOM)
if w > 0:
quadrant_symmetry = quadrant_symmetry.transpose(Image.FLIP_LEFT_RIGHT)
output.paste(quadrant_symmetry, pos)
return output
def save_region_symmetry(img, region_box, filename, transpose_method=None):
quadrant_img = img.crop(region_box)
if transpose_method is not None:
quadrant_img = quadrant_img.transpose(transpose_method)
symmetry = gen_quadrant_symmetry(quadrant_img)
symmetry.save(filename)
if __name__ == "__main__":
with Pool(16) as p:
artworks = Path("/home/bernardo/workspace/ai-faces/this_artwork_does_not_exist/")
output_dir = Path("/home/bernardo/envs/dark_symmetry/output/")
if not output_dir.exists():
os.mkdir(output_dir)
for image_path in tqdm(artworks.iterdir(), total=2048):
img_num = int(image_path.name.split(".")[0])
img_num = f"{img_num:04d}"
img_dir = output_dir / img_num
if not img_dir.exists():
os.mkdir(img_dir)
shutil.copyfile(image_path, img_dir / f"{img_num}{image_path.suffix}")
img = Image.open(image_path).resize(DIMENSION)
quadrants_regions = [
(0, 0, W//2, H//2),
(W//2, 0, W, H//2),
(0, H//2, W//2, H),
(W//2, H//2, W, H),
]
counter = 0
for region_box in quadrants_regions:
transpose_methods = [
None,
Image.ROTATE_90, # 2
Image.ROTATE_180, # 3
Image.ROTATE_270, # 4
]
args = []
for method in transpose_methods:
counter += 1
filename = img_dir / f"{img_num}-out-{counter:02d}.png"
args.append([img, region_box, filename, method])
p.starmap(save_region_symmetry, args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.