{"id":902,"date":"2024-10-17T03:35:00","date_gmt":"2024-10-17T01:35:00","guid":{"rendered":"http:\/\/labodemaths.fr\/WordPress3\/?p=902"},"modified":"2024-11-04T11:31:51","modified_gmt":"2024-11-04T10:31:51","slug":"nsi-tp5-traitement-d-images-et-tuples","status":"publish","type":"post","link":"https:\/\/labodemaths.fr\/WordPress3\/nsi-tp5-traitement-d-images-et-tuples\/","title":{"rendered":"NSI : 2024-2025, traitement d&rsquo;images et tuples"},"content":{"rendered":"\n<h4>1. Pillow : une biblioth\u00e8que de traitement d&rsquo;images en Python.<\/h4>\n\n\n\n<p>Nous allons utiliser pour ce TP certaines fonctions de la biblioth\u00e8que Pillow ou PIL.<br>Pour en savoir plus : <a href=\"https:\/\/he-arc.github.io\/livre-python\/pillow\/index.html\">https:\/\/he-arc.github.io\/livre-python\/pillow\/index.html<\/a><br><\/p>\n\n\n\n<h4>2. Mise en place du tp :<\/h4>\n\n\n\n<ul><li>Cr\u00e9er un dossier TP_images,<\/li><li>Ouvrir Thonny, et copier-coller le code ci-dessous dans l&rsquo;\u00e9diteur de programme,<\/li><li>enregistrer votre fichier avec le nom \u00ab\u00a0TP_images\u00a0\u00bb dans le dossier TP_images,<\/li><li>enregistrer dans le dossier les deux images de la Joconde ci-dessous.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/Mona_Lisa.jpg\">Mona_Lisa<\/a><a href=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/Mona_Lisa.jpg\" class=\"wp-block-file__button\" download>T\u00e9l\u00e9charger<\/a><\/div>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/Mona_Lisa-1.png\">Mona_Lisa-1<\/a><a href=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/Mona_Lisa-1.png\" class=\"wp-block-file__button\" download>T\u00e9l\u00e9charger<\/a><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>from PIL import Image # on importe la classe Image et ses m\u00e9thodes de la\n# biblioth\u00e8que Pillow alias PIL\n\nimage = Image.open(\"Mona_lisa.jpg\") # Chargement d'une image avec PIL\n# image est d\u00e9clar\u00e9e comme un objet de la classe Image de PIL\n\ndef taille(img):\n    return img.size # m\u00e9thode propre \u00e0 la classe Image\n\ndef format(img):\n    return img.format # m\u00e9thode propre \u00e0 la classe Image\n\ndef lire_pixel(img,x,y):\n    return img.getpixel((x,y)) # m\u00e9thode propre \u00e0 la classe Image\n\ndef changer_pixel(img,x,y,couleur):\n    image=img\n    image.putpixel((x,y),couleur) # m\u00e9thode propre \u00e0 la classe Image\n    return image\n\ndef afficher(img):\n    img.show() # m\u00e9thode propre \u00e0 la classe Image\n\n# premieres fonctions de traitement de l'image\n\ndef eclaircir(img,e):\n    image_retour=img.copy()\n    (l, h) = taille(image)\n    for y in range(h):\n        for x in range(l):\n            c = lire_pixel(image_retour,x,y)\n            nouvelle_c=(c&#91;0]+e,c&#91;1]+e,c&#91;2]+e)\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour<\/code><\/pre>\n\n\n\n<h4>3. D\u00e9couverte des premi\u00e8res fonctions.<\/h4>\n\n\n\n<p>a) Tester la commande taille(image).<br>Que retourne cette commande ? Quel type d&rsquo;objet python est retourn\u00e9 par cette commande ?<\/p>\n\n\n\n<p>b) Tester la commande format(image).<br>Que retourne cette commande ? Quel type d&rsquo;objet python est retourn\u00e9 par cette commande ?<\/p>\n\n\n\n<p>c) Tester la commande lire_pixel(image,100,100)<br>Que retourne cette commande ? <br>Quel type d&rsquo;objet python est retourn\u00e9 ? <br>A votre avis quel est le syst\u00e8me de couleur utilis\u00e9 pour d\u00e9crire le contenu de ce pixel ?<\/p>\n\n\n\n<p>d) On parle un peu de format d&rsquo;images :<br>En consid\u00e9rant que l&rsquo;image n&rsquo;est constitu\u00e9e que des pixels qu&rsquo;elle contient, chaque pixel ayant une couleur cod\u00e9e sur 3 octets, d\u00e9terminer la taille m\u00e9moire occup\u00e9e th\u00e9oriquement par l&rsquo;image de Mona Lisa.<br>Comparer avec les informations propos\u00e9es pour les 2 images que vous avez enregistr\u00e9es.<br>Que peut-on remarquer ?<\/p>\n\n\n\n<p>d) Tester la commande changer_pixel(image,100,100,(255,0,0)) suivie de lire_pixel(image,100,100). <\/p>\n\n\n\n<p>e) Tester la commande afficher(image)<\/p>\n\n\n\n<h4>4. Premi\u00e8re fonction de traitement d&rsquo;image.<\/h4>\n\n\n\n<p>Tester la fonction eclaircir() avec comme premier param\u00e8tre image, et comme second respectivement les 3 valeurs  10, puis 50 et enfin -50.<br>Etudier cette fonction pour la suite.<br>Interroger vous sur la ligne 27 : image_retour=img.copy().<br><\/p>\n\n\n\n<h4>5. De nouvelles fonctions de traitement de l&rsquo;image.<br><\/h4>\n\n\n\n<p>Un pixel est caract\u00e9ris\u00e9 par sa couleur sous la forme d&rsquo;un tuple de type (R,V,B).<br>1) En vous inspirant de la fonction eclaircir, \u00e9crire 3 fonctions canal_R, canal_V et canal_B qui afficheront et retourneront l&rsquo;image o\u00f9 pour chaque pixel, on n&rsquo;a conserv\u00e9 respectivement que le canal couleur (R,0,0), (0,V,0) et (0,0,B).<\/p>\n\n\n\n<p>2) Cr\u00e9er une fonction degrade_gris() qui affiche et retourne l&rsquo;image o\u00f9 chaque canal de couleur a \u00e9t\u00e9 remplac\u00e9 par la valeur (R+V+B)\/3<\/p>\n\n\n\n<p>3) Cr\u00e9er une fonction symetrie() qui affiche et retourne l&rsquo;image apr\u00e8s une sym\u00e9trie par rapport \u00e0 l&rsquo;axe vertical partageant l&rsquo;image en deux.<\/p>\n\n\n\n<p>4) Cr\u00e9er une fonction photo_maton() qui r\u00e9alise la transformation d\u00e9crite dans cet article :<br><a href=\"https:\/\/interstices.info\/mona-lisa-au-photomaton\/\">https:\/\/interstices.info\/mona-lisa-au-photomaton\/<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"600\" height=\"390\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/transfo-photomaton.gif\" alt=\"\" class=\"wp-image-906\"\/><\/figure>\n\n\n\n<h2>Proposition correction<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>from PIL import Image # on importe la classe Image et ses m\u00e9thodes de la\n# biblioth\u00e8que Pillow alias PIL\n\nimage = Image.open(\"Mona_lisa.jpg\") # Chargement d'une image avec PIL\n# image est d\u00e9clar\u00e9e comme un objet de la classe Image de PIL\n\ndef taille(img):\n    return img.size # m\u00e9thode propre \u00e0 la classe Image\n\ndef format(img):\n    return img.format # m\u00e9thode propre \u00e0 la classe Image\n\ndef lire_pixel(img,x,y):\n    return img.getpixel((x,y)) # m\u00e9thode propre \u00e0 la classe Image\n\ndef changer_pixel(img,x,y,couleur):\n    image=img\n    image.putpixel((x,y),couleur) # m\u00e9thode propre \u00e0 la classe Image\n    return image\n\ndef afficher(img):\n    img.show() # m\u00e9thode propre \u00e0 la classe Image\n\n# premieres fonctions de traitement de l'image\n\ndef eclaircir(img,e):\n    image_retour=img.copy()\n    (l, h) = taille(image)\n    for y in range(h):\n        for x in range(l):\n            c = lire_pixel(image_retour,x,y)\n            nouvelle_c=(c&#91;0]+e,c&#91;1]+e,c&#91;2]+e)\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour\n\ndef canal_R(img):\n    image_retour=img.copy()\n    (l, h) = taille(image)\n    for y in range(h):\n        for x in range(l):\n            c = lire_pixel(image_retour,x,y)\n            nouvelle_c=(c&#91;0],0,0)\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour\n\ndef canal_V(img):\n    image_retour=img.copy()\n    (l, h) = taille(image)\n    for y in range(h):\n        for x in range(l):\n            c = lire_pixel(image_retour,x,y)\n            nouvelle_c=(0,c&#91;1],0)\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour\n\ndef canal_B(img):\n    image_retour=img.copy()\n    (l, h) = taille(image)\n    for y in range(h):\n        for x in range(l):\n            c = lire_pixel(image_retour,x,y)\n            nouvelle_c=(0,0,c&#91;2])\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour\n\ndef convertir_noir_blanc(img):\n    image_retour=img.copy()\n    (l, h) = taille(image)\n    for y in range(h):\n        for x in range(l):\n            c = lire_pixel(image_retour,x,y)\n            moyenne=int((c&#91;0]+c&#91;1]+c&#91;2])\/3)\n            nouvelle_c=( moyenne , moyenne , moyenne )\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour\n\ndef symetrie1(image):\r\n    image_retour=image.copy()\r\n    (l, h) = taille(image)\r\n    for y in range(h):\r\n        for x in range(l\/\/2):\r\n            c = lire_pixel(image_retour,x,y)\r\n            image_retour=changer_pixel(image_retour,l-1-x,y,c)\r\n    afficher(image_retour)\r\n    return image_retour\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. Pillow : une biblioth\u00e8que de traitement d&rsquo;images en Python. Nous allons utiliser pour ce&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/labodemaths.fr\/WordPress3\/nsi-tp5-traitement-d-images-et-tuples\/\">Read the post<span class=\"screen-reader-text\">NSI : 2024-2025, traitement d&rsquo;images et tuples<\/span><\/a><\/div>\n","protected":false},"author":2,"featured_media":903,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[57],"tags":[],"_links":{"self":[{"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/posts\/902"}],"collection":[{"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/comments?post=902"}],"version-history":[{"count":16,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":2577,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/posts\/902\/revisions\/2577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/media\/903"}],"wp:attachment":[{"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/media?parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/categories?post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/tags?post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}