Guest User

Untitled

a guest
Jun 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. """
  2.    Modules de traitement d'images
  3. """
  4.  
  5. from images import Image
  6.  
  7. def change_rgb(image, change_function):
  8.     for x in range(width):
  9.         for y in range(height):
  10.             rgb = image.getPixel(x,y)
  11.             change_function(x, y, rgb[0], rgb[1], rgb[2])
  12.     return rgb
  13.  
  14. def binarisation(image):
  15.     """Conversion d'une image en noir et blanc."""
  16.     get_rgb(image)
  17.     if (rgb[0] + rgb[1] + rgb[2]) / 3 < 128:
  18.         image.setPixel(x, y,(0, 0, 0))
  19.     else:
  20.         image.setPixel(x, y,(255, 255, 255))
  21.     image.save("smokey_binarisation.gif")
  22.  
  23.  
  24.  
  25. def grayscale(image):
  26.     """Conversion d'une image en niveau de gris."""
  27.     get_rgb(image)
  28.     l = (rgb[0] + rgb[1] + rgb[2]) / 3
  29.     image.setPixel(x, y,(l, l, l))
  30.     image.save("smokey_grayscale.gif")
  31.  
  32.     #À compléter
  33.      
  34.  
  35. def negatif(image):
  36.     """Conversion d'une image en negatif."""
  37.     get_rgb(image)
  38.     image.setPixel(x, y,(255-rgb[0], 255-rgb[1], 255-rgb[2]))
  39.     image.save("smokey_negatif.gif")
  40.            
  41.  
  42.     #À compléter
  43.      
  44.  
  45. def sepia(image):
  46.     """Conversion dans une image sépia."""
  47.     grayscale(image)
  48.    
  49.     get_rgb(image)
  50.    
  51.     if rgb[0] < 63:
  52.         r = int(rgb[0] * 1.1);
  53.         b = int(rgb[2] * 0.9)
  54.     elif rgb[0] < 192:
  55.         r = int(rgb[0] * 1.15);
  56.         b = int(rgb[2] * 0.85)
  57.     else:
  58.         r = min(int(rgb[0] * 1.08), 255); # min() retourne le minimum entre deux valeurs
  59.         b = int(rgb[2] * 0.93)
  60.     image.setPixel(x, y,(r, rgb[1], b))
  61.     image.save("smokey_sepia.gif")
  62.    
  63.  
  64.     #À compléter
  65.      
  66.  
  67.  
  68. if __name__ == "__main__":
  69.     print ("Quelle opération désirez-vous faire?")
  70.     print ("1 - Binarisation par seuillage")
  71.     print ("2 - Niveau de gris")
  72.     print ("3 - Négatif")
  73.     print ("4 - Sépia\n")
  74.     while True:
  75.         try:
  76.             choix = int(input())
  77.         except ValueError:
  78.             print("Vous n'avez pas entré un nombre entre 1 et 4!")
  79.             continue
  80.         if choix>=1 and choix<=4: break
  81.        
  82.     while True:
  83.         try:
  84.             filename = input("Entrez le nom de votre fichier image: ")
  85.             image = Image(filename)
  86.             width = image.getWidth()
  87.             height = image.getHeight()
  88.         except Exception:
  89.             print("Le fichier", filename,"est introuvable")
  90.             continue
  91.         break
  92.  
  93.     if choix == 1:
  94.         binarisation(image)
  95.     if choix ==2:
  96.         grayscale(image)
  97.     if choix == 3:
  98.          #grayscale(image)#optionnel appeler pour un niveau de gris avant d'appeler
  99.                           #pour le négatif et remarquez la différence des 2 résultats
  100.          negatif(image)
  101.     if choix == 4:
  102.         sepia(image)
  103.     image.draw()
Add Comment
Please, Sign In to add comment