Éric Wegrzynowski 2f5a2cc61d
2021-01-08 18:23:03 +01:00

#### 85 lines 2.4 KiB Python Raw Blame History

 ```#!/usr/bin/python3 ``` ```# -*- coding: utf-8 -*- ``` ``` ``` ```""" ``` ```:mod:`sudoku_grid` module ``` ```:author: `Éric W` ``` ```:date: 2020, december ``` ``` ``` ```Représentation de grilles de Sudoku sous la forme d'une image ``` ```« à la Mondrian » ``` ```""" ``` ``` ``` ```from PIL import Image ``` ``` ``` ```# Quelques constantes ``` ```# Epaisseur gros traits ``` ```EGT = 12 ``` ```# Epaisseur petits traits ``` ```EPT = 4 ``` ```# taille d'une case ``` ```LCASE = 40 ``` ```# dimension image ``` ```LGRILLE = 4*EGT + 6*EPT + 9*LCASE ``` ``` ``` ```# Couleur traits ``` ```COUL_TRAITS = (0, 0, 0) ``` ```# Couleurs des cases ``` ```PALETTE = ((255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), ``` ``` (255, 0, 255), (0, 255, 255), (255, 127, 127), (127, 255, 127), (127, 127, 255)) ``` ``` ``` ```def colorie_case(img, coord, coul): ``` ``` ''' ``` ``` :param img: (Image) une image (une grille de sudoku) ``` ``` :param coord: (tuple) couple de coordonnées d'une case ``` ``` :param coul: (int) un entier compris entre 0 et 9 indice de la couleur dans la palette ``` ``` :return: (NoneType) ``` ``` :effet de bord: colorie la case de coordonnées coord de la grille img ``` ``` avec la couleur coul ``` ``` ''' ``` ``` i, j = coord ``` ``` f = lambda k : EGT + k//3 * (EGT + 2*EPT) + k%3 * EPT + k * LCASE ``` ``` offsetx = f(i) ``` ``` offsety = f(j) ``` ``` for dx in range(LCASE): ``` ``` for dy in range(LCASE): ``` ``` img.putpixel((offsetx + dx, offsety + dy), PALETTE[coul]) ``` ``` ``` ```def colorie_sudoku(grid): ``` ``` ''' ``` ``` :param grid: (SudokuGrid) la grille de Sudoku à représenter ``` ``` :return: (Image) une image colorée représentant cette grille ``` ``` ''' ``` ``` img = Image.new('RGB', (LGRILLE, LGRILLE)) ``` ``` # Les gros traits ``` ``` # traits horizontaux ``` ``` for i in range(4): ``` ``` for k in range(EGT): ``` ``` for x in range(LGRILLE): ``` ``` img.putpixel((x, i*(EGT + 2*EPT + 3*LCASE) + k), COUL_TRAITS) ``` ``` # traits verticaux ``` ``` for j in range(4): ``` ``` for k in range(EGT): ``` ``` for y in range(LGRILLE): ``` ``` img.putpixel((j*(EGT + 2*EPT + 3*LCASE) + k, y), COUL_TRAITS) ``` ``` # Les petits traits ``` ``` # horizontaux ``` ``` for i in range(6): ``` ``` for k in range(EPT): ``` ``` for x in range(LGRILLE): ``` ``` img.putpixel((x, (i//2 + 1)*EGT + i*EPT + (i + 1 + i//2)*LCASE), COUL_TRAITS) ``` ``` # verticaux ``` ``` for j in range(6): ``` ``` for k in range(EPT): ``` ``` for y in range(LGRILLE): ``` ``` img.putpixel(((j//2 + 1)*EGT + j*EPT + (j + 1 + j//2)*LCASE, y), COUL_TRAITS) ``` ``` # coloriage des cases ``` ``` for y in range(9): ``` ``` for x in range(9): ``` ``` colorie_case(img, (x, y), int(grid[x, y])) ``` ``` return img ``` ``` ``` ``` ``` ``` ``` ``` ```