#This program arranges in frame two microscopical images recorded on a classical film with photographic device and the scanned from negatives # provisorial name Inframe # it starts from three microscopy images consecutively recorded of the same #this verion 1.2 adjust image 2 on image 1 by cutting some columns anr rows of pixel until image 2 is in frame with the first. The cut pixels are substituted by black pixels # Provided under GPL licence # Author Alessio Papini, Department of Plant Biology University of Florence Italy, Via La Pira, 4 Firenze, mail alpapiniATunifi.it #THIS VERSION WORKS ONLY WITH TWO IMAGES TO BE PUT IN FRAME #the three images should have been recorded with just the same exposimeter level, otherwise pixelvalues cannot be compared. For TEM images it is not always so # in case of exposimeter problems you could try resetexpo1_0.py print('It starts from 3 images to put in frame. The three images should be more or less in frame by eye to accelerate search') print("The two images should not be rotated one wth respect to the other") i1=raw_input('Write the name of the first image with its path to directory if different from the current working directory: ') i2=raw_input('Write the name of the second image 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 che il programma deve mettere in frame print('now it acquires the name of the three images') i5=raw_input('Write the name of the rearranged second 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 os, sys import Image im1 = Image.open(i1) im2 = Image.open(i2) # it loads the images # per il size ho prima width e poi height quindi larghezza e altezza in pixels x, y = im1.size # width and height for the two nested cycle needed to go through the image #search box 6X6 pixels located in the center of the first image cooboxsx=((x/2)-6) cooboysx=((y/2)-6) cooboxrh=((x/2)+6) cooboyrh=((y/2)+6) #coordinate del vertice in alto a sx del box e del vertice in basso a rh box = (cooboxsx, cooboysx, cooboxrh,cooboyrh) region1 = im1.crop(box) region1.save("boxim1.bmp") im5=Image.new("RGB",(x,y)) countplus=0 #it initializes the counter of pixels in common between the central box of image 1 and the box searched in image 2 for i in range ((cooboxsx-40), (cooboxrh+40), 1): for j in range ((cooboysx-40), (cooboyrh+40), 1): #search of box 1 of image 1 dentro image 2, i will start from a perimeter starting 40 pixels up and sx and to 40 pixels down and 40 right of the center of image2 #devo caricare un box delle dimensioni di quello di partenza in im1 sulla im2 a partire dal vertice in alto a sx della zona di ricerca box = (i, j, (i+6), (j+6)) region2=im2.crop(box) region2.save("boxim2.bmp") #in region2 is the box in image 2 #the following cycle compares the box in figure 2 in the position i,j with the central box of figure 1 (region1) #identity considered ok for 32 pixels of 36 countplus=0 for l in range (6): for m in range(6): c=region1.getpixel((l,m)) d=region2.getpixel((l,m)) if c==d: countplus=countplus+1 if countplus > 8: print countplus print i,j,l,m,c,d if countplus==32: #32 pixels of 36 of identity between the boxes are considered sufficient print "match dei box!" #now we rebuild the two images in frame enlarging the two matching boxes #now i establish in wich direction x,y and in which amount is the frame shift deltax1_2=cooboxsx-i deltay1_2=cooboysx-j if deltax1_2 == 0 and deltay1_2 == 0: print("the two images are already in frame") if deltax1_2 > 0 and deltay1_2 > 0: box = (0,0,(x-(abs(deltax1_2))),(y-(abs(deltay1_2)))) region2=im2.crop(box) # region2.save(i5) im5.paste(region2, (0,0)) im5.save(i5) # box = ((abs(deltax1_2)),(abs(deltay1_2)),x,y) # region1=im1.crop(box) # region1.save(i4) # region2=im2.crop(box) #image shifted towards left and upwards # ima1cut and ima2cut should be in frame if deltax1_2 > 0 and deltay1_2 < 0: #image shifted towards left and downwards box = (0, (abs(deltay1_2)),(x-(abs(deltax1_2))),y) region2=im2.crop(box) im5.paste(region2, (0,0)) im5.save(i5) if deltax1_2 < 0 and deltay1_2 > 0: #image shifted towards right and upwards box = ((abs(deltax1_2)),0, x, (y-(abs(deltay1_2)))) region2=im2.crop(box) im5.paste(region2, (0,0)) im5.save(i5) if deltax1_2 < 0 and deltay1_2 < 0: #image shifted towards right and downwards box = ((abs(deltax1_2)),(abs(deltay1_2)), x, y) region2=im2.crop(box) im5.paste(region2, (0,0)) im5.save(i5) if deltax1_2 == 0 and deltay1_2 > 0: #image shifted upwards box = (0,0,x, (y-(abs(deltay1_2)))) region2=im2.crop(box) im5.paste(region2, (0,0)) im5.save(i5) if deltax1_2 == 0 and deltay1_2 < 0: #image shifted downwards box = (0,(abs(deltay1_2)), x, y) region2=im2.crop(box) im5.paste(region2, (0,0)) im5.save(i5) if deltax1_2 < 0 and deltay1_2 == 0: #image shifted towards right box = ((abs(deltax1_2)),0, x, y) region2=im2.crop(box) im5.paste(region2, (0,0)) im5.save(i5) if deltax1_2 > 0 and deltay1_2 == 0: box = (0,0, (x-(abs(deltax1_2))), y) region2=im2.crop(box) im5.paste(region2, (0,0)) im5.save(i5) #image shifted towards left # 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