import numpy message = "" try: # Si vous avez modifié le nom du fichier, # indiquer le nouveau nom à la place de dm1 # Par exemple si c'est le fichier dm.py, on mettra : # import dm as dm import dm1 as dm except ImportError: message += "Pour tester ce fichier, vous devez modifier le import dm1 as dm\n" message += "en import nomfichier as dm où nomfichier.py est votre fichier python" print(message) with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise except Exception as err: x, y = err.args message += "Vous ne pouvez pas rendre le devoir en l'état \n" if type(err) == IndentationError: message += " Il y a une erreur d'indentation : Ligne " message += str(y[1]) + "\n" message += " Pensez à vérifier la ligne au-dessus\n" elif type(err) == SyntaxError: message += " Il y a une erreur de syntaxe : Ligne " message += str(y[1]) + "\n" message += " Pensez à vérifier les parenthèses ou les :\n" print(message) with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise image_tab = numpy.array([[(255, 200, 60), (100, 200, 75), (200, 200, 27)] ,[(255, 200, 60), (100, 200, 75), (200, 200, 27)] ,[(255, 200, 60), (100, 200, 75), (200, 200, 27)]]) texte = "*******************TEST Exercice 1 : ***********************\n" texte += "*******************image_rouge_gris : ***********************\n" message += texte print(texte) try : image_tab2 = dm.image_rouge_gris(image_tab) except Exception as err: texte = "" texte += "Vous ne pouvez pas rendre le devoir en l'état \n" texte += "Il y a une erreur dans image _rouge_gris\n" print(texte) message+=texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise if type(image_tab2) == type(None): texte = " La fonction image _rouge_gris ne renvoie rien.\n" texte += " Elle n'est peut-être pas finie\n" print(texte) message += texte elif type(image_tab2[0,0]) != int and type(image_tab2[0,0]) != numpy.uint8: texte = " La fonction image _rouge_gris ne renvoie pas une image grise.\n" texte += " Vérifiez que ce n'est pas une image couleur\n" print(texte) message += texte else: image_tab_correction = numpy.array([[255, 100, 200] ,[255, 100, 200] ,[255, 100, 200]]) bon = True for i in range(len(image_tab2)): for j in range(len(image_tab2[0])): if image_tab_correction[i,j] != image_tab2[i,j]: bon = False if bon: texte = " La fonction image _rouge_gris semble bonne\n" else: texte = " La fonction image_rouge_gris ne renvoie pas le bon résultat\n" print(texte) message+=texte texte = "\n*******************image_rouge : ***********************\n" message += texte print(texte) try : image_tab2 = dm.image_rouge(image_tab) except Exception as err: texte = "Vous ne pouvez pas rendre le devoir en l'état \n" texte += "Il y a une erreur dans image _rouge\n" print(texte) message+=texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise if type(image_tab2) == type(None): texte = " La fonction image _rouge ne renvoie rien.\n" texte += " Elle n'est peut-être pas finie" print(texte) message += texte elif type(image_tab2[0,0]) != numpy.ndarray and len(image_tab2[0,0])!=3: texte = " La fonction image _rouge ne renvoie pas une image couleur.\n" texte += " Vérifiez que ce n'est pas une image grise" print(texte) message += texte else: image_tab_correction = numpy.array([[(255, 0, 0), (100, 0, 0), (200, 0, 0)] ,[(255, 0, 0), (100, 0, 0), (200, 0, 0)] ,[(255, 0, 0), (100, 0, 0), (200, 0, 0)]]) bon = True for i in range(len(image_tab2)): for j in range(len(image_tab2[0])): for couleur in range(3): if image_tab_correction[i,j][couleur] != image_tab2[i,j][couleur]: bon = False if bon: texte = " La fonction image _rouge semble bonne\n" else: texte = " La fonction image_rouge ne renvoie pas le bon résultat\n" print(texte) message+=texte texte = "\n*******************TEST Exercice 2 : ***********************\n" texte += "******************* rotation : ***********************\n" message += texte print(texte) image_tab = numpy.array([[(255, 200, 60), (100, 200, 75), (200, 200, 27)] ,[(255, 200, 60), (100, 200, 75), (200, 200, 27)]]) try : image_tab2 = dm.rotation(image_tab) except Exception as err: texte = "" texte += "Vous ne pouvez pas rendre le devoir en l'état \n" texte += "Il y a une erreur dans rotation\n" print(texte) message+=texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise if type(image_tab2) == type(None): texte = " La fonction rotation ne renvoie rien.\n" texte += " Elle n'est peut-être pas finie\n" print(texte) message += texte elif len(image_tab)!=len(image_tab2[0]) or len(image_tab[0])!=len(image_tab2): texte = " Les dimensions ne sont pas bonnes.\n" print(texte) message += texte else: image_tab_correction = numpy.array([[(200, 200, 27), (200, 200, 27)], [(100, 200, 75), (100, 200, 75)], [(255, 200, 60), (255, 200, 60)]]) bon = True for i in range(len(image_tab2)): for j in range(len(image_tab2[0])): for c in range(3): if image_tab_correction[i,j][c] != image_tab2[i,j][c]: bon = False if bon: texte = " La fonction rotation semble bonne\n" else: texte = " La fonction rotation ne renvoie pas le bon résultat\n" print(texte) message+=texte texte = "\n******************* TEST Exercice 3 : ***********************\n" texte += "******************* Agrandissement : ***********************\n" message += texte print(texte) image_tab = numpy.array([[(255, 200, 60), (100, 200, 75), (200, 200, 27)] ,[(255, 200, 60), (100, 200, 75), (200, 200, 27)]]) try : image_tab2 = dm.agrandissement(image_tab) except Exception as err: texte = "" texte += "Vous ne pouvez pas rendre le devoir en l'état \n" texte += "Il y a une erreur dans agrandissement\n" print(texte) message+=texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise if type(image_tab2) == type(None): texte = " La fonction agrandissement ne renvoie rien.\n" texte += " Elle n'est peut-être pas finie\n" print(texte) message+=texte elif 2*len(image_tab)!=len(image_tab2) or 2*len(image_tab[0])!=len(image_tab2[0]): texte = " Les dimensions ne sont pas bonnes.\n" print(texte) message += texte else: image_tab_correction = numpy.array([[(255, 200, 60), (255, 200, 60), (100, 200, 75), (100, 200, 75), (200, 200, 27), (200, 200, 27)], [(255, 200, 60), (255, 200, 60), (100, 200, 75), (100, 200, 75), (200, 200, 27), (200, 200, 27)], [(255, 200, 60), (255, 200, 60), (100, 200, 75), (100, 200, 75), (200, 200, 27), (200, 200, 27)], [(255, 200, 60), (255, 200, 60), (100, 200, 75), (100, 200, 75), (200, 200, 27), (200, 200, 27)]]) bon = True for i in range(len(image_tab2)): for j in range(len(image_tab2[0])): for c in range(3): if image_tab_correction[i,j][c] != image_tab2[i,j][c]: bon = False if bon: texte = " La fonction agrandissement semble bonne\n" else: texte = " La fonction agrandissement ne renvoie pas le bon résultat\n" print(texte) message+=texte texte = "\n******************* Réduction : ***********************\n" message += texte print(texte) image_tab = numpy.array([[(50, 70, 80), (60, 80, 90)] ,[(40, 60, 70), (50, 70, 80)]]) try : image_tab2 = dm.reduction(image_tab) except Exception as err: texte = "" texte += "Vous ne pouvez pas rendre le devoir en l'état \n" texte += "Il y a une erreur dans reduction\n" print(texte) message+=texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise if type(image_tab2) == type(None): texte = " La fonction reduction ne renvoie rien.\n" texte += " Elle n'est peut-être pas finie\n" print(texte) message+=texte elif len(image_tab)!= 2*len(image_tab2) or len(image_tab[0])!= 2*len(image_tab2[0]): texte = " Les dimensions ne sont pas bonnes.\n" print(texte) message += texte else: image_tab_correction = numpy.array([[(50, 70, 80)]]) bon = True for c in range(3): if image_tab_correction[0,0][c] != image_tab2[0,0][c]: bon = False if bon: texte = " La fonction reduction semble bonne\n" else: texte = " La fonction reduction ne renvoie pas le bon résultat\n" print(texte) message+=texte texte = "\n******************* TEST Exercice 4 : ***********************\n" texte += "******************* contour_couleur : ***********************\n" message += texte print(texte) image_tab = numpy.array([[(255, 200, 60), (100, 200, 75), (200, 200, 27)] ,[(255, 200, 60), (100, 200, 75), (200, 200, 27)]]) try : image_tab2 = dm.contour_couleur(image_tab) except Exception as err: texte = "" texte += "Vous ne pouvez pas rendre le devoir en l'état \n" texte += "Il y a une erreur dans contour_couleur\n" print(texte) message+=texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise if type(image_tab2) == type(None): texte = " La fonction contour_couleur ne renvoie rien.\n" texte += " Elle n'est peut-être pas finie\n" print(texte) message+=texte elif len(image_tab)!= len(image_tab2) or len(image_tab[0])!= len(image_tab2[0]): print(image_tab) print(image_tab2) texte = " Les dimensions ne sont pas bonnes.\n" print(texte) message += texte else: bon = True for i in range(1,len(image_tab)-1): for j in range(1,len(image_tab[0])-1): if image_tab2[i,j] !=0 or image_tab2[i,j] != 255 or image_tab2[i,j] !=1: bon = False if bon: texte = " La fonction contour_couleur renvoie bien une image noir et blanc\n" else: texte = " La fonction contour_couleur ne renvoie pas le bon résultat\n" print(texte) message+=texte texte = "\n******************* dilatation : ***********************\n" message += texte print(texte) image_tab = numpy.array([[255, 255, 255 ], [255, 255, 255], [255, 255, 0 ]]) image_tab3 = numpy.array([[255, 255, 255 ], [255, 0, 255], [255, 255, 255 ]]) try : image_tab2 = dm.dilatation(image_tab) image_tab4 = dm.dilatation(image_tab3) except Exception as err: texte = "" texte += "Vous ne pouvez pas rendre le devoir en l'état \n" texte += "Il y a une erreur dans dilatation\n" texte += "Attention le nom de la fonction est dilation et non dilater\n" print(texte) message+=texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise if type(image_tab2) == type(None): texte = " La fonction dilater ne renvoie rien.\n" texte += " Elle n'est peut-être pas finie\n" print(texte) message+=texte elif len(image_tab)!= len(image_tab2) or len(image_tab[0])!= len(image_tab2[0]): print(image_tab) print(image_tab2) texte = " Les dimensions ne sont pas bonnes.\n" print(texte) message += texte else: bon = True if image_tab2[1,1]==0 and image_tab4[1,1]==255: texte = " La fonction dilater a l'air de fonctionner\n" else: texte = " La fonction dilater ne renvoie pas le bon résultat\n" print(texte) message+=texte texte = "\n******************* érosion : ***********************\n" message += texte print(texte) image_tab = numpy.array([[0, 0, 0 ], [0, 255, 0], [0, 0, 0 ]]) image_tab3 = numpy.array([[255, 0, 0 ], [0, 0, 0], [0, 0, 0 ]]) try : image_tab2 = dm.erosion(image_tab) image_tab4 = dm.erosion(image_tab3) except Exception as err: texte = "" texte += "Vous ne pouvez pas rendre le devoir en l'état \n" texte += "Il y a une erreur dans erosion\n" print(texte) message+=texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message) raise if type(image_tab2) == type(None): texte = " La fonction erosion ne renvoie rien.\n" texte += " Elle n'est peut-être pas finie\n" print(texte) message+=texte elif len(image_tab)!= len(image_tab2) or len(image_tab[0])!= len(image_tab2[0]): print(image_tab) print(image_tab2) texte = " Les dimensions ne sont pas bonnes.\n" print(texte) message += texte else: bon = True if image_tab2[1,1]==0 and image_tab4[1,1]==255: texte = " La fonction erosion a l'air de fonctionner\n" else: texte = " La fonction dilater ne renvoie pas le bon résultat\n" print(texte) message+=texte texte = "\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" texte += "Si tout est bon : vous pouvez rendre le DM en l'état\n" texte += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" print(texte) message += texte with open('dm.log', 'w') as mon_fichier: mon_fichier.write(message)