# programme doing the mode of the pixels in position (x,y) from eight microscopy images # in general useful for reducing noise of images # provisorial name Allamoda (alex Moda) # it starts from four microscopy images consecutively recorded of the same # the mode is calculated as the value that appears at least two times in four images in the given position # the reason is that the probability of having the same value for two times in four images is very low # Provided under GPL licence # Author Alessio Papini, Department of Evolutionary Biology University of Florence Italy, Via La Pira, 4 Firenze, mail alpapiniATunifi.it import os, sys # same detail and in frame #versione greyscale Allamodagrey2_0.py print('The program calculates the mode of the pixels in position x,y in eight different images recorded from the same subject to reduce noise; if i obtain four 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: ') #i3b=raw_input('Write the name of the third of the three images with its path to directory if different from the current working directory: ') #i3c=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; var named i3b because it was added later #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 per test metto adesso fout.bmp i4 = "foutav8b.bmp" #PER TEST METTO I NOMI DELLE IMAGES A MANO E ESCLUDO INPUT, after i will reinsert the variables i1 i2 i3 i3b in parentheses after Image.open import Image im1 = Image.open("h9.bmp") im2 = Image.open("h10.bmp") im3 = Image.open("h11.bmp") im3b = Image.open("h12.bmp") im3c = Image.open("h13.bmp") im3d = Image.open("h14.bmp") im3e = Image.open("h15.bmp") im3f = Image.open("h16.bmp") # 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)) d=im3b.getpixel((i,j)) e=im3c.getpixel((i,j)) f=im3d.getpixel((i,j)) g=im3e.getpixel((i,j)) h=im3f.getpixel((i,j)) 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 + d +e +f +g + h)/8 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