Bien sûr !
Pour ajouter une interface graphique qui permet à l'utilisateur de choisir une photo et de
l'afficher avec l'effet cartoon, nous allons utiliser la bibliothèque Tkinter pour créer cette interface.
Voici le code modifié :
```python
import cv2
import numpy as np
from tkinter import Tk, filedialog, Button, Label
from tkinter import PhotoImage
from PIL import Image, ImageTk
class Cartoonizer:
"""Cartoonizer effect
A class that applies a cartoon effect to an image.
The class uses a bilateral filter and adaptive thresholding to create
a cartoon effect.
"""
def __init__(self):
pass
def render(self, img_rgb):
img_rgb = [Link](img_rgb)
img_rgb = [Link](img_rgb, (1366, 768))
numDownSamples = 2 # number of downscaling steps
numBilateralFilters = 50 # number of bilateral filtering steps
# -- STEP 1 --
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in range(numDownSamples):
img_color = [Link](img_color)
# repeatedly apply small bilateral filter instead of applying
# one large filter
for _ in range(numBilateralFilters):
img_color = [Link](img_color, 9, 9, 7)
# upsample image to original size
for _ in range(numDownSamples):
img_color = [Link](img_color)
# -- STEPS 2 and 3 --
# convert to grayscale and apply median blur
img_gray = [Link](img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = [Link](img_gray, 3)
# -- STEP 4 --
# detect and enhance edges
img_edge = [Link](img_blur, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 9, 2)
# -- STEP 5 --
# convert back to color so that it can be bit-ANDed with color image
(x, y, z) = img_color.shape
img_edge = [Link](img_edge, (y, x))
img_edge = [Link](img_edge, cv2.COLOR_GRAY2RGB)
return cv2.bitwise_and(img_color, img_edge)
def select_file():
file_path = [Link](filetypes=[("Image files", "*.jpg;*.jpeg;*.png")])
if file_path:
res = tmp_canvas.render(file_path)
[Link]("Cartoon_version.jpg", res)
display_image("Cartoon_version.jpg")
def display_image(image_path):
img = [Link](image_path)
img = [Link](img)
[Link](image=img)
[Link] = img
# Création de l'interface utilisateur
root = Tk()
[Link]("Cartoonizer")
Button(root, text="Choisir une photo", command=select_file).pack(pady=10)
panel = Label(root)
[Link](pady=20)
tmp_canvas = Cartoonizer()
[Link]()
```
### Fonctionnement du Code :
1. **Importations** : Importation des bibliothèques nécessaires, y compris Tkinter pour l'interface
graphique.
2. **Classe Cartoonizer** : La classe Cartoonizer applique l'effet cartoon à une image comme
précédemment.
3. **Fonction select_file** : Ouvre une boîte de dialogue pour permettre à l'utilisateur de sélectionner
un fichier image. Ensuite, elle applique l'effet cartoon et affiche l'image résultante.
4. **Fonction display_image** : Affiche l'image traitée dans l'interface utilisateur.
5. **Interface Utilisateur** : Utilisation de Tkinter pour créer une interface avec un bouton pour choisir
la photo et un label pour afficher l'image cartoon.
Cela devrait fournir une interface simple où l'utilisateur peut sélectionner une image et appliquer l'effet
cartoon en cliquant sur un bouton. Si vous avez besoin d'améliorations ou d'ajouts spécifiques, faites-le
moi savoir ! 😊[43dcd9a7-70db-4a1f-b0ae-981daa162054]([Link]
tree/8cab9aa1bd12e19f38ae8c9c11dfbfecf286c1b0/cartonism%[Link]?citationMarker=43dcd9a7-
70db-4a1f-b0ae-981daa162054
"1")[43dcd9a7-70db-4a1f-b0ae-981daa162054]([Link]
a2c7bcc4082bab3972511900d998958d87123c94/render_cartoon.py?citationMarker=43dcd9a7-70db-
4a1f-b0ae-981daa162054
"2")[43dcd9a7-70db-4a1f-b0ae-981daa162054]([Link]
379a971ba4b006e9cdc4238583f787e52791a340/Do_an_cuoi_ki%2Feffect_sketch.py?
citationMarker=43dcd9a7-70db-4a1f-b0ae-981daa162054 "3")[43dcd9a7-70db-4a1f-b0ae-
981daa162054]([Link]
305fe807a983c33e76fa5fd8044649fc4fdc0ed0/[Link]?citationMarker=43dcd9a7-70db-4a1f-b0ae-
981daa162054 "4")[43dcd9a7-70db-4a1f-b0ae-981daa162054]([Link]
openpibo-python/tree/17b1ea99154dc604a5fce9298f122f3a830ba835/openpibo%[Link]?
citationMarker=43dcd9a7-70db-4a1f-b0ae-981daa162054 "5")[43dcd9a7-70db-4a1f-b0ae-
981daa162054]([Link]
ba1efa13f48ee66dfbd3ce8ef9eafe3e5e9f8b6c/opencv-blueprints-old%2Ffilters%[Link]?
citationMarker=43dcd9a7-70db-4a1f-b0ae-981daa162054 "6")[43dcd9a7-70db-4a1f-b0ae-
981daa162054]([Link]
f8e45c0634ced4692fdc37de51f8b7b90d19c2c1/[Link]?citationMarker=43dcd9a7-70db-4a1f-
b0ae-981daa162054 "7")[43dcd9a7-70db-4a1f-b0ae-981daa162054]([Link]
sreenivasan/Cartooning/tree/9a55869bfe43b27950388f973506edf96031c9a2/[Link]?
citationMarker=43dcd9a7-70db-4a1f-b0ae-981daa162054 "8")[43dcd9a7-70db-4a1f-b0ae-
981daa162054]([Link]
582748f7591e8062e0ef0f4e8abbe2a2e5d45c57/image_filter.py?citationMarker=43dcd9a7-70db-4a1f-
b0ae-981daa162054 "9")