# programme doing the mode of the pixels in position (x,y) from three microscopy images # in general useful for reducing noise of images # provisorial name Allamoda (alex Moda) # it starts from three microscopy images consecutively recorded of the same subject import os, sys # same detail and in frame #versione greyscale Allamodagrey1_0.py #versione rgb AllamodaRGB1_0.py # Provided under GPL licence # Author Alessio Papini, Department of Plant Biology University of Florence Italy, Via La Pira, 4 Firenze, mail alpapiniATunifi.it print('The program calculates the mode of the pixels in position x,y in three different images recorded from the same subject to reduce noise; if i obtain three different values in position x,y the final image will have in that position the mean of the three values') # Alternativa potrebbe essere con la immagine finale in quel pixel la media del valore dei due valori piu vicini tra loro print('supported formats bmp, jpg, tif') print('The three images must be of the same dimensions (widhxheight) and in frame one with the other') print('This is the rgb version') # It explains what the program does i1=raw_input('Write the name of the first of the three images with its path to directory if different from the current working directory: ') i2=raw_input('Write the name of the second of the three images with its path to directory if different from the current working directory: ') i3=raw_input('Write the name of the third of the three images with its path to directory if different from the current working directory: ') #acquisisce i nomi delle tre immagini di cui il programma #fa la moda dei valori dei pixels i4=raw_input('Write the name of the final image that will result from the analysis with its path to directory if different from the current working directory: ') # name of finale resulting image import Image im1 = Image.open(i1) im2 = Image.open(i2) im3 = Image.open(i3) # it loads the images # per il size ho prima width e poi height quindi larghezza e altezza in pixels x, y = im3.size # width and height for the two nested cycle needed to go through the image: the pixel position will be in a(x,y) im1.save(i4) # it creates an image starting form the first image that will be modified by the analysis and will contain the final image im4 = Image.open(i4) #COMANDI PER RGB im1r, im1g, im1b = im1.split() im2r, im2g, im2b = im2.split() im3r, im3g, im3b = im3.split() im4r, im4g, im4b = im4.split() #splits the images in the three bands r, g e b # then necessary command merge # the algorithm must be repeated one time for each band # it begins two nested cycles the external with width and internal the heighth of the image for i in range (x): for j in range (y): ar=im1r.getpixel((i,j)) br=im2r.getpixel((i,j)) cr=im3r.getpixel((i,j)) if ar==br and br==cr: im4r.putpixel((i,j),(ar)) ag=im1g.getpixel((i,j)) bg=im2g.getpixel((i,j)) cg=im3g.getpixel((i,j)) if ag==bg and bg==cg: im4g.putpixel((i,j),(ag)) ab=im1b.getpixel((i,j)) bb=im2b.getpixel((i,j)) cb=im3b.getpixel((i,j)) if ab==bb and bb==cb: im4b.putpixel((i,j),(ab)) #if the three images have all the same values in that pixel the final image will have that value in that position #it does that one for each band r,g,b if ar==br and br!=cr: im4r.putpixel((i,j),(ar)) if ag==bg and bg!=cg: im4g.putpixel((i,j),(ag)) if ab==bb and bb!=cb: im4b.putpixel((i,j),(ab)) #if in that position the value is the same in im1 and im2 and different in im3, im4 assumes the value of im1 and im2, hence the mode of the three values if ar!=br and br==cr: im4r.putpixel((i,j),(br)) if ag!=bg and bg==cg: im4g.putpixel((i,j),(bg)) if ab!=bb and bb==cb: im4b.putpixel((i,j),(bb)) #if in that position the value is the same in im2 and im3 and different in im1, im4 assumes the value of im2 and im3, hence the mode of the three values if br!=cr and ar==cr: im4r.putpixel((i,j),(ar)) if bg!=cg and ag==cg: im4g.putpixel((i,j),(ag)) if bb!=cb and ab==cb: im4b.putpixel((i,j),(ab)) #if in that position the value is the same in im1 and im3 and different in im2, im4 assumes the value of im1 and im3, hence the mode of the three values if ar!=br and br!=cr and ar!=cr: mr=(ar + br + cr)/3 im4r.putpixel((i,j),(mr)) if ag!=bg and bg!=cg and ag!=cg: mg=(ag + bg + cg)/3 im4g.putpixel((i,j),(mg)) if ab!=bb and bb!=cb and ab!=cb: mb=(ab + bb + cb)/3 im4b.putpixel((i,j),(mb)) #if in that position the value is the different in the three images, im4 assumes the value of the mean of the three values #non trovo funzione mean quindi faccio la media a mano # alternativa se i tre valori sono diversi guardo quali sono i due #piu vicini e l'immagine finale avra la media fra quei #due im4 = Image.merge("RGB", (im4r,im4g,im4b)) # it merges the three bands rgb to the final image im4 that will get the name procided by the user im4.save(i4) print('Resulting image saved with the indicated name') print i4 #assegna alla immagine im4 modificata il nome assegnato dallo utente in input all'inizio