Carson1

Untitled

Apr 28th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. from PIL import Image
  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. from PIL import ImageOps
  10. import math
  11. class pictures:
  12.   def __init__(self, dir = 'task4'):
  13.     self.dir = dir
  14.     self.allpictures = []
  15.     i = 1
  16.     while True:
  17.         try:
  18.             self.allpictures.append(Image.open('{}/{}.jpg'.format(dir, i)))
  19.         except FileNotFoundError:
  20.             break
  21.         i = i + 1
  22.   def resize(self, size):
  23.     self.resizepictures = []
  24.     for i in self.allpictures:
  25.         f = i.resize((size[0], size[1]))
  26.         self.resizepictures.append(f)
  27.   def whiteimage(self, image1, image2):
  28.     ar1 = np.asarray(image1)
  29.     ar2 = np.asarray(image2)
  30.     self.templateimage = Image.new('RGB',(ar1.shape[0]*ar2.shape[0], ar1.shape[1]*ar2.shape[1]))
  31.   def mean_color(self, picture):
  32.     ar = np.asarray(picture)
  33.     res = [0, 0, 0]
  34.     res[0] = np.sum(ar[:, :, 0]) / len(ar) / len(ar[0])
  35.     res[1] = np.sum(ar[:, :, 1]) / len(ar) / len(ar[0])
  36.     res[2] = np.sum(ar[:, :, 2]) / len(ar) / len(ar[0])
  37.     return res
  38.     """self.res = np.sum(np.sum(ar, axis = 1), axis = 0)//(ar.shape[0] * ar.shape[1])
  39.    return self.res"""
  40.   def dist(self, color1, color2):
  41.     """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)
  42.    print(self.d)"""
  43.     self.d = 0
  44.     for i in range(len(color1)):
  45.         res = (color1[i] - color2[i])**2
  46.         self.d = self.d + res
  47.     return round(self.d)
  48.   def get_pixelart(self, image):
  49.     ar = np.asarray(image)
  50.     min_dist = float(1000)
  51.     for i in range(ar.shape[0]):
  52.         for j in range(ar.shape[1]):
  53.             for h in self.resizepictures:
  54.                 print(ar[i, j], self.mean_color(h))
  55.                 new_min_dist = self.dist(ar[i, j], self.mean_color(h))
  56.                 if new_min_dist < min_dist:
  57.                     min_dist = new_min_dist
  58.             min_dist = float(1000)
Add Comment
Please, Sign In to add comment