Pick up random pictures from a pool and generate a new one by blending them
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

200 lines
5.2 KiB

import os
from PIL import Image, ImageChops, ImageEnhance, ImageFilter, ImageOps
import random
import config
import time
BLEND_TECH = ['multiply', 'screen', 'subtract']
EFFECT_TECH = ['filter', 'enhance', 'mirror', 'color', 'detail']
# return a random folder path in the local media folder
def get_folder():
flist = config.FOLDERS
# return listdir(l)[random.randrange(len(listdir(l)) - 1)] + '/'
return flist[random.randrange(len(flist)) - 1] + '/'
# return a (random) file path
def get_file():
f = get_folder()
return f + os.listdir(f)[random.randrange(len(os.listdir(f)) - 1)]
# return a list of image paths, length between min and max
def get_file_list(min=3, max=7):
lst = []
for f in range(random.randrange(min, max)):
lst.append(get_file())
return lst
# return a list of PIL Image objects
def get_img_list():
fl = get_file_list()
img_file_list = []
for n in fl:
if not os.path.isdir(n):
img_file_list.append(Image.open(n))
return img_file_list
# return a list of images suitable for PIL transform functions to work on :
# single elements for "simple" effect like blur and mirroring, tuple of two
# images for effects like blending requiring more than one images as args
def get_working_list():
img_list = resize(get_img_list())
lst = []
for x, y in zip(img_list[:-1], img_list[1:]):
if ((random.randint(0, 100) % 2) == 0):
lst.append((x, y))
else:
lst.append(x)
lst.append(y)
return lst
# return a list of PIL Image objects resized to size
def resize(img_list, size=(1080, 1080)):
for index, n in enumerate(img_list):
n = n.resize(size)
img_list[index] = n
return img_list
# return a blended PIL Image object of img1
# and img2 with the given opacity amount
def blend(img1, img2, opacity=round(random.uniform(0.2, 0.6), 1)):
return Image.blend(img1, img2, opacity)
def subtract(img1, img2):
img = ImageChops.subtract(img1, img2)
return img
# return a PIL Image object of surimposed img1 and img2
def multiply(img1, img2):
img = ImageChops.multiply(img1, img2)
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(round(random.random(), 1)+0.5)
return img
# return a PIL Image object of screened img1 and img2
def screen(img1, img2):
img = ImageChops.screen(img1, img2)
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(round(random.random(), 1)+0.2)
return img
# apply to the given img a random filter from the ImageFilter module
def filter(img):
filters = [
ImageFilter.MinFilter,
ImageFilter.MaxFilter,
ImageFilter.MedianFilter,
ImageFilter.ModeFilter]
return img.filter(random.choice(filters)())
# blur or sharpen a given img
def detail(img):
details = [
ImageFilter.EDGE_ENHANCE_MORE,
ImageFilter.SHARPEN,
ImageFilter.SMOOTH_MORE,
ImageFilter.EMBOSS,
ImageFilter.GaussianBlur]
return img.filter(random.choice(details)())
# apply various color operations on the given image
def color(img):
colors = [
ImageOps.equalize,
ImageOps.invert,
ImageOps.solarize]
img = random.choice(colors)(img)
return img
# apply a random enhance transformation from the ImageEnhance module
def enhance(img):
enhance = [
ImageEnhance.Color,
ImageEnhance.Sharpness]
enhancer = random.choice(enhance)(img)
img = enhancer.enhance(round(random.random(), 1) + 0.3)
return img
# flip a given img either vertically or horizontally
def mirror(img):
img = random.choice([ImageOps.flip, ImageOps.mirror])(img)
return img
# Return percent of perceveid brightness of a given img
def calculate_brightness(img):
datas = img.getdata()
w, h = img.size
pixels_count = w * h
bright = []
for r, g, b in datas:
bright.append((0.299*r + 0.587*g + 0.114*b))
return round((sum(bright) / pixels_count), 2)
# brigthen or darken given img
def correct_screen(img, amount):
enhancer = ImageEnhance.Brightness(img)
img = enhancer.enhance(amount)
return img
def auto_correct_screen(img):
if calculate_brightness(img) > 110:
img = correct_screen(img, 0.8)
elif calculate_brightness(img) < 50:
img = correct_screen(img, 1.3)
return img
# brigthen or darken given img
def correct_contrast(img, amount):
enhancer = ImageEnhance.Contrast(img)
img = enhancer.enhance(amount)
return img
def test3():
il = resize(get_img_list())
default_size = (1080, 1080)
img = Image.new('RGBA', default_size)
filename = config.OUTPUT_FOLDER + str(round(time.time())) + ".png"
for n in il:
for i in range(random.randint(2, 5)):
modifier = globals()[(random.choice(EFFECT_TECH))]
img = modifier(n)
img = auto_correct_screen(img)
for x, y in zip(il[:-1], il[1:]):
for i in range(random.randint(2, 5)):
modifier = globals()[(random.choice(BLEND_TECH))]
img = modifier(x, y)
img = auto_correct_screen(img)
img.save(filename)
return img
def main():
test3()
if __name__ == "__main__":
main()