Carson1

Untitled

Apr 28th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.28 KB | None | 0 0
  1. from PIL import Image, ImageOps
  2. import matplotlib.pyplot as plt
  3. from IPython.display import display
  4. import os
  5. import random
  6. import numpy as np
  7. import numpy.random as rand
  8. import matplotlib.image as mpimg
  9. import math
  10. class pictures:
  11.   def __init__(self, dir = 'van_gog'):
  12.     self.dir = dir
  13.     self.allpictures = []
  14.     i = 1
  15.     while True:
  16.         try:
  17.             self.allpictures.append(Image.open('{}/{}.jpg'.format(dir, i)))
  18.         except FileNotFoundError:
  19.             break
  20.         i = i + 1
  21.   def resize(self, size):
  22.     self.resizedpictures = []
  23.     for i in self.allpictures:
  24.         f = i.resize((size))
  25.         self.resizedpictures.append(f)
  26.   def whiteimage(self, image1, image2):
  27.     ar1 = np.asarray(image1)
  28.     ar2 = np.asarray(image2)
  29.     self.templateimage = Image.new('RGB',(ar1.shape[0] * ar2.shape[0], ar1.shape[1] * ar2.shape[1]))
  30.   def mean_color(self, picture):
  31.     ar = np.asarray(picture)
  32.     res = [0, 0, 0]
  33.     res[0] = np.sum(ar[0]) / len(ar) / len(ar[0])
  34.     res[1] = np.sum(ar[1]) / len(ar) / len(ar[0])
  35.     res[2] = np.sum(ar[2]) / len(ar) / len(ar[0])
  36.     return res
  37.     """self.res = np.sum(np.sum(ar, axis = 1), axis = 0)//(ar.shape[0] * ar.shape[1])
  38.    return self.res"""
  39.   def dist(self, color1, color2):
  40.     """self.d = math.sqrt((int(color1[0]) - int(color2[0]))**2 + (int(color1[1]) - int(color2[1]))**2 + (int(color1[2]) - int(color2[2]))**2)
  41.    print(self.d)"""
  42.     d = 0
  43.     for i in range(len(color1)):
  44.         res = (color1[i] - color2[i])**2
  45.         d = d + res
  46.     return d
  47.   def get_pixelart(self, image):
  48.     ar = np.asarray(image)
  49.     for i in range(ar.shape[0]):
  50.         for j in range(ar.shape[1]):
  51.             min_dist = float(100000)
  52.             for k in self.resizedpictures:
  53.                 new_min_dist = self.dist(ar[i, j], self.mean_color(k))
  54.                 if new_min_dist < min_dist:
  55.                     min_dist = new_min_dist
  56.                     f = np.asarray(k)
  57.                     self.templateimage.paste(k,
  58.                                              (f.shape[1]*i - 4,
  59.                                               f.shape[0]*j - 4,
  60.                                               f.shape[1]*i,
  61.                                               f.shape[0]*j))        
  62.     display(self.templateimage)
Advertisement
Add Comment
Please, Sign In to add comment