Last active
June 3, 2019 12:01
-
-
Save gfhuertac/868ad0fcf173ca2595af943472635aa5 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
# -*- coding: utf-8 -*- | |
# paso 0. Importar módulos | |
import requests | |
import matplotlib.pyplot as plt | |
import numpy as np | |
# ejercicio 1. Convertir las instrucciones a una función llamada muestra_imagen | |
def guarda_imagen(imagen, indice): | |
plt.imsave('imagen_' + str(indice) + '.png', imagen) | |
# paso 1. Bajar imagen desde un sitio web | |
url = 'https://www.uai.cl/assets/uploads/2018/09/logo-uai.png' | |
r = requests.get(url, stream=True) | |
if r.status_code == 200: | |
# paso 1.1. si la imagen está comprimida, descomprimirla | |
r.raw.decode_content = True | |
# paso 2. leo la imagen como una matriz numpy | |
plt_img = plt.imread(r.raw) | |
plt_img = (plt_img*255).astype('uint8') | |
guarda_imagen(plt_img, 0) | |
# cambiar a muestra_imagen(plt_img) | |
# paso 4. recorto lo imagen y la muestro | |
cropped_img = plt_img[80:556,:,:] | |
guarda_imagen(cropped_img, 1) | |
# paso 5. juguemos un poco. "Ocultemos" un color en una parte de la imagen | |
largo = cropped_img.shape[1] | |
copy_img = np.copy(cropped_img) | |
# paso 5.1. en el primer tercio, dejamos de ocupar el rojo | |
copy_img[:,:largo//3,0] = 0 | |
# paso 5.2. en el segundo tercio, dejamos de ocupar el verde | |
copy_img[:,largo//3:2*largo//3,1] = 0 | |
# paso 5.3. en el último tercio, dejamos de ocupar el azul | |
copy_img[:,2*largo//3:,2] = 0 | |
guarda_imagen(copy_img,2) | |
# paso 6. ahora invertimos los colores | |
copy_img = np.copy(cropped_img) | |
for i in range(copy_img.shape[0]): | |
for j in range(copy_img.shape[1]): | |
if copy_img[i,j,0]>=250 and copy_img[i,j,1]>=250 and copy_img[i,j,2]>=250: | |
copy_img[i,j,0] = 0 | |
copy_img[i,j,1] = 0 | |
copy_img[i,j,2] = 0 | |
else: | |
copy_img[i,j,0] = 255 | |
copy_img[i,j,1] = 255 | |
copy_img[i,j,2] = 255 | |
guarda_imagen(copy_img,3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment