Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- from PIL import Image, ImageFilter
- from PIL.ImageOps import invert
- from numpy import array
- from cv2 import pencilSketch, stylization, cvtColor, COLOR_BGR2RGB
- BACKGROUND_PICS = ["smoke.jpg", "smoke2.jfif"]
- INPUT_PIC = "input.jpg"
- # Make transform matrix, to multiply R by 1.5, leaving G and B unchanged
- Matrix = (
- 0.4, 0.1, 0.1, 0.0,
- 0.1, 0.1, 0.1, 0.0,
- 0.1, 0.1, 0.1, 0.0
- )
- Rhetoric = (
- 0.2, 0.1, 0.1, 0.0,
- 0.1, 0.4, 0.1, 0.0,
- 0.1, 0.1, 0.3, 0.0
- )
- Logic = (
- 0.3, 0.1, 0.1, 0.0,
- 0.1, 0.1, 0.1, 0.0,
- 0.1, 0.1, 0.4, 0.0
- )
- Drama = (
- 0.4, 0.1, 0.1, 0.0,
- 0.1, 0.3, 0.1, 0.0,
- 0.1, 0.1, 0.1, 0.0
- )
- Paranoia = (
- 0.1, 0.1, 0.1, 0.0,
- 0.1, 0.3, 0.1, 0.0,
- 0.1, 0.1, 0.4, 0.0
- )
- # Rotates a given block.
- def rot(PIL_im, n, x1, y1):
- temple = []
- for i in range(n):
- temple.append([])
- for j in range(n):
- temple[i].append(PIL_im.getpixel((x1+i, y1+j)))
- for i in range(n):
- for j in range(n):
- PIL_im.putpixel((x1+i, y1+j), temple[n-1-i][n-1-j])
- def schiziphy(PIL_im):
- xres, yres = PIL_im.size
- BLKSZ = 80 # blocksize
- """
- for i in range(2, BLKSZ + 1):
- for j in range(int(math.floor(float(xres) / float(i)))):
- for k in range(int(math.floor(float(yres) / float(i)))):
- rot(PIL_im, i, j * i, k * i)
- for i in range(3, BLKSZ + 1):
- for j in range(int(math.floor(float(xres) / float(BLKSZ+2-i)))):
- for k in range(int(math.floor(float(yres) / float(BLKSZ+2-i)))):
- rot(PIL_im, BLKSZ+2-i, j*(BLKSZ+2-i), k*(BLKSZ+2-i))
- """
- for i in range(10):
- x = random.randint(xres * 0.25, xres * 0.75)
- y = random.randint(yres * 0.25, yres * 0.75)
- rot(PIL_im, BLKSZ, x, y)
- def PIL_2_cv(PIL_im):
- cv_im = array(PIL_im)
- cv_im = cv_im[:, :, ::-1].copy()
- return cv_im
- def cv_2_PIL(cv_im):
- PIL_im = cvtColor(cv_im, COLOR_BGR2RGB)
- PIL_im = Image.fromarray(PIL_im)
- return PIL_im
- # Open image.
- im = Image.open(INPUT_PIC).convert("RGB")
- background = Image.open(random.choice(BACKGROUND_PICS)).convert("RGB")
- cv_im = PIL_2_cv(im)
- cv_im = stylization(cv_im, sigma_s=60, sigma_r=0.07)
- dst_gray, cv_im = pencilSketch(cv_im, sigma_s=60, sigma_r=0.07, shade_factor=0.05)
- im = cv_2_PIL(cv_im)
- cv_im = PIL_2_cv(background)
- cv_im = stylization(cv_im, sigma_s=60, sigma_r=0.07)
- dst_gray, cv_im = pencilSketch(cv_im, sigma_s=60, sigma_r=0.07, shade_factor=0.05)
- background = cv_2_PIL(cv_im)
- """
- width, height = im.size
- random_pixs = int((width * height) * 0.3)
- for i in range(random_pixs):
- x = random.randint(0, width - 1)
- y = random.randint(0, height - 1)
- r = random.randrange(0,255)
- g = random.randrange(0,255)
- b = random.randrange(0,255)
- pixel[x, y] = (r, g, b)
- im = im.filter(ImageFilter.GaussianBlur(3))
- """
- # Apply transform and save.
- im = im.convert("RGB", Matrix)
- background = background.convert("RGB", Matrix)
- im = im.resize((512, 512), Image.ANTIALIAS)
- background = background.resize((512, 512), Image.ANTIALIAS)
- im = im.convert("RGBA")
- baackground = background.convert("RGBA")
- pixel = im.load()
- r, g, b, a = pixel[0, 0]
- data = array(im)
- data[(data == (r, g, b, a)).all(axis = -1)] = (255, 255, 255, 0)
- im = Image.fromarray(data, mode='RGBA')
- schiziphy(im)
- background.paste(im, (0, 0), im)
- background.save('result.png', "PNG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement