Advertisement
Natchlou

Untitled

May 15th, 2024
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.59 KB | Source Code | 0 0
  1. from PIL import Image, ImageDraw # type: ignore
  2. from math import *
  3.  
  4. class bcolors:
  5.     HEADER = '\033[95m'
  6.     OKBLUE = '\033[94m'
  7.     OKCYAN = '\033[96m'
  8.     OKGREEN = '\033[92m'
  9.     WARNING = '\033[93m'
  10.     FAIL = '\033[91m'
  11.     ENDC = '\033[0m'
  12.     BOLD = '\033[1m'
  13.     UNDERLINE = '\033[4m'
  14.  
  15.  
  16. def dist(x: int,y :int,xc :int,yc :int)-> float:
  17.     """La fonction dist permet de calculer la distance entre 2 pixels"""
  18.     return sqrt((x-xc)**2+(y-yc)**2)
  19.  
  20.  
  21.  
  22.  
  23. def putPixel(hauteur, largeur, color):
  24.     for x in range(largeur):
  25.         for y in range(hauteur):
  26.             R, V, B = image.getpixel( (x,y) )
  27.             image.putpixel((x,y),(R-color[0],V-color[1],B-color[2]))
  28.  
  29. def putLine(x,y,lenght,color):
  30.     if(x+lenght > largeur):
  31.         print(bcolors.FAIL + 'La ligne dépasse la longueur de l\'image totale' + bcolors.ENDC)
  32.         breakpoint
  33.     else:
  34.         print(x,y,lenght)
  35.         x= int(x)
  36.         for a in range(x):
  37.             image.putpixel((a,y),(color[0],color[1],color[2]))
  38.  
  39. def drawZone(startx, starty, endx, endy, type):
  40.     print(bcolors.HEADER + f'Création de la zone en cours ...' + bcolors.ENDC)
  41.     startx = int(startx)
  42.     starty = int(starty)
  43.     endx = int(endx)
  44.     endy = int(endy)
  45.     for a in range(startx, endx):
  46.         for b in range(starty, endy):
  47.             R,V,B = image.getpixel((a,b))
  48.             G=(R+V+B)//3
  49.             if type == 'B&W':
  50.                 image.putpixel((a,b),(G,G,G))
  51.             elif type == 'filter':
  52.                 image.putpixel((a,b),(G,14,G))
  53.             elif type == 'sombre':
  54.                 image.putpixel((a,b),(7*R//10,7*V//10,7*B//10))
  55.             elif type == 'clair':
  56.                 image.putpixel((a,b),(R+70,V+70,B+70))
  57.     print(bcolors.OKGREEN + f'Zone crée' + bcolors.ENDC)
  58.  
  59.  
  60. # ------------------------------------------------------------------------------- #
  61. #                                                                                 #
  62. #           CHOIX DE L'IMAGE ET RECUPERATION DE LA LARGEUR ET LONGUEUR            #
  63. #                                                                                 #
  64. # ------------------------------------------------------------------------------- #
  65.  
  66. image = Image.open("champdefleurs.jpg").convert('RGB')
  67.  
  68. draw = ImageDraw.Draw(image)
  69. largeur, hauteur=image.size
  70.  
  71.  
  72.  
  73. # ------------------------------------------------------------------------------- #
  74. #                                                                                 #
  75. #                   DESSIN DES DIFFERENTES ZONES DANS L'IMAGE                     #
  76. #                                                                                 #
  77. # ------------------------------------------------------------------------------- #
  78.  
  79. # H A U T
  80. draw.line([(0,hauteur//2),(largeur,hauteur//2)], fill=(0,0,0), width=5) # GAUCHE
  81. draw.line([(largeur//2,0),(largeur//2,hauteur//2)], fill=(0,0,0), width=5) # DROITE
  82. drawZone(0,0,largeur,hauteur//2,'filter') # 1/2
  83. drawZone(largeur//2,0,largeur,hauteur//2,'B&W') # 2/2
  84.  
  85. # B A S
  86. draw.line([(largeur//3,hauteur//2),(largeur//3,hauteur)], fill=(0,0,0), width=5)
  87. draw.line([(2*largeur//3,hauteur//2),(2*largeur//3,hauteur)], fill=(0,0,0), width=5)
  88.  
  89. drawZone(0,hauteur//2,largeur//3,hauteur,'sombre') # 1/3
  90. drawZone(largeur//3,hauteur//2,2*largeur//3,hauteur,'clair') # 2/3
  91. drawZone(2*largeur//3,hauteur//2,3*largeur//3,hauteur,'filter') # 3/3
  92.  
  93. # ------------------------------------------------------------------------------- #
  94. #                                                                                 #
  95. #                   DESSIN DES FORMES DANS LES DIFFERENTES ZONES                  #
  96. #                                                                                 #
  97. # ------------------------------------------------------------------------------- #
  98.  
  99. rayon1=min(largeur//9,hauteur//9)    #On choisit un rayon égal au minimum entre la largeur et la hauteur, divisé par 5
  100.  
  101. x1,y1=largeur/2,(3/4)*hauteur  # On définit les coordonnées du centre du cercle, ici égal au pixel au centre de l'image
  102.  
  103. # La double boucle "for" qui parcourt tous les pixels (x,y) de l'image :
  104. for x in range(largeur):
  105.     for y in range(hauteur):
  106.         R, V, B = image.getpixel( (x,y) )
  107.         G=(R+V+B)//3
  108.         if dist(x,y,x1,y1)<=rayon1 :  # si la distance entre le pixel(x,y) et le centre (x1,y1) est inférieure au rayon rayon1 alors :
  109.             image.putpixel((x,y),(255-R,255-V,255-B))  # on transforme le code RVB en son négatif
  110.  
  111. rayon2=1.5*rayon1
  112. for x in range(largeur):
  113.     for y in range(hauteur):
  114.         R, V, B = image.getpixel( (x,y) )
  115.         G=(R+V+B)//3
  116.         if dist(x,y,x1,y1)<=rayon2 and  dist(x,y,x1,y1)>rayon1:  # si la distance entre le pixel(x,y) et le centre (x1,y1) est inférieure au rayon rayon1 alors :
  117.             image.putpixel((x,y),(R,R,R))
  118. rayon3=1.1*rayon2
  119. for x in range(largeur):
  120.     for y in range(hauteur):
  121.         R, V, B = image.getpixel( (x,y) )
  122.         G=(R+V+B)//3
  123.         if dist(x,y,x1,y1)<=rayon3 and  dist(x,y,x1,y1)>rayon2:
  124.             image.putpixel((x,y),(100-R,100-V,100-B))
  125.  
  126.  
  127. # ------------------------------------------------------------------------------- #
  128. #                                                                                 #
  129. #                           ENREGISTREMENT DE L'IMAGE                             #
  130. #                                                                                 #
  131. # ------------------------------------------------------------------------------- #
  132.  
  133. print(bcolors.OKBLUE + 'Dessin fini' + bcolors.ENDC)
  134. image.save("champdefleursstesssst.jpg", "jpeg")
  135. image.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement