# 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 # Provided under GPL licence # Author Alessio Papini, Department of Plant Biology University of Florence Italy, Via La Pira, 4 Firenze, mail alpapiniATunifi.it import os, sys # same detail and in frame #versione greyscale Allamodagrey1_0.py 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') # 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 im1.save(i4) # crea una immagine per ora uguale alla im1 che verra modificata via via con la analisi e conterra la # immagine finale elaborata con la moda dei pixel im4 = Image.open(i4) #carica la immagine in i4 nella variabile im4 #ora inizio i due cicli annidati lo esterno con la width e lo interno con la heighth della immagine countnoise_im2_im3=0 countnoise_im1_im2=0 countnoise_im1_im3=0 countnoiseTRIPLO=0 #inizializzo contatori e variabili for i in range (x): for j in range (y): a=im1.getpixel((i,j)) b=im2.getpixel((i,j)) c=im3.getpixel((i,j)) if a==b and b==c: im4.putpixel((i,j),(a)) #if the three images have all the same values in that pixel the final image will have that value in that position if a==b and b!=c: im4.putpixel((i,j),(a)) #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 countnoise_im1_im3=countnoise_im1_im3 + 1 #counter del rumore fra immagine im2 e immagine im3] if a!=b and b==c: im4.putpixel((i,j),(b)) #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 countnoise_im1_im2=countnoise_im1_im2 + 1 if b!=c and a==c: im4.putpixel((i,j),(a)) countnoise_im2_im3=countnoise_im2_im3 + 1 #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 a!=b and b!=c and a!=c: # diff_im1_im2=abs(a-b) #diff_im1_im3=abs(a-c) #diff_im2_im3=abs(b-c) countnoiseTRIPLO=countnoiseTRIPLO + 1 #if in that position the value is the different in the three images, im4 assumes the value of the mean of the three values m=(a + b + c)/3 im4.putpixel((i,j),(m)) 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 print('number of pixels that are different between the first and the second image (noise) is ') print countnoise_im1_im2 print('number of pixels that are different between the first and the third image (noise) is ') print countnoise_im1_im3 print('number of pixels that are different between the second and the third image (noise) is ') print countnoise_im2_im3 print('number of pixels that are different in all the three images is ') print countnoiseTRIPLO print('of a total of image pixels of ') print x*y