{"id":969,"date":"2019-11-19T20:35:22","date_gmt":"2019-11-19T19:35:22","guid":{"rendered":"http:\/\/labodemaths.fr\/WordPress3\/?p=969"},"modified":"2020-09-09T19:19:37","modified_gmt":"2020-09-09T17:19:37","slug":"nsi-fin-correction-tp-traitement-image","status":"publish","type":"post","link":"https:\/\/labodemaths.fr\/WordPress3\/nsi-fin-correction-tp-traitement-image\/","title":{"rendered":"NSI : fin correction tp traitement image"},"content":{"rendered":"\n<p>Le code complet :<\/p>\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()\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[0]+e,c[1]+e,c[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[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[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[2])\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour\n\ndef superposition(img):\n    img1=canal_R(img)\n    img2=canal_V(img)\n    img3=canal_B(img)\n    image_retour=Image.new('RGB',taille(img1) , color=0) # on cr\u00e9e une image vide\n    (l, h) = taille(image)\n    for y in range(h):\n        for x in range(l):\n            c1 = lire_pixel(img1,x,y)\n            c2 = lire_pixel(img2,x,y)\n            c3 = lire_pixel(img3,x,y)\n            nouvelle_c=(c1[0]+c2[0]+c3[0],c1[1]+c2[1]+c3[1],c1[2]+c2[2]+c3[2])\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour\n\ndef symetrie(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,x,y)\n            image_retour=changer_pixel(image_retour,l-1-x,y,c)\n    afficher(image_retour)\n    return image_retour\n\ndef symetrie_1(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            image_retour=changer_pixel(image_retour,l-1-x,y,c)\n    afficher(image_retour)\n    return image_retour\n\ndef symetrie_2(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,l-1-x,y)\n            image_retour=changer_pixel(image_retour,x,y,c)\n    afficher(image_retour)\n    return image_retour\n\ndef photomaton(img):\n    '''\n    ne fonctionne qu'avec une image de dimensions paires\n    '''\n    image_retour=img.copy()\n    (l, h) = taille(image)\n    l1=int(l\/2)\n    h1=int(h\/2)\n    for x in range(0,l-1,2):\n        for y in range(0,h-1,2):\n            pixel1 = lire_pixel(img,x,y)\n            pixel2 = lire_pixel(img,x+1,y)\n            pixel3 = lire_pixel(img,x,y+1)\n            pixel4 = lire_pixel(img,x+1,y+1)\n            x1=int(x\/2)\n            y1=int(y\/2)\n            image_retour=changer_pixel(image_retour,x1,y1,pixel1)\n            image_retour=changer_pixel(image_retour,x1+l1,y1,pixel2)\n            image_retour=changer_pixel(image_retour,x1,y1+h1,pixel3)\n            image_retour=changer_pixel(image_retour,x1+l1,y1+h1,pixel4)\n    afficher(image_retour)\n    return image_retour\n\ndef boucle_photomaton(img,n):\n    image_retour=img.copy()\n    for i in range(n):\n        image_retour=photomaton(image_retour)\n    return image_retour\n\ndef difference(img1,img2):\n    image_retour=Image.new('RGB',taille(img1) , color=0) # on cr\u00e9e une image vide\n    (l, h) = taille(image)\n    for y in range(h):\n        for x in range(l):\n            c1 = lire_pixel(img1,x,y)\n            c2 = lire_pixel(img2,x,y)\n            nouvelle_c=(c1[0]-c2[0],c1[1]-c2[1],c1[2]-c2[2])\n            if nouvelle_c!=(0,0,0):\n                return False\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_c)\n    afficher(image_retour)\n    return image_retour\n\n# calcul de valeur moyenne\n\ndef moyenne(img,x,y,c):\n    '''\n    calcule la moyenne des pixels du carr\u00e9 de c\u00f4t\u00e9 c\n    centr\u00e9 sur le pixel de coordonn\u00e9es (x,y)\n    return : tuple de type (R,V,B)\n    attention aux pixels sur le bord de l'image\n    '''\n    R_moyenne=0\n    V_moyenne=0\n    B_moyenne=0\n    (l, h) = taille(img)\n    d=int((c-1)\/2)\n    n=0\n    for i in range(x-d,x+d+1):\n        for j in range(y-d,y+d+1):\n            if (i>=0) and (i&lt;=l-1) and (j>=0) and (j&lt;=h-1) :\n                n+=1\n                pixel=lire_pixel(img,i,j)\n                R_moyenne+=pixel[0]\n                V_moyenne+=pixel[1]\n                B_moyenne+=pixel[2]\n    R_moyenne=int(R_moyenne\/n)\n    V_moyenne=int(V_moyenne\/n)\n    B_moyenne=int(B_moyenne\/n)\n    return (R_moyenne,V_moyenne,B_moyenne)\n\n# filtre moyenne\ndef filtre_moyenne(img,c):\n    '''\n    remplace tous les pixels de l'image\n    par la valeur moyenne calcul\u00e9e sur le\n    carr\u00e9 de c\u00f4t\u00e9 c pixel\n    '''\n    image_retour=img.copy()\n    (l, h) = taille(img)\n    for x in range(0,l):\n        for y in range(0,h):\n            nouvelle_couleur=moyenne(img,x,y,c)\n            # on remplace la couleur des pixels du carr\u00e9 par leur moyenne\n            image_retour=changer_pixel(image_retour,x,y,nouvelle_couleur)\n    afficher(image_retour)\n    return image_retour\n\n\n# pixelisation\n\ndef pixelisation(img,c):\n    image_retour=img.copy()\n    (l, h) = taille(img)\n    for x in range(0,l-c,c):\n        for y in range(0,h-c,c):\n            nouvelle_couleur=moyenne(img,x,y,c)\n            # on remplace la couleur des pixels du carr\u00e9 par leur moyenne\n            for i in range(x,x+c):\n                for j in range(y,y+c):\n                    image_retour=changer_pixel(image_retour,i,j,nouvelle_couleur)\n    afficher(image_retour)\n    return image_retour\n\n# superposition image fond vert\n\ndef superposition():\n    img=Image.open(\"seoul.jpg\")\n    image_incrustee=Image.open(\"JCVD.jpg\")\n    (l, h) = taille(image_incrustee)\n    (l1,h1)=taille(img)\n    l=min(l,l1)\n    h=min(h,h1)\n    image_retour=Image.new('RGB',(l,h) , color=0) # on cr\u00e9e une image vide\n    pixel=lire_pixel(image_incrustee,1,1)\n    r=pixel[0]\n    v=pixel[1]\n    b=pixel[2]\n    t=100 # tolerance\n    for x in range(0,l):\n        for y in range(0,h):\n            fond=lire_pixel(image_incrustee,x,y)\n            if (fond[0]>r-t) and (fond[0]&lt;r+t) and (fond[1]>v-t)  and (fond[1]&lt;v+t) and (fond[2]>b-t) and (fond[2]&lt;b+t) :\n            # on remplace la couleur des pixels du carr\u00e9 par leur moyenne\n                nouvelle_couleur=lire_pixel(img,x,y)\n                image_retour=changer_pixel(image_retour,x,y,nouvelle_couleur)\n            else :\n                nouvelle_couleur=lire_pixel(image_incrustee,x,y)\n                image_retour=changer_pixel(image_retour,x,y,nouvelle_couleur)\n\n    afficher(image_retour)\n    return image_retour<\/code><\/pre>\n\n\n\n<p>Le filtre moyenne en action:<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-3 is-cropped\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"514\" height=\"768\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/moyenne_3.png\" alt=\"\" data-id=\"970\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=970\" class=\"wp-image-970\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/moyenne_3.png 514w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/moyenne_3-201x300.png 201w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><figcaption>Moyenne c=3<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"514\" height=\"768\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/moyenne_5.png\" alt=\"\" data-id=\"971\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=971\" class=\"wp-image-971\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/moyenne_5.png 514w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/moyenne_5-201x300.png 201w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><figcaption>Moyenne c=5<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"514\" height=\"768\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/Mona_Lisa-2.jpg\" alt=\"\" data-id=\"972\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=972\" class=\"wp-image-972\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/Mona_Lisa-2.jpg 514w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/Mona_Lisa-2-201x300.jpg 201w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><figcaption>original<\/figcaption><\/figure><\/li><\/ul>\n\n\n\n<p>Le filtre pixelisation :<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-3 is-cropped\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"514\" height=\"768\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel5.png\" alt=\"\" data-id=\"973\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=973\" class=\"wp-image-973\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel5.png 514w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel5-201x300.png 201w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"514\" height=\"768\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel9.png\" alt=\"\" data-id=\"974\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=974\" class=\"wp-image-974\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel9.png 514w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel9-201x300.png 201w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"514\" height=\"768\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel11.png\" alt=\"\" data-id=\"975\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=975\" class=\"wp-image-975\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel11.png 514w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/pixel11-201x300.png 201w\" sizes=\"(max-width: 514px) 100vw, 514px\" \/><\/figure><\/li><\/ul>\n\n\n\n<p>La fonction superposition de 2 images dont l&rsquo;une avec un fond vert :<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-3 is-cropped\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"1024\" height=\"496\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/JCVD-1-1024x496.jpg\" alt=\"\" data-id=\"976\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=976\" class=\"wp-image-976\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/JCVD-1-1024x496.jpg 1024w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/JCVD-1-300x145.jpg 300w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/JCVD-1-768x372.jpg 768w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/JCVD-1.jpg 1377w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"1024\" height=\"683\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/seoul-1-1024x683.jpg\" alt=\"\" data-id=\"977\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=977\" class=\"wp-image-977\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/seoul-1.jpg 1024w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/seoul-1-300x200.jpg 300w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/seoul-1-768x512.jpg 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" width=\"1024\" height=\"667\" src=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/superposition-1024x667.png\" alt=\"\" data-id=\"978\" data-link=\"https:\/\/labodemaths.fr\/WordPress3\/?attachment_id=978\" class=\"wp-image-978\" srcset=\"https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/superposition.png 1024w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/superposition-300x195.png 300w, https:\/\/labodemaths.fr\/WordPress3\/wp-content\/uploads\/2019\/11\/superposition-768x500.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Le code complet : Le filtre moyenne en action: Le filtre pixelisation : La fonction&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/labodemaths.fr\/WordPress3\/nsi-fin-correction-tp-traitement-image\/\">Read the post<span class=\"screen-reader-text\">NSI : fin correction tp traitement image<\/span><\/a><\/div>\n","protected":false},"author":2,"featured_media":978,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/posts\/969"}],"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=969"}],"version-history":[{"count":1,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":979,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/posts\/969\/revisions\/979"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/media\/978"}],"wp:attachment":[{"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labodemaths.fr\/WordPress3\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}