Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. import random
  2. import math
  3.  
  4. from PIL import Image, ImageFilter
  5. from PIL.ImageOps import invert
  6. from numpy import array
  7. from cv2 import pencilSketch, stylization, cvtColor, COLOR_BGR2RGB
  8.  
  9. BACKGROUND_PICS = ["smoke.jpg", "smoke2.jfif"]
  10. INPUT_PIC = "input.jpg"
  11.  
  12. # Make transform matrix, to multiply R by 1.5, leaving G and B unchanged
  13. Matrix = (
  14. 0.4, 0.1, 0.1, 0.0,
  15. 0.1, 0.1, 0.1, 0.0,
  16. 0.1, 0.1, 0.1, 0.0
  17. )
  18.  
  19. Rhetoric = (
  20. 0.2, 0.1, 0.1, 0.0,
  21. 0.1, 0.4, 0.1, 0.0,
  22. 0.1, 0.1, 0.3, 0.0
  23. )
  24.  
  25. Logic = (
  26. 0.3, 0.1, 0.1, 0.0,
  27. 0.1, 0.1, 0.1, 0.0,
  28. 0.1, 0.1, 0.4, 0.0
  29. )
  30.  
  31. Drama = (
  32. 0.4, 0.1, 0.1, 0.0,
  33. 0.1, 0.3, 0.1, 0.0,
  34. 0.1, 0.1, 0.1, 0.0
  35. )
  36.  
  37. Paranoia = (
  38. 0.1, 0.1, 0.1, 0.0,
  39. 0.1, 0.3, 0.1, 0.0,
  40. 0.1, 0.1, 0.4, 0.0
  41. )
  42.  
  43. # Rotates a given block.
  44. def rot(PIL_im, n, x1, y1):
  45. temple = []
  46. for i in range(n):
  47. temple.append([])
  48. for j in range(n):
  49. temple[i].append(PIL_im.getpixel((x1+i, y1+j)))
  50.  
  51. for i in range(n):
  52. for j in range(n):
  53. PIL_im.putpixel((x1+i, y1+j), temple[n-1-i][n-1-j])
  54.  
  55.  
  56. def schiziphy(PIL_im):
  57. xres, yres = PIL_im.size
  58. BLKSZ = 80 # blocksize
  59. """
  60. for i in range(2, BLKSZ + 1):
  61. for j in range(int(math.floor(float(xres) / float(i)))):
  62. for k in range(int(math.floor(float(yres) / float(i)))):
  63. rot(PIL_im, i, j * i, k * i)
  64.  
  65. for i in range(3, BLKSZ + 1):
  66. for j in range(int(math.floor(float(xres) / float(BLKSZ+2-i)))):
  67. for k in range(int(math.floor(float(yres) / float(BLKSZ+2-i)))):
  68. rot(PIL_im, BLKSZ+2-i, j*(BLKSZ+2-i), k*(BLKSZ+2-i))
  69. """
  70. for i in range(10):
  71. x = random.randint(xres * 0.25, xres * 0.75)
  72. y = random.randint(yres * 0.25, yres * 0.75)
  73. rot(PIL_im, BLKSZ, x, y)
  74.  
  75. def PIL_2_cv(PIL_im):
  76. cv_im = array(PIL_im)
  77. cv_im = cv_im[:, :, ::-1].copy()
  78. return cv_im
  79.  
  80.  
  81. def cv_2_PIL(cv_im):
  82. PIL_im = cvtColor(cv_im, COLOR_BGR2RGB)
  83. PIL_im = Image.fromarray(PIL_im)
  84. return PIL_im
  85.  
  86. # Open image.
  87. im = Image.open(INPUT_PIC).convert("RGB")
  88. background = Image.open(random.choice(BACKGROUND_PICS)).convert("RGB")
  89.  
  90. cv_im = PIL_2_cv(im)
  91.  
  92. cv_im = stylization(cv_im, sigma_s=60, sigma_r=0.07)
  93. dst_gray, cv_im = pencilSketch(cv_im, sigma_s=60, sigma_r=0.07, shade_factor=0.05)
  94.  
  95. im = cv_2_PIL(cv_im)
  96.  
  97. cv_im = PIL_2_cv(background)
  98.  
  99. cv_im = stylization(cv_im, sigma_s=60, sigma_r=0.07)
  100. dst_gray, cv_im = pencilSketch(cv_im, sigma_s=60, sigma_r=0.07, shade_factor=0.05)
  101.  
  102. background = cv_2_PIL(cv_im)
  103.  
  104. """
  105. width, height = im.size
  106.  
  107. random_pixs = int((width * height) * 0.3)
  108.  
  109. for i in range(random_pixs):
  110. x = random.randint(0, width - 1)
  111. y = random.randint(0, height - 1)
  112.  
  113. r = random.randrange(0,255)
  114. g = random.randrange(0,255)
  115. b = random.randrange(0,255)
  116.  
  117. pixel[x, y] = (r, g, b)
  118.  
  119. im = im.filter(ImageFilter.GaussianBlur(3))
  120. """
  121.  
  122. # Apply transform and save.
  123. im = im.convert("RGB", Matrix)
  124. background = background.convert("RGB", Matrix)
  125.  
  126. im = im.resize((512, 512), Image.ANTIALIAS)
  127. background = background.resize((512, 512), Image.ANTIALIAS)
  128.  
  129. im = im.convert("RGBA")
  130. baackground = background.convert("RGBA")
  131.  
  132. pixel = im.load()
  133. r, g, b, a = pixel[0, 0]
  134.  
  135. data = array(im)
  136. data[(data == (r, g, b, a)).all(axis = -1)] = (255, 255, 255, 0)
  137. im = Image.fromarray(data, mode='RGBA')
  138.  
  139. schiziphy(im)
  140.  
  141. background.paste(im, (0, 0), im)
  142.  
  143. background.save('result.png', "PNG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement