IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Tkinter Python Discussion :

[POO] Flux webcam dans un Canvas/Frame


Sujet :

Tkinter Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    terminale S
    Inscrit en
    Juillet 2012
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : terminale S

    Informations forums :
    Inscription : Juillet 2012
    Messages : 27
    Par défaut [POO] Flux webcam dans un Canvas/Frame
    Bonsoir a tous, cela fait 2 heures que je galère sur mon projet d'info a rendre mardi..
    Alors je viens vous demander de l'aide humblement

    Mon projet consiste a créer une petite applications PC(Windows) Qui permette de prendre une photo en direct avec une webcam, puis de la modifier avec des filtres photos.

    Pour les filtres photo c'est OK.

    Voici le code pour l'instant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    from Tkinter import *
    from PIL import Image, ImageTk
    import webbrowser
     
     
     
    def PrendrePhoto():
        print "yeah"
     
    def OuvertureImage():
        print"oh"
     
    def Enregistrer():
        print""
     
    def Tweet():
        webbrowser.open('https://twitter.com/')
     
    def Fb():
        webbrowser.open("https://fr-fr.facebook.com/login.php")
     
     
                #cr?ation des filtres
     
    def filtre_color():
     
        color=var.get()
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
     
                if (color==0):
                    im2.putpixel((x,y),(p[0],0,0))
     
                if (color==1):
                    im2.putpixel((x,y),(0,p[1],0))
     
                if (color==2):
                    im2.putpixel((x,y),(0,0,p[2]))
     
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_color.png")
        Cfiltre1.delete(ALL)
        im3 = ImageTk.PhotoImage(im2)
        Im_Color = Cfiltre1.create_image(74,51, image=im3)
     
     
    def filtre_B_W():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
                gris = int(p[0]*0.299+p[1]*0.587+p[2]*0.114)
                im2.putpixel((x,y),(gris,gris,gris))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_B-W.png")
     
     
    def filtre_negatif():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
     
                im2.putpixel((x,y),(abs(p[0]-255),abs(p[1]-255),abs(p[2]-255)))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_negative.png")
     
     
    def filtre_contrast():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = list(im.getpixel((x,y)))
     
                for i in range (0,3):
                    if p[i]<30:
                        p[i]=0
                    if p[i]>225:
                        p[i]=255
                    else :
                        p[i] = int((255.0 / 195.0) * (p[i] - 30) + 0.5)
     
     
                im2.putpixel((x,y),(p[0],p[1],p[2]))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_contrast.png")
     
     
    def filtre_lumin():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = list(im.getpixel((x,y)))
     
                for i in range (0,3):
                    p[i]= p[i]+30
     
     
                im2.putpixel((x,y),(p[0],p[1],p[2]))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_lumin.png")
     
     
     
     
     
     
     
    fen1 = Tk()
    #fen1.wm_state(newstate="zoomed")
    fen1.configure(width=960, height=621)      #cr?ation de la fenetre principale
    fen1.resizable(width=False, height=False)
     
     
    #ouverture des images
     
    background = Image.open('fond3.png')
    #background = background.resize((640,413))
    Backg = ImageTk.PhotoImage(background)
     
     
    OuvertImage = Image.open('choisirunephoto3.png')
    #OuvertImage = OuvertImage.resize((172,34))
    OuvImage = ImageTk.PhotoImage(OuvertImage)
     
    TakePhoto = Image.open('prendreunephoto3.png')
    #TakePhoto = TakePhoto.resize((172,34))
    Photo = ImageTk.PhotoImage(TakePhoto)
     
    SvIm = Image.open('enregistrerlaphoto3.png')
    SavePhoto = ImageTk.PhotoImage(SvIm)
     
    cameleon = Image.open('filtre3.png')
    #cameleon = cameleon.resize((120,120))
    Filtr1 = ImageTk.PhotoImage(cameleon)
     
    fb = Image.open('facebook3.png')
    #fb = fb.resize((120,120))
    facebook = ImageTk.PhotoImage(fb)
     
    tw = Image.open('twitter3.png')
    #tw = tw.resize((120,120))
    twitter = ImageTk.PhotoImage(tw)
     
    imColor = Image.open("im_color.png")
    ImColor = ImageTk.PhotoImage(imColor)
     
    imBW = Image.open("im_B-W.png")
    ImBW = ImageTk.PhotoImage(imBW)
     
    imContrast = Image.open("im_contrast.png")
    ImContrast = ImageTk.PhotoImage(imContrast)
     
    imLumin = Image.open("im_lumin.png")
    ImLumin = ImageTk.PhotoImage(imLumin)
     
     
    #cr?ation canvas fond ecran
     
    BG = Canvas(fen1, height=621, width=960)
    BG.place(x=0, y=0)
    Bg = BG.create_image(479,309, image=Backg)
     
     
     
    #cr?ation canvas filtre
     
    Cfiltre1 = Canvas (fen1, height=102, width=148)
    Cfiltre1.place(x=143, y=381)
     
    Cfiltre2 = Canvas (fen1, height=102, width=148)
    Cfiltre2.place(x=318, y=381)
     
    Cfiltre3 = Canvas (fen1, height=102, width=148)
    Cfiltre3.place(x=493, y=381)
     
    Cfiltre4 = Canvas (fen1, height=102, width=148)
    Cfiltre4.place(x=667, y=381)
     
    #affichage images filtres
     
    Im_Color = Cfiltre1.create_image(74,51, image=ImColor)
    Im_BW = Cfiltre2.create_image(74,51,image=ImBW)
    Im_Contrast = Cfiltre3.create_image(74,51,image=ImContrast)
    Im_Lumin = Cfiltre4.create_image(74,51,image=ImLumin)
     
     
     
    #cr?ation des boutons
     
    InserezImage = Button(fen1, height=48, width=278, image=OuvImage, relief=FLAT, command=OuvertureImage)
    InserezImage.place(x=506, y=111)
     
    CaptPhoto = Button(fen1, height=48, width=278, image=Photo, relief=FLAT, command=OuvertureImage)
    CaptPhoto.place(x=506, y=185)
     
    SaveImage = Button(fen1, height=60, width=286, image=SavePhoto, relief=FLAT, command=OuvertureImage)
    SaveImage.place(x=501, y=271)
     
    FaceBook = Button(fen1, height=86, width=87, image=facebook, command=Fb)
    FaceBook.place(x=851,y=385)
     
    Twitter = Button(fen1, height=86, width=87, image=twitter, command=Tweet)
    Twitter.place(x=851, y=485)
     
     
     
    #cr?ation radio bouton
     
    var = IntVar()
     
    filtre1 = Radiobutton(fen1, height=1, width=1, variable=var, value=0, command=filtre_color)
    filtre1.place(x=158, y=520)
     
    filtre2 = Radiobutton(fen1, height=1, width=1, variable=var, value=1, command=filtre_color)
    filtre2.place(x=198, y=520)
     
    filtre3 = Radiobutton(fen1, height=1, width=1, variable=var, value=2, command=filtre_color)
    filtre3.place(x=238, y=520)
     
     
    filtre4 = Radiobutton(fen1, height=1, width=1, variable=var, value=4, command=filtre_B_W)
    filtre4.place(x=375, y=520)
     
     
    filtre5 = Radiobutton(fen1, height=1, width=1, variable=var, value=5, command=filtre_contrast)
    filtre5.place(x=552, y=520)
     
     
    filtre6 = Radiobutton(fen1, height=1, width=1, variable=var, value=6, command=filtre_lumin)
    filtre6.place(x=729, y=520)
     
     
     
     
     
    fen1.mainloop()
    Le but est d'afficher le flux webcam dans le plus grand cadre (Où il y a pour l'instant un cameleon).


    On a opter pour le module videoCapture qui apparemment permet ce genre de choses.

    Voici un code que j'ai récupérer sur un site et modifier légèrement:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    from   VideoCapture import Device
    from   PIL          import Image, ImageTk
    import ImageFilter
    from   Tkinter      import *
    import time
     
    class Affichage(Frame):
        """ Affiche L'image Calculee """
     
        def __init__(self, parent, temps="0:0:0"):
            Frame.__init__(self, parent)
            self.parent = parent
     
            self.CameraGauche = Device(devnum = 0, showVideoWindow =0)
     
     
            self.AffichageG = Canvas(self, width = 336, height = 254)
            self.AffichageG.grid(row=1, column=1)
     
            self.Rafraichis()
     
        def Rafraichis(self):
            self.PhotoGauche  = self.CameraGauche.getImage()
     
            self.ImageGauche = ImageTk.PhotoImage(self.PhotoGauche)
     
            self.AffichageG.create_image(160,120, image = self.ImageGauche)
     
            self.parent.after(100, self.Rafraichis)
     
     
     
    root = Tk()
    root.title('Vision Stereo ....')
     
     
    Rendu = Affichage(root)
    Rendu.place(x=0, y=0)
     
    root.mainloop()
    J'ai donc réussi à afficher le flux de ma webcam dans un Frame contenant lui-même un canvas.

    Mais je ne connais pas du tout la programmation orientée objet, alors avec un petit tuto, j'ai compris le code dans ses grandes lignes. (Définition de la classe Affichage semblable aux frame etc.)


    Pour finir, j'ai essayer d'intégrer Le dernier code dans mon projet. ce qui donne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    from Tkinter import *
    from PIL import Image, ImageTk
    import webbrowser
    from VideoCapture import Device
    import time
     
                        #--> Nouvelles lignes de code <--#
     
    class Affichage(Frame):
        """ Affiche L'image Calculee """
     
        def __init__(self, parent, temps="0:0:0"):
            Frame.__init__(self, parent)
            self.parent = parent
     
            self.CameraGauche = Device(devnum = 0, showVideoWindow =0)
     
     
            self.AffichageG = Canvas(self, width = 336, height = 254)
            self.AffichageG.place(x=138, y=88)
     
            self.Rafraichis()
     
        def Rafraichis(self):
            self.PhotoGauche  = self.CameraGauche.getImage()
     
            self.ImageGauche = ImageTk.PhotoImage(self.PhotoGauche)
     
            self.AffichageG.create_image(160,120, image = self.ImageGauche)
     
            self.parent.after(100, self.Rafraichis)
     
     
                    #------------------------------#
     
     
    def WebCam():
        a
     
     
    def OuvertureImage():
        cam1 = Device(devnum=0, showVideoWindow=1)
     
        imagecam1 = cam1.getImage()
        imagecam1.save("Main_Im.png")
     
        Main_Im=Image.open("Main_Im.png")
        Main_Image = ImageTk.PhotoImage(Main_Im)
        Main_Photo = CanPrinc.create_image(168,127, Main_Image)
     
    def Enregistrer():
        print""
     
    def Tweet():
        webbrowser.open('fr.twitter.com')
     
    def Fb():
        webbrowser.open('fr-fr.facebook.com')
     
     
                #cr?ation des filtres
     
    def filtre_color():
     
        color=var.get()
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
     
                if (color==0):
                    im2.putpixel((x,y),(p[0],0,0))
     
                if (color==1):
                    im2.putpixel((x,y),(0,p[1],0))
     
                if (color==2):
                    im2.putpixel((x,y),(0,0,p[2]))
     
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_color.png")
     
     
     
    def filtre_B_W():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
                gris = int(p[0]*0.299+p[1]*0.587+p[2]*0.114)
                im2.putpixel((x,y),(gris,gris,gris))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_B-W.png")
     
     
    def filtre_negatif():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
     
                im2.putpixel((x,y),(abs(p[0]-255),abs(p[1]-255),abs(p[2]-255)))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_negative.png")
     
     
    def filtre_contrast():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = list(im.getpixel((x,y)))
     
                for i in range (0,3):
                    if p[i]<30:
                        p[i]=0
                    if p[i]>225:
                        p[i]=255
                    else :
                        p[i] = int((255.0 / 195.0) * (p[i] - 30) + 0.5)
     
     
                im2.putpixel((x,y),(p[0],p[1],p[2]))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_contrast.png")
     
     
    def filtre_lumin():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = list(im.getpixel((x,y)))
     
                for i in range (0,3):
                    p[i]= p[i]+30
     
     
                im2.putpixel((x,y),(p[0],p[1],p[2]))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_lumin.png")
     
     
     
     
     
     
     
    fen1 = Tk()
    #fen1.wm_state(newstate="zoomed")
    fen1.configure(width=960, height=621)      #cr?ation de la fenetre principale
    fen1.resizable(width=False, height=False)
     
     
     
    #ouverture des images
     
    background = Image.open('fond3.png')
    #background = background.resize((640,413))
    Backg = ImageTk.PhotoImage(background)
     
     
    OuvertImage = Image.open('choisirunephoto3.png')
    #OuvertImage = OuvertImage.resize((172,34))
    OuvImage = ImageTk.PhotoImage(OuvertImage)
     
    TakePhoto = Image.open('prendreunephoto3.png')
    #TakePhoto = TakePhoto.resize((172,34))
    Photo = ImageTk.PhotoImage(TakePhoto)
     
    SvIm = Image.open('enregistrerlaphoto3.png')
    SavePhoto = ImageTk.PhotoImage(SvIm)
     
    cameleon = Image.open('filtre3.png')
    #cameleon = cameleon.resize((120,120))
    Filtr1 = ImageTk.PhotoImage(cameleon)
     
    fb = Image.open('facebook3.png')
    #fb = fb.resize((120,120))
    facebook = ImageTk.PhotoImage(fb)
     
    tw = Image.open('twitter3.png')
    #tw = tw.resize((120,120))
    twitter = ImageTk.PhotoImage(tw)
     
    imColor = Image.open('im_color.png')
    Im_Color = ImageTk.PhotoImage(imColor)
     
    imBW = Image.open('im_B-W.png')
    Im_BW = ImageTk.PhotoImage(imBW)
     
    imLumin = Image.open('im_lumin.png')
    Im_Lumin = ImageTk.PhotoImage(imLumin)
     
    imContrast = Image.open('im_contrast.png')
    Im_Contrast = ImageTk.PhotoImage(imContrast)
     
     
    #cr?ation canvas fond ecran
     
    BG = Canvas(fen1, height=621, width=960)
    BG.place(x=0, y=0)
    Bg = BG.create_image(479,309, image=Backg)
     
     
    #cr?ation canvas filtre
     
    Cfiltre1 = Canvas (fen1, height=102, width=148)
    Cfiltre1.place(x=143, y=381)
     
    Cfiltre2 = Canvas (fen1, height=102, width=148)
    Cfiltre2.place(x=318, y=381)
     
    Cfiltre3 = Canvas (fen1, height=102, width=148)
    Cfiltre3.place(x=493, y=381)
     
    Cfiltre4 = Canvas (fen1, height=102, width=148)
    Cfiltre4.place(x=667, y=381)
     
    #affichage images filtres
     
    ImColor = Cfiltre1.create_image(74,51,image=Im_Color)
    ImBW = Cfiltre2.create_image(74,51,image=Im_BW)
    ImContrast = Cfiltre3.create_image(74,51,image=Im_Contrast)
    ImLumin = Cfiltre4.create_image(74,51,image=Im_Lumin)
     
    #--> Création canvas photo principale <--
     
    #CanPrinc = Canvas (fen1, height=254, width=336)
    #CanPrinc.place(x=138, y=88)
     
     
    #cr?ation des boutons
     
    InserezImage = Button(fen1, height=48, width=278, image=OuvImage, relief=FLAT, command=OuvertureImage)
    InserezImage.place(x=506, y=111)
     
    CaptPhoto = Button(fen1, height=48, width=278, image=Photo, relief=FLAT, command=WebCam)
    CaptPhoto.place(x=506, y=185)
     
    SaveImage = Button(fen1, height=60, width=286, image=SavePhoto, relief=FLAT, command=OuvertureImage)
    SaveImage.place(x=501, y=271)
     
    Twitter = Button(fen1, height=87, width=87, image=twitter, command=Tweet)
    Twitter.place(x=850, y=480)
     
    FaceBook = Button(fen1, height=87, width=87, image=facebook, command=Fb)
    FaceBook.place(x=850, y=370)
     
     
    #cr?ation radio bouton
     
    var = IntVar()
     
    filtre1 = Radiobutton(fen1, height=1, width=1, bg='white', variable=var, value=0, command=filtre_color)
    filtre1.place(x=198, y=520)
     
    filtre2 = Radiobutton(fen1, height=1, width=1, variable=var, value=1, command=filtre_color)
    filtre2.place(x=158, y=520)
     
    filtre3 = Radiobutton(fen1, height=1, width=1, variable=var, value=2, command=filtre_color)
    filtre3.place(x=238, y=520)
     
     
    filtre4 = Radiobutton(fen1, height=1, width=1, variable=var, value=4, command=filtre_B_W)
    filtre4.place(x=375, y=520)
     
     
    filtre5 = Radiobutton(fen1, height=1, width=1, variable=var, value=5, command=filtre_contrast)
    filtre5.place(x=552, y=520)
     
     
    filtre6 = Radiobutton(fen1, height=1, width=1, variable=var, value=6, command=filtre_lumin)
    filtre6.place(x=729, y=520)
     
     
                        #--> Nouvelles lignes de code <--#
     
    Rendu = Affichage(fen1)
    Rendu.place(x=0, y=0)
     
                        #------------------------------#
    fen1.title('Cameleon')
    fen1.mainloop()
    -> Les lignes de code rajoutées sont précédées du commentaire "Nouvelles lignes de code". (soit tout en haut et tout en bas)


    Mais comme vous pouvait le constater, le flux de la webcam ne s'affiche pas dans mon canvas.
    Je ne comprend pas du tout pourquoi, alors si une âme charitable passe par là, je lui serait grandement reconnaissant de m'éclairer


    Merci d'avance !

  2. #2
    Membre Expert Avatar de PauseKawa
    Homme Profil pro
    Technicien Help Desk, maintenance, réseau, système et +
    Inscrit en
    Juin 2006
    Messages
    2 725
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Technicien Help Desk, maintenance, réseau, système et +
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 725
    Par défaut
    Bonjour,

    .place demande de la rigueur : Vous avez juste un souci de géométrie.
    Pas le temps de jouer avec place et Windows mais un simple self.AffichageG.pack() (.place(x=138, y=88)) devrais vous montrer la chose.

    Note : Pourquoi créer un Canvas pour chaque image alors qu'elles sont alignées ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Cfiltre1 = Canvas (fen1, height=102, width=148, bg='yellow')
    Cfiltre1.place(x=143, y=381)
     
    Cfiltre2 = Canvas (fen1, height=102, width=148, bg='yellow')
    Cfiltre2.place(x=318, y=381)
     
    Cfiltre3 = Canvas (fen1, height=102, width=148, bg='yellow')
    Cfiltre3.place(x=493, y=381)
     
    Cfiltre4 = Canvas (fen1, height=102, width=148, bg='yellow')
    Cfiltre4.place(x=667, y=381)
    Vous pouvez n'en créer qu'un.

    Etc...

    Bon courage.

    @+

  3. #3
    Membre averti
    Homme Profil pro
    terminale S
    Inscrit en
    Juillet 2012
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : terminale S

    Informations forums :
    Inscription : Juillet 2012
    Messages : 27
    Par défaut
    PauseKawa, vous êtes un génie !
    Cela marche, merci beaucoup. Mais je ne comprend pas pourquoi mon .place était mauvais.


    Maintenant, je ne vois pas comment prendre une photo lorsque j'appuie sur "prendre une photo".
    J'essaye de supprimer mon objet "Rendu", c'est a dire le flux de ma webcam et d'afficher l'image prise par la webcam au moment de la suppression de rendu grâce à :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    cam = Device(devnum=0, showVideoWindow=0) #Affichage de la fenetre video
     
        imagecam = cam.getImage()
        Image_cam = ImageTk.PhotoImage(imagecam)
     
        Main_Image = CanPrinc.create_image(168,127, image=Image_cam)
    Mais lorsque j’essaie de supprimer le flux de cette manière :
    J'obtient l'erreur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Traceback (most recent call last):
      File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
        return self.func(*args)
      File "C:\python\isnprojetbac\Cameleon\Cameleon v4.py", line 54, in PrisePhoto
        del Rendu
    UnboundLocalError: local variable 'Rendu' referenced before assignment
    >>>
    Pourtant mon objet 'Rendu' est défini avant puisque mon flux s'affiche !
    Donc j'ai encore une fois besoin de votre aide


    Merci d'avance.


    PS : Voici mon code complet, 'del Rendu' se trouve dans ma fonction 'PrisePhoto' au début de mon code.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    from Tkinter import *
    from PIL import Image, ImageTk
    import webbrowser
    from VideoCapture import Device
    import time
     
                        #--> Nouvelles lignes de code <--#
     
    class Affichage(Frame):
        """ Affiche L'image Calculee """
     
        def __init__(self, parent, temps="0:0:0"):
            Frame.__init__(self, parent)
            self.parent = parent
     
            self.CameraGauche = Device(devnum = 0, showVideoWindow =0)
     
     
            self.AffichageG = Canvas(self, width = 336, height = 254)
            self.AffichageG.pack()
     
            self.Rafraichis()
     
        def Rafraichis(self):
            self.PhotoGauche  = self.CameraGauche.getImage()
     
            self.ImageGauche = ImageTk.PhotoImage(self.PhotoGauche)
     
            self.AffichageG.create_image(168,127, image = self.ImageGauche)
     
            self.parent.after(100, self.Rafraichis)
     
     
     
                    #------------------------------#
     
     
    def PrisePhoto():
        #cam = Device(devnum=0, showVideoWindow=0) #Affichage de la fenetre video
     
        #imagecam = cam.getImage()
        #Image_cam = ImageTk.PhotoImage(imagecam)
        del Rendu
        #Main_Image = CanPrinc.create_image(168,127, image=Image_cam)
     
     
     
    def OuvertureImage():
        a
     
     
     
    def Enregistrer():
        print""
     
    def Tweet():
        webbrowser.open('fr.twitter.com')
     
    def Fb():
        webbrowser.open('fr-fr.facebook.com')
     
     
                #cr?ation des filtres
     
    def filtre_color():
     
        color=var.get()
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
     
                if (color==0):
                    im2.putpixel((x,y),(p[0],0,0))
     
                if (color==1):
                    im2.putpixel((x,y),(0,p[1],0))
     
                if (color==2):
                    im2.putpixel((x,y),(0,0,p[2]))
     
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_color.png")
     
     
     
    def filtre_B_W():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
                gris = int(p[0]*0.299+p[1]*0.587+p[2]*0.114)
                im2.putpixel((x,y),(gris,gris,gris))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_B-W.png")
     
     
    def filtre_negatif():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = im.getpixel((x,y))
     
                im2.putpixel((x,y),(abs(p[0]-255),abs(p[1]-255),abs(p[2]-255)))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_negative.png")
     
     
    def filtre_contrast():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = list(im.getpixel((x,y)))
     
                for i in range (0,3):
                    if p[i]<30:
                        p[i]=0
                    if p[i]>225:
                        p[i]=255
                    else :
                        p[i] = int((255.0 / 195.0) * (p[i] - 30) + 0.5)
     
     
                im2.putpixel((x,y),(p[0],p[1],p[2]))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_contrast.png")
     
     
    def filtre_lumin():
     
        im=Image.open("filtre3.png")
        #im.show()
        w,h=im.size
        im2 = Image.new("RGB", (w,h))
     
        for y in range (h):
     
            for x in range(w):
                p = list(im.getpixel((x,y)))
     
                for i in range (0,3):
                    p[i]= p[i]+30
     
     
                im2.putpixel((x,y),(p[0],p[1],p[2]))
     
     
                #endif
            #endfor
        #endfor
     
        im2.save("im_lumin.png")
     
     
     
     
     
     
     
    fen1 = Tk()
    #fen1.wm_state(newstate="zoomed")
    fen1.configure(width=960, height=621)      #cr?ation de la fenetre principale
    fen1.resizable(width=False, height=False)
     
     
     
    #ouverture des images
     
    background = Image.open('fond3.png')
    #background = background.resize((640,413))
    Backg = ImageTk.PhotoImage(background)
     
     
    OuvertImage = Image.open('choisirunephoto3.png')
    #OuvertImage = OuvertImage.resize((172,34))
    OuvImage = ImageTk.PhotoImage(OuvertImage)
     
    TakePhoto = Image.open('prendreunephoto3.png')
    #TakePhoto = TakePhoto.resize((172,34))
    Photo = ImageTk.PhotoImage(TakePhoto)
     
    SvIm = Image.open('enregistrerlaphoto3.png')
    SavePhoto = ImageTk.PhotoImage(SvIm)
     
    cameleon = Image.open('filtre3.png')
    #cameleon = cameleon.resize((120,120))
    Filtr1 = ImageTk.PhotoImage(cameleon)
     
    fb = Image.open('facebook3.png')
    #fb = fb.resize((120,120))
    facebook = ImageTk.PhotoImage(fb)
     
    tw = Image.open('twitter3.png')
    #tw = tw.resize((120,120))
    twitter = ImageTk.PhotoImage(tw)
     
    imColor = Image.open('im_color.png')
    Im_Color = ImageTk.PhotoImage(imColor)
     
    imBW = Image.open('im_B-W.png')
    Im_BW = ImageTk.PhotoImage(imBW)
     
    imLumin = Image.open('im_lumin.png')
    Im_Lumin = ImageTk.PhotoImage(imLumin)
     
    imContrast = Image.open('im_contrast.png')
    Im_Contrast = ImageTk.PhotoImage(imContrast)
     
     
    #cr?ation canvas fond ecran
     
    BG = Canvas(fen1, height=621, width=960)
    BG.place(x=0, y=0)
    Bg = BG.create_image(479,309, image=Backg)
     
     
    #cr?ation canvas filtre
     
    Cfiltre1 = Canvas (fen1, height=102, width=148)
    Cfiltre1.place(x=143, y=381)
     
    Cfiltre2 = Canvas (fen1, height=102, width=148)
    Cfiltre2.place(x=318, y=381)
     
    Cfiltre3 = Canvas (fen1, height=102, width=148)
    Cfiltre3.place(x=493, y=381)
     
    Cfiltre4 = Canvas (fen1, height=102, width=148)
    Cfiltre4.place(x=667, y=381)
     
    #affichage images filtres
     
    ImColor = Cfiltre1.create_image(74,51,image=Im_Color)
    ImBW = Cfiltre2.create_image(74,51,image=Im_BW)
    ImContrast = Cfiltre3.create_image(74,51,image=Im_Contrast)
    ImLumin = Cfiltre4.create_image(74,51,image=Im_Lumin)
     
    #--> Cr?ation canvas photo principale <--
     
    CanPrinc = Canvas (fen1, height=254, width=336)
    CanPrinc.place(x=138, y=88)
     
     
    #cr?ation des boutons
     
    InserezImage = Button(fen1, height=48, width=278, image=OuvImage, relief=FLAT, command=OuvertureImage)
    InserezImage.place(x=506, y=185)
     
    CaptPhoto = Button(fen1, height=48, width=278, image=Photo, relief=FLAT, command=PrisePhoto)
    CaptPhoto.place(x=506, y=111)
     
    SaveImage = Button(fen1, height=60, width=286, image=SavePhoto, relief=FLAT, command=OuvertureImage)
    SaveImage.place(x=501, y=271)
     
    Twitter = Button(fen1, height=87, width=87, image=twitter, command=Tweet)
    Twitter.place(x=850, y=480)
     
    FaceBook = Button(fen1, height=87, width=87, image=facebook, command=Fb)
    FaceBook.place(x=850, y=370)
     
     
    #cr?ation radio bouton
     
    var = IntVar()
     
    filtre1 = Radiobutton(fen1, height=1, width=1, bg='white', variable=var, value=0, command=filtre_color)
    filtre1.place(x=198, y=520)
     
    filtre2 = Radiobutton(fen1, height=1, width=1, variable=var, value=1, command=filtre_color)
    filtre2.place(x=158, y=520)
     
    filtre3 = Radiobutton(fen1, height=1, width=1, variable=var, value=2, command=filtre_color)
    filtre3.place(x=238, y=520)
     
     
    filtre4 = Radiobutton(fen1, height=1, width=1, variable=var, value=4, command=filtre_B_W)
    filtre4.place(x=375, y=520)
     
     
    filtre5 = Radiobutton(fen1, height=1, width=1, variable=var, value=5, command=filtre_contrast)
    filtre5.place(x=552, y=520)
     
     
    filtre6 = Radiobutton(fen1, height=1, width=1, variable=var, value=6, command=filtre_lumin)
    filtre6.place(x=729, y=520)
     
     
                        #--> Nouvelles lignes de code <--#
     
    Rendu = Affichage(CanPrinc)
    Rendu.place(x=0, y=0)
     
                        #------------------------------#
    fen1.title('Cameleon')
    fen1.mainloop()

  4. #4
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 754
    Par défaut
    Salut,
    Par defaut, les variables d'une fonction sont "locales". Et en Python une variable existe apres qu'on lui ait assigne un objet.
    Il faudrait preciser global Rendu.
    Ceci dit, l'objet assigne a Rendu est une instance de la classe Affichage qui hérite de tk.Frame. del Rendu supprimera la référence a cet objet et la clé 'Rendu' dans le dictionnaire globals() sans détruire l'objet tk.
    Il faudrait pour cela appliquer la méthode .destroy.
    Ce qui donnerait:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    def PrisePhoto():
        global Rendu
        Rendu.destroy()
        del Rendu
    Ayant créé un class pour Affiche, la logique voudrait que les changements d’état des instances soient gérées par ses méthodes.
    Exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    CaptPhoto = Button(fen1, height=48, width=278, image=Photo, relief=FLAT, command=Rendu.destroy)
    En supposant avoir cree Rendu plus tot.
    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

  5. #5
    Membre averti
    Homme Profil pro
    terminale S
    Inscrit en
    Juillet 2012
    Messages
    27
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : terminale S

    Informations forums :
    Inscription : Juillet 2012
    Messages : 27
    Par défaut
    En effet, il est très fort désormais !
    merci beaucoup.

    Maintenant, je parviens a afficher le flux de ma webcam et le supprimer, lorsque j'appuie sur le bouton 'prendre une photo'.
    Et je parviens également a afficher une image de ma webcam dans mon canvas principal (en récupérant cette image grâce a la methode .getImage), lorsque je n'affiche pas le flux.

    En revanche lorsque je fais les deux a la fois, c'est a dire que j'affiche le flux, puis dès que j'appuie sur le bouton, je le supprime, et prend une photo que j'affiche, mon programme se bloque après pression sur le bouton.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    def PrisePhoto():
     
     
        global Rendu
        Rendu.destroy()
        del Rendu
     
     
        global CanPrinc
     
        cam = Device()
        imagecam = cam.getImage()
        IMAGECAM = ImageTk.PhotoImage(imagecam)
     
        global IMAGECAM
     
        MainIm = CanPrinc.create_image(168,127, image=IMAGECAM)
    Je ne vois pas ou est le problème, peut être que ça bloque car ma webcam est utilisé deux fois.

    Enfin, j'ai encore besoin d'aide

  6. #6
    Expert éminent
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 754
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 754
    Par défaut
    Salut,

    Si vous ne postez pas un code plus complet qui permette de reproduire un peu ce que vous racontez, pas facile de vous répondre.
    - W
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Accéder au flux de deux webcam dans deux form differentes
    Par chapitre37 dans le forum C++Builder
    Réponses: 5
    Dernier message: 15/03/2012, 15h32
  2. Passage d'infos dans une autre frame
    Par dumser1 dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 23/11/2005, 09h28
  3. [POO] Problème inclusion dans une classe
    Par LordBob dans le forum Langage
    Réponses: 11
    Dernier message: 22/11/2005, 15h21
  4. Deformer une Police dans un Canvas
    Par zarbydigital dans le forum Langage
    Réponses: 2
    Dernier message: 10/11/2005, 10h51
  5. [vbscript] écrire dans un autre frame
    Par gremar dans le forum ASP
    Réponses: 3
    Dernier message: 01/03/2005, 17h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo