Last active
September 5, 2017 00:28
-
-
Save Florencia-97/b699356328e42cdaf05f6202e720c698 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from PIL import ImageDraw | |
import os | |
import numpy as np | |
from math import sqrt | |
from colormath.color_objects import sRGBColor, LabColor | |
from colormath.color_conversions import convert_color | |
from colormath.color_diff import delta_e_cie2000 | |
IMAGEN_ORIGINAL = 'monalisa.jpg' | |
IMAGEN_FINAL = 'monalisa_pixelada.jpg' | |
def distance_btw_colors(rgb1,rgb2): | |
r1,g1,b1 = rgb1 | |
r2,g2,b2 = rgb2 | |
return sqrt((r2-r1)**2+(g2-g1)**2+(b2-b1)**2) | |
def borrar_trash(): | |
for file in os.listdir(): | |
if 'fila' in file or 'trash' in file: | |
os.remove(file) | |
def image_size(image): | |
image=Image.open(image) | |
w,h=image.size | |
return w,h | |
def convert(imagen_original, imagen_final): | |
"""Función principal""" | |
lista_fotos = fotos_a_lista(imagen_original) | |
anchura_final,altura_final = image_size(imagen_original) | |
cambiar_imagen(imagen_original,imagen_final,altura_final, anchura_final,lista_fotos) | |
def get_dominant_color(image_file): | |
image = Image.open(image_file) | |
try: | |
single_pixel = image.resize((1,1), Image.ANTIALIAS) | |
rgb = single_pixel.getpixel((0,0)) | |
if isinstance(rgb,tuple): | |
return rgb | |
except IOError: | |
return False | |
return False | |
def fotos_a_lista(image): | |
"""Lista todas las fotos q están donde se esta compilando todo""" | |
archivos= os.listdir() | |
new_list=[] | |
for file in archivos: | |
if 'jpg' in file.lower() and not image in file.lower(): | |
Image.open(file) | |
color = get_dominant_color(file) | |
if color!=False: | |
tupla = (color,file) | |
new_list.append(tupla) | |
return sorted(new_list) | |
def cambiar_imagen(picture,imagen_final,height,width,list_images): | |
""" recorre por pixel la imagen, da el color de este para así reemplazarla por otra""" | |
image = Image.open(picture) | |
image = image.resize((width,height)) | |
pixels = image.load() | |
list_final=[] | |
for y in range(height): | |
list_img=[] | |
for x in range (width): | |
r,g,b= pixels[x,y] | |
new_image = get_new_image((r,g,b) , list_images ) #ve q imagen pega más con el píxel | |
list_img.append(new_image) | |
name="fila_{}.jpg".format(y) | |
img_row = paste(list_img,1,name,50,50) # me tiene q devolves una hilera ya toda pegada | |
list_final.append(name) | |
new_name= "{}_{}ka.jpg".format(x,y) | |
new_image= paste(list_final,0,new_name,10000,170) | |
new_image.save(imagen_final) | |
borrar_trash() | |
def get_new_image(color,list_images): | |
"""Dado un color devuelve una imagen con esa tonalidad, color es un tupla q contiene las tres tonalidades""" | |
""" List_images es una lista con tuplas del tipo (nombre de imagen,luminosidad) acomodado de menos a más luminoso | |
#puedo optimizarla con una busqueda del estilo binaria """ | |
dist = distance_btw_colors(color,list_images[0][0]) | |
final_image=list_images[0] | |
for image in list_images: | |
new_dist= distance_btw_colors(color,image[0]) | |
if new_dist<dist: | |
dist=new_dist | |
final_image=image | |
return final_image[1] #devuelve el nombre | |
def paste(list_im,mode,name,width,high): #funciona bien | |
"""Pega las imagenes dadas en un lista, si el modo es 1 de manera horizontal, modo 0, vertical""" | |
imgs = [ Image.open(i) for i in list_im ] | |
for i in range (len(imgs)): | |
imgs[i]= imgs[i].resize((width,high)) | |
# pick the image which is the smallest, and resize the others to match it (can be arbitrary image shape here) | |
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1] | |
if mode == 1: | |
imgs_comb = np.hstack( (np.asarray( i.resize(min_shape))for i in imgs))#horizontal | |
else: | |
imgs_comb = np.vstack( (np.asarray( i.resize(min_shape))for i in imgs)) #vertical | |
# save that beautiful picture | |
imgs_comb = Image.fromarray( imgs_comb) | |
imgs_comb.save( name ) | |
return(imgs_comb) | |
convert(IMAGEN_ORIGINAL,IMAGEN_FINAL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment