Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://note.nkmk.me/en/python-pillow-imagedraw/
- # https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html
- from PIL import Image, ImageDraw, ImageFont
- import math
- def MatrizUniforme(filas : int, col: int, val = ""):
- mat = []
- for i in range(filas):
- mat.append([])
- for k in range(col):
- mat[i].append(val)
- return mat
- class EstiloMatriz:
- largo_casilla = 32
- alto_casilla = 32
- offset = (10, 10)
- bg_color = (255, 255, 255)
- fg_color = (0, 0, 0)
- fuente = ImageFont.truetype("FreeSerif.ttf", 20)
- grosor = 2
- def __init__(self, offset = (10, 10)):
- self.offset = offset
- def col_x(self, col : int) -> int:
- return self.offset[0] + col * self.largo_casilla
- def fila_y(self, fila : int) -> int:
- return self.offset[1] + fila * self.alto_casilla
- def casilla_offset(self, fila : int, col: int) -> tuple:
- return (self.col_x(col), self.fila_y(fila))
- def casilla_centro(self, fila : int, col: int) -> tuple:
- offset = self.casilla_offset(fila, col)
- return (offset[0] + self.largo_casilla / 2, offset[1] + self.alto_casilla / 2)
- draw : ImageDraw
- def DibujarMatriz(mat : list, estilo : EstiloMatriz):
- num_filas = len(mat)
- num_columnas = len(mat[0])
- largo_cuadricula = estilo.largo_casilla * num_columnas
- alto_cuadricula = estilo.alto_casilla * num_filas
- corner_start = estilo.offset
- corner_end = (corner_start[0] + largo_cuadricula, corner_start[1] + alto_cuadricula)
- def col_x(col : int) -> int:
- return estilo.col_x(col)
- def fila_y(fila : int) -> int:
- return estilo.fila_y(fila)
- def dibuja_fondo():
- draw.rectangle(xy=(corner_start, corner_end), fill=estilo.bg_color, outline=estilo.fg_color, width = estilo.grosor)
- def dibuja_filas():
- for i in range(num_filas):
- inicio : tuple
- fin : tuple
- inicio = (corner_start[0], fila_y(i))
- fin = (corner_end[0], fila_y(i))
- draw.line(xy=(inicio, fin), fill = estilo.fg_color, width = estilo.grosor)
- def dibuja_columnas():
- for i in range(num_columnas):
- inicio : tuple
- fin : tuple
- inicio = (col_x(i), corner_start[1])
- fin = (col_x(i), corner_end[1])
- draw.line(xy=(inicio, fin), fill = estilo.fg_color, width = estilo.grosor)
- def dibuja_cuadricula():
- dibuja_fondo()
- dibuja_filas()
- dibuja_columnas()
- def texto_en_casilla(fila : int, col : int)->str:
- return mat[fila][col]
- def dibuja_texto_casilla(fila : int, col : int):
- draw.text(xy = estilo.casilla_centro(fila, col), text = texto_en_casilla(fila, col), fill = estilo.fg_color, font = estilo.fuente, anchor = "mm")
- def dibuja_texto():
- for i in range(num_filas):
- for k in range(num_columnas):
- dibuja_texto_casilla(i, k)
- dibuja_cuadricula()
- dibuja_texto()
- class EstiloFlecha:
- ancho = 2
- grosor_punta = 10
- color = (0, 0, 255)
- def ComplejoATupla(z : complex):
- return (int(z.real), int(z.imag))
- def DibujarFlecha(inicio : tuple, fin : tuple, estilo : EstiloFlecha):
- draw.line(xy=(inicio, fin), fill = estilo.color, width = estilo.ancho)
- direccion = complex(fin[0] - inicio[0] + (fin[1] - inicio[1])*1j)
- direccion = direccion / abs(direccion)
- o = complex(fin[0] + fin[1] * 1j)
- ang = math.pi * (1.0 - 1.0/6.0)
- turn = math.cos(ang) + math.sin(ang) * 1j
- izq = o + direccion * turn * estilo.grosor_punta
- der = o + direccion * turn.conjugate() * estilo.grosor_punta
- draw.polygon(xy=(ComplejoATupla(o), ComplejoATupla(izq), ComplejoATupla(der)), fill = estilo.color, outline = estilo.color)
- def DibujarFrame(mat : list, flechas = [], filtro = lambda i, k : True,
- estilo_matriz = EstiloMatriz(offset = (10, 10))) -> list:
- mat_str = MatrizUniforme(len(mat), len(mat[0]))
- global draw
- for i in range(len(mat_str)):
- for k in range(len(mat_str[i])):
- if filtro(i, k):
- mat_str[i][k] = str(mat[i][k])
- imagen = Image.new('RGB', (250, 250), (128, 128, 128))
- draw = ImageDraw.Draw(imagen)
- DibujarMatriz(mat_str, estilo_matriz)
- for f in flechas:
- a = f[0]
- b = f[1]
- inicio_flecha = estilo_matriz.casilla_centro(fila = a[0], col = a[1])
- fin_flecha = estilo_matriz.casilla_centro(fila = b[0], col = b[1])
- DibujarFlecha(inicio_flecha, fin_flecha, EstiloFlecha())
- return imagen.convert("P", palette = Image.ADAPTIVE, colors = 256)
- def GuardarGif(movie : list):
- movie[0].save('movie.gif', save_all=True, append_images=movie[1:], optimize=True, duration=200, loop=0, include_color_table = True)
RAW Paste Data