Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- import matplotlib.pyplot as plt
- from IPython.display import display
- import os
- import random
- import numpy as np
- import numpy.random as rand
- import matplotlib.image as mpimg
- from PIL import ImageOps
- import math
- class pictures:
- def __init__(self, dir = 'task4'):
- self.dir = dir
- self.allpictures = []
- i = 1
- while True:
- try:
- self.allpictures.append(Image.open('{}/{}.jpg'.format(dir, i)))
- except FileNotFoundError:
- break
- i = i + 1
- def resize(self, size):
- self.resizepictures = []
- for i in self.allpictures:
- f = i.resize((size[0], size[1]))
- self.resizepictures.append(f)
- def whiteimage(self, image1, image2):
- ar1 = np.asarray(image1)
- ar2 = np.asarray(image2)
- self.templateimage = Image.new('RGB',(ar1.shape[0]*ar2.shape[0], ar1.shape[1]*ar2.shape[1]))
- def mean_color(self, picture):
- ar = np.asarray(picture)
- res = [0, 0, 0]
- res[0] = np.sum(ar[:, :, 0]) / len(ar) / len(ar[0])
- res[1] = np.sum(ar[:, :, 1]) / len(ar) / len(ar[0])
- res[2] = np.sum(ar[:, :, 2]) / len(ar) / len(ar[0])
- return res
- """self.res = np.sum(np.sum(ar, axis = 1), axis = 0)//(ar.shape[0] * ar.shape[1])
- return self.res"""
- def dist(self, color1, color2):
- """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)
- print(self.d)"""
- self.d = 0
- for i in range(len(color1)):
- res = (color1[i] - color2[i])**2
- self.d = self.d + res
- return round(self.d)
- def get_pixelart(self, image):
- ar = np.asarray(image)
- min_dist = float(1000)
- for i in range(ar.shape[0]):
- for j in range(ar.shape[1]):
- for h in self.resizepictures:
- print(ar[i, j], self.mean_color(h))
- new_min_dist = self.dist(ar[i, j], self.mean_color(h))
- if new_min_dist < min_dist:
- min_dist = new_min_dist
- min_dist = float(1000)
Add Comment
Please, Sign In to add comment