- Home
- Users & Science
- Find a beamline
- Matter at extremes
- ID06 Large Volume Press
- Users guide
- Data Treatment
- Python scripts
- remove "chip" gaps from .edf 2-D image
remove "chip" gaps from .edf 2-D image
I recommend this script in cases when masking does not solve a problem with detector gaps. The script is time and memory consuming. It can be used in cases when a nice "clear" picture is needed or with an automatic background constraction softwares (such as "Match!"). The script below uses an image data set containing gaps (<= 0 values), replaces them by interpolating from neighboring data points with a given kernel. Detailed discription is here. It saves the interpolated image to a new .tiff or .edf file (see coments in the code).
Copy the py script below in any text editor and save it in a file with a .py extension. In an .edf directory create a folder named "tiff".
You need:
numpy
fabio
glob
os
PIL
astropy.convolution
import numpy as np
import fabio
import glob
import os
from PIL import Image
from astropy.convolution import Gaussian2DKernel, interpolate_replace_nans, convolve
# image path and format
path = r'C:\scripts\image'
files = glob.glob(path + "/*.edf")
for filename in files:
specname = os.path.split(filename) # get filename
specname = 'fix_%s' % specname[-1] # set a new name to save
pre, ext = os.path.splitext(specname) # get file type
ext = '.tiff' # change to .tiff (for dioptase) comment if .edf is needed
print(pre)
imdat = fabio.open(filename) # open image
# rescale & make it float
img = imdat.data[-1000:1000, 0:4439] * 1e0
# make < 0 values to NaN values
img[img <= 0] = np.nan
# smooth with a Gaussian kernel with x_stddev and y_stddev)
kernel = Gaussian2DKernel(x_stddev=3, y_stddev=0.1)
# create a "fixed" image with NaNs replaced by interpolated values
fixed_image = interpolate_replace_nans(img, kernel, convolve)
img = Image.fromarray(fixed_image)
# save in a folder named "/tiff/" that has to be created first
img.save(path + '/tiff/' + pre + ext)