#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Nov 20 11:52:07 2018 @author: andrea """ import image class Color: def __init__(self, r, g, b): self.set_color(r, g, b) def __mul__(self, f): return Color(self.r*f, self.g*f, self.b*f) def __repr__(self): return "Color({}, {}, {})".format(self.r, self.g, self.b) def bound(self): ''' riporto i valori in interi e nell'intervallo 0-255''' self.r = max(0, min( 255, round(self.r))) self.g = max(0, min( 255, round(self.g))) self.b = max(0, min( 255, round(self.b))) def __add__(self, other): return Color(self.r+other.r, self.g+other.g, self.b+other.b) def to_tuple(self): return self.r, self.g, self.b def set_color(self, r, g, b): #if not type(r) == int and 0 <= r <= 255: # raise ValueError("hai passato un canale rosso errato") #if not type(g) == int and 0 <= g <= 255: # raise ValueError("hai passato un canale verde errato") #if not type(b) == int and 0 <= b <= 255: # raise ValueError("hai passato un canale blu errato") self.r = r self.g = g self.b = b def luminosita(self): return (self.r+self.g+self.b) / (3*255) class Immagine: def __init__(self, w, h, colore=Color(0,0,0)): self._pixels = [ [ colore for _ in range(w) ] for _ in range(h) ] def width(self): return len(self._pixels[0]) def height(self): return len(self._pixels) def to_img(self): return [ [ c.to_tuple() for c in riga ] for riga in self._pixels ] def load(self, filename): img = image.load(filename) self._pixels = [ [ Color(r, g, b) for r, g, b in riga ] for riga in img ] def get_pixel(self, x, y): # qui ci andrebbero i controlli su x e y interi if 0 <= x < self.width() and 0 <= y < self.height(): return self._pixels[y][x] else: return Color(0,0,0) def set_pixel(self, x, y, colore): # qui ci andrebbero i controlli su x e y interi if 0 <= x < self.width() and 0 <= y < self.height(): self._pixels[y][x] = colore def save(self, filename): img = self.to_img() image.save(filename, img) def gradienteH(self, c0, c1): # qui ci vanno i controlli che c0 e c1 siano delle istanze di Colore if not type(c0) == Color: raise ValueError("c0 non è un Color") if not type(c1) == Color: raise ValueError("c1 non è un Color") for x in range(self.width()): f = x/self.width() assert 0 <= f <= 1 colore = c0*(1-f)+c1*f colore.bound() for y in range(self.height()): self.set_pixel(x, y, colore) def gradienteV(self, c0, c1): # qui ci vanno i controlli che c0 e c1 siano delle istanze di Colore if not type(c0) == Color: raise ValueError("c0 non è un Color") if not type(c1) == Color: raise ValueError("c1 non è un Color") for y in range(self.height()): f = y/self.height() assert 0 <= f <= 1 colore = c0*(1-f)+c1*f colore.bound() for x in range(self.width()): self.set_pixel(x, y, colore) def filtra(self, filtro, *altri_parametri): # qui ci va un controllo che filtro sia una funzione filtrata = Immagine(self.width(), self.height()) for x in range(self.width()): for y in range(self.height()): colore = filtro(self.get_pixel(x, y), *altri_parametri ) filtrata.set_pixel(x, y, colore) return filtrata def distorci(self, filtro, *altri_parametri): # qui ci va un controllo che filtro sia una funzione filtrata = Immagine(self.width(), self.height()) for x in range(self.width()): for y in range(self.height()): colore = filtro(self.get_pixel(x, y), x, y, self, *altri_parametri ) filtrata.set_pixel(x, y, colore) return filtrata def _repr_png_(self): return image.Immagine(self.to_img())._repr_png_()