-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Module de manipulation d'images bien avancé.
WARNING, je vais modifier le résultat du chargement des pixels des images. Si je fais n'importe quoi, revenir à ce commit
- Loading branch information
1 parent
647dbf7
commit cc468b2
Showing
7 changed files
with
218 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# PyCharm config files | ||
.idea/* | ||
.idea/ | ||
|
||
|
||
# Byte-compiled / optimized / DLL files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
""" Ce module contient des fonctions et methodes permettant de convertir le contenu d'une image dans un certain codage | ||
vers un autre. | ||
""" | ||
|
||
from enum import IntEnum | ||
|
||
import numpy as np | ||
|
||
|
||
class Codage(IntEnum): | ||
NOIR_ET_BLANC = 0 | ||
RGB = 1 | ||
HSV = 2 | ||
|
||
|
||
class UnknownCodage(Exception): | ||
def __init__(self, codage): | ||
Exception.__init__("Le codage {} n'est actuellement pas supporté ou néexiste pas".format(codage)) | ||
|
||
|
||
def change_codage(pixels: np.ndarray, codage_src: Codage, codage_dest: Codage) -> np.ndarray: | ||
""" Change le codage des pixels puis les retourne. | ||
:param: pixels, les pixels dont on veut convertir le codage. | ||
:param: codage_src, le codage dans lequel les pixels sont actuellement encodés. | ||
:param: codage_dest, le codage dans lequel on veut convertir nos pixels. | ||
:return: Les pixels encodés dans e codage `codage_dest`. | ||
""" | ||
if codage_src == Codage.NOIR_ET_BLANC: | ||
if codage_dest == Codage.NOIR_ET_BLANC: | ||
return pixels | ||
elif codage_dest == Codage.RGB: | ||
return _black_and_white_to_rgb(pixels) | ||
elif codage_dest == Codage.HSV: | ||
return _black_and_white_to_hsv(pixels) | ||
elif codage_src == Codage.RGB: | ||
if codage_dest == Codage.NOIR_ET_BLANC: | ||
return _rgb_to_black_and_white(pixels) | ||
elif codage_dest == Codage.RGB: | ||
return pixels | ||
elif codage_dest == Codage.HSV: | ||
return _rgb_to_hsv(pixels) | ||
elif codage_src == Codage.HSV: | ||
if codage_dest == Codage.NOIR_ET_BLANC: | ||
return _hsv_to_black_and_white(pixels) | ||
elif codage_dest == Codage.RGB: | ||
return _hsv_to_rgb(pixels) | ||
elif codage_dest == Codage.HSV: | ||
return pixels | ||
else: | ||
raise UnknownCodage(codage_dest) | ||
|
||
|
||
def _black_and_white_to_rgb(pixels: np.ndarray) -> np.ndarray: | ||
pass | ||
|
||
|
||
def _black_and_white_to_hsv(pixels: np.ndarray) -> np.ndarray: | ||
pass | ||
|
||
|
||
def _rgb_to_hsv(pixels: np.ndarray) -> np.ndarray: | ||
pass | ||
|
||
|
||
def _rgb_to_black_and_white(pixels: np.ndarray) -> np.ndarray: | ||
pass | ||
|
||
|
||
def _hsv_to_rgb(pixels: np.ndarray) -> np.ndarray: | ||
pass | ||
|
||
|
||
def _hsv_to_black_and_white(pixels: np.ndarray) -> np.ndarray: | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
""" Ce module contient des examples d'images courament utilisés dans le traitement d'image. | ||
""" | ||
from os.path import join, dirname | ||
|
||
|
||
_PATH_DIR_PICTURES = join(dirname(__file__), "../../res/pictures") | ||
|
||
CAMERAMAN = join(_PATH_DIR_PICTURES, "cameraman.tif") | ||
HOUSE = join(_PATH_DIR_PICTURES, "house.tif") | ||
JETPLANE = join(_PATH_DIR_PICTURES, "jetplane.tif") | ||
LAKE = join(_PATH_DIR_PICTURES, "lake.tif") | ||
LENA_COLOR_256 = join(_PATH_DIR_PICTURES, "lena_color_256.tif") | ||
LENA_COLOR_512 = join(_PATH_DIR_PICTURES, "lena_color_512.tif") | ||
LENA_GRAY_256 = join(_PATH_DIR_PICTURES, "lena_gray_256.tif") | ||
LENA_GRAY_512 = join(_PATH_DIR_PICTURES, "lena_gray_512.tif") | ||
LIVINGROOM = join(_PATH_DIR_PICTURES, "livingroom.tif") | ||
MANDRIL_COLOR = join(_PATH_DIR_PICTURES, "mandril_color.tif") | ||
MANDRIL_GRAY = join(_PATH_DIR_PICTURES, "mandril_gray.tif") | ||
PEPPERS_COLOR = join(_PATH_DIR_PICTURES, "peppers_color.tif") | ||
PEPPERS_GRAY = join(_PATH_DIR_PICTURES, "peppers_gray.tif") | ||
PIRATE = join(_PATH_DIR_PICTURES, "pirate.tif") | ||
WALKBRIDGE = join(_PATH_DIR_PICTURES, "walkbridg.tif") | ||
WOMAN_BLONDE = join(_PATH_DIR_PICTURES, "woman_blonde.tif") | ||
WOMAN_DARKHAIR = join(_PATH_DIR_PICTURES, "woman_darkhair.tif") | ||
|
||
|
||
if __name__ == "__main__": | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# -*- coding: utf-8 -*- | ||
""" Ce module contient toutes les méthodes et fonctions nécessaires pour | ||
pouvoir manipuler facilement des images. | ||
""" | ||
from typing import Tuple | ||
import os.path | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
from src.picture_tools.codage import Codage, change_codage | ||
|
||
|
||
VALUE_MISSING_PIXEL = -100 | ||
|
||
|
||
class Picture: | ||
""" Permet de manipuler très facilement une image tout en supportant plusieurs encodages. | ||
Attributs : | ||
- picture_path : str, le chemin vers l'image. | ||
- codage : Codage, le codage des pixels de l'image (par défaut, HSV). | ||
- pixels : np.ndarray, les pixels (le contenu) de l'image. | ||
- hauteur : int, la hauteur de l'image. | ||
- largeur : int, la largeur de l'image. | ||
""" | ||
def __init__(self, picture_path: str, codage: Codage = Codage.HSV): | ||
self.picture_path = picture_path | ||
self.codage = codage | ||
pixels, self.hauteur, self.largeur = _load_pixels(picture_path) | ||
self.pixels = change_codage(pixels, Codage.RGB, self.codage) | ||
|
||
def show(self, show=True) -> None: | ||
""" Plot l'image sur matplotlib et l'affiche sur demande. | ||
:param: show, waut `True` si on affiche l'image après l'avoir plottée. | ||
""" | ||
picture = _pixels_to_picture(self.pixels, self.hauteur, self.largeur) | ||
plt.imshow(picture) | ||
if show: | ||
plt.show() | ||
|
||
def save(self, picture_path: str = None) -> None: | ||
""" Sauvegarde l'image sur le disque. | ||
Si aucun nom n'est passé en paramètre, sauvegarde l'image dans le repertoire courant avec le basename utilisé à | ||
l'ouverture. | ||
:param: picture_path, le chemin de destination. | ||
""" | ||
if picture_path is None: | ||
picture_path = os.path.basename(self.picture_path) | ||
image = _pixels_to_picture(self.pixels, self.hauteur, self.largeur) | ||
plt.imshow(image) | ||
plt.savefig(picture_path) | ||
|
||
|
||
def _load_pixels(picture_path: str) -> Tuple[np.ndarray, int, int]: | ||
""" Charge les pixels d'une image depuis le disque et retourne son contenu. | ||
Si l'image est en couleur, retourne ses pixels au format RGB en ignorant le canal alpha. | ||
Si l'image est en noir et blanc, retourne ses pixels de la même intensité dans chaque couleur (elle sera donc | ||
toujours en noir et blanc, mais au format RGB). | ||
:param: picture_path, Le chemin contenant l'image à charger. | ||
:return: Un tuple contenant les pixels de l'image, sa hauteur et sa largeur. | ||
""" | ||
picture = plt.imread(picture_path) | ||
|
||
if len(picture.shape) == 3: | ||
# L'image comporte trois composantes : RGBK | ||
# Elle est donc en couleurs.K | ||
# Elle peut contenir une composante Alpha, mais elle sera ignorée. | ||
picture = picture[:, :, :3] | ||
else: | ||
# L'image comporte une seule composante : l'intensité des pixels. | ||
# Elle est donc en noir et blanc. On la converti au format RGB. | ||
picture = np.dstack([picture] * 3) | ||
|
||
# Transformation en matrice `n * 3`, avec `n` le nombre de pixels | ||
image_hauteur, image_largeur, _ = picture.shape | ||
pixels = _picture_to_pixels(picture, image_hauteur, image_largeur) | ||
|
||
return pixels, image_hauteur, image_largeur | ||
|
||
|
||
def _picture_to_pixels(picture: np.ndarray, image_hauteur: int, image_largeur: int) -> np.ndarray: | ||
""" Récupère les pixels d'une image dans un format plus facilement utilisable. | ||
:param: image, l'image chargée par matplotlib. | ||
:param: image_hauteur, la hauteur de l'image. | ||
:param: image_largeur, la largeur de l'image. | ||
""" | ||
return picture.reshape((image_hauteur * image_largeur, 3)) | ||
|
||
|
||
def _pixels_to_picture(pixels: np.ndarray, image_hauteur: int, image_largeur: int) -> np.ndarray: | ||
""" Récupère les pixels d'une image dans un format plus facilement utilisable. | ||
:param: pixels, Les pixels de l'image chargée par matplotlib. | ||
:param: image_hauteur, la hauteur de l'image. | ||
:param: image_largeur, la largeur de l'image. | ||
""" | ||
return pixels.reshape((image_hauteur, image_largeur , 3)) | ||
|
||
|
||
if __name__ == "__main__": | ||
pass |