def test_same(self): """ check we can read bruker100 images""" ref = openimage(os.path.join(self.im_dir, REFIMAGE)) for line in TESTIMAGES.split("\n"): obt = openimage(os.path.join(self.im_dir, line.split()[0])) self.assert_( abs(ref.data - obt.data).max() == 0, "data are the same")
def testExecute(self): """ """ self.run() plugin = self.getPlugin() ################################################################################ # Compare XSDataResults ################################################################################ strExpectedOutput = self.readAndParseFile (self.getReferenceDataOutputFile()) EDVerbose.DEBUG("Checking obtained result...") xsDataResultReference = XSDataResultBioSaxsProcessOneFilev1_0.parseString(strExpectedOutput) xsDataResultObtained = plugin.getDataOutput() EDAssert.strAlmostEqual(xsDataResultReference.marshal(), xsDataResultObtained.marshal(), "XSDataResult output are the same", _strExcluded="bioSaxs") ################################################################################ # Compare dictionary ################################################################################ obtFile = xsDataResultObtained.integratedImage.path.value refFile = os.path.join(self.getTestsDataImagesHome(), "bioSaxsProcessIntegrated.edf") EDVerbose.DEBUG("fileRef=%s fileObt=%s" % (refFile, obtFile)) edfRef = openimage(refFile) edfObt = openimage(obtFile) headerRef = edfRef.header headerObt = edfObt.header keysRef = headerRef.keys() keysObt = headerObt.keys() keysRef.sort() keysObt.sort() # EDAssert.equal(keysRef, keysObt, _strComment="Same keys in the header dict") for key in keysRef: if not (key.startswith("History") or key in [ "Compression", "DDummy", "Dummy", "EDF_HeaderSize", "HeaderID", "Image", "filename", "RasterOrientation"]): EDAssert.strAlmostEqual(headerRef[key], headerObt[key], _strComment="header value %s are the same" % key, _strExcluded="bioSaxs") ################################################################################ # Compare images ################################################################################ outputData = edfRef.data referenceData = edfObt.data EDAssert.arraySimilar(numpy.maximum(outputData, 0), numpy.maximum(referenceData, 0) , _fScaledMaxDelta=0.05, _strComment="Images-data are the same") outputData = edfRef.next().data referenceData = edfObt.next().data EDAssert.arraySimilar(numpy.maximum(outputData, 0), numpy.maximum(referenceData, 0) , _fScaledMaxDelta=0.05, _strComment="Images-ESD are the same") ################################################################################ # Compare Ascii files ################################################################################ asciiObt = os.linesep.join([i.strip() for i in open(xsDataResultObtained.integratedCurve.path.value) if "Raster" not in i]) asciiRef = os.linesep.join([i.strip() for i in open(os.path.join(self.getTestsDataImagesHome(), "bioSaxsProcessIntegrated1_1.dat")) if "Raster" not in i]) EDAssert.strAlmostEqual(asciiRef, asciiObt, _strComment="3 column ascii files are the same", _fRelError=0.1, _strExcluded=os.environ["USER"]) EDVerbose.screen("Execution time for %s: %.3fs" % (plugin.getClassName(), plugin.getRunTime()))
def test_conversion(self): fname = UtilsTest.getimage("testcbf.cbf.bz2")[:-4] c = openimage(fname) assert "Cbf" in c.__class__.__name__, "This is a CbfImage" b = c.convert("bruker100") fname_out = os.path.join(UtilsTest.tempdir, "testcbf2bruker100.sfrm") b.write(fname_out) a = openimage(fname_out) self.assertTrue(numpy.allclose(a.data, c.data), msg="data are the same")
def test_open(self): """ check we can read images from Eiger""" e = openimage(self.fn2) self.assertEqual(e.shape, (99, 101)) self.assertEqual(e.nframes, 1, "nframes OK") self.assertEqual(e.bpp, 4, "nframes OK") e = openimage(self.fn3) self.assertEqual(e.shape, (99, 101)) self.assertEqual(e.nframes, 50, "nframes OK") self.assertEqual(e.bpp, 4, "nframes OK")
def test_open(self): """ check we can read images from Eiger""" e = openimage(self.fn2) self.assertEqual(e.dim1, 101, "dim1 OK") self.assertEqual(e.dim2, 99, "dim2 OK") self.assertEqual(e.nframes, 1, "nframes OK") self.assertEqual(e.bpp, 4, "nframes OK") e = openimage(self.fn3) self.assertEqual(e.dim1, 101, "dim1 OK") self.assertEqual(e.dim2, 99, "dim2 OK") self.assertEqual(e.nframes, 50, "nframes OK") self.assertEqual(e.bpp, 4, "nframes OK")
def test_write(self): fname = TESTIMAGES.split()[0] obt = openimage(os.path.join(self.im_dir, fname)) name = os.path.basename(fname) obj = Bruker100Image(data=obt.data, header=obt.header) obj.write(os.path.join(UtilsTest.tempdir, name)) other = openimage(os.path.join(UtilsTest.tempdir, name)) self.assertEqual(abs(obt.data - other.data).max(), 0, "data are the same") for key in obt.header: self.assertTrue(key in other.header, "Key %s is in header" % key) self.assertEqual(obt.header[key], other.header[key], "value are the same for key %s" % key) os.unlink(os.path.join(UtilsTest.tempdir, name))
def __init__(self, darkfile=None, darkoffset=None, floodfile=None, floodmultiplier=None, splinefile=None, border=None, powfac=1.0, overflow=65534, detrend=None, monitorcol=None, monitorval=None, maskfilename=None): self.overflow = overflow self.darkfile = darkfile self.darkoffset = darkoffset self.floodfile = floodfile self.splinefile = splinefile self.distnorm = None self.border = border self.floodmultiplier = None # self.darkimage = None self.floodimage = None self.flmult = None self.powfac = powfac self.detrend = detrend self.monitorcol = monitorcol self.monitorval = monitorval if maskfilename is not None: self.mask = 1 - openimage(maskfilename).data else: self.mask = None
def test_read(self): """ check we can read pnm images""" obj = openimage(self.fn) self.assertEqual(obj.bytecode, numpy.uint16, msg="bytecode is OK") self.assertEqual(obj.shape, (11, 9)) self.assertTrue(numpy.allclose(obj.data, self.ary), "data")
def integrate_peak(number, colfile, bbs=10): i = list(colfile.spot3d_id.astype(int)).index(number) om = colfile.omega[i] print("omega", i, om) bb = [ colfile.Min_s[i], colfile.Max_s[i], colfile.Min_f[i], colfile.Max_f[i], colfile.Min_o[i], colfile.Max_o[i] ] first_image = int(om) * 147 last_image = first_image + 146 print(bb) s = colfile.s_raw[i] f = colfile.f_raw[i] intensity = [] for j in range(first_image, last_image + 1): print(j, end=' ') sys.stdout.flush() im = openimage(DATA + "%04d.edf" % (j)).data intensity.append(im[s - bbs:s + bbs, f - bbs:f + bbs].mean()) if 0 and j == 54: imshow(im, vmax=1000) figure() imshow(im[s - 20:s + 20, f - 20:f + 20], vmax=1000) show() plot(intensity) show() return om, intensity
def testg1(): gauss = np.exp(-(x * x + x.T * x.T) / 10) dims = (1526, 1024) ret = centre_psf(gauss, dims) from fabio.openimage import openimage im = openimage("c_Al_s1_000__quantix_0037.edf") d = im.data figure(1) show() imshow(d) colorbar() show() ret = centre_psf(gauss, d.shape) rl = RichardsonLucy(gauss, d.shape) c = rl.convolve(d) def sm(x): return np.ravel(x).sum() print(sm(c), sm(d), sm(gauss)) figure(2) imshow(c) colorbar() input("press a key")
def testcase(self): """ check we can read EDF image with openimage""" obj = openimage(self.fname) obj2 = edfimage() obj2.read(self.fname) self.assertEqual(obj.data[10, 10], obj2.data[10, 10]) self.assertEqual(type(obj), type(obj2)) self.assertEqual(abs(obj.data.astype(int) - obj2.data.astype(int)).sum(), 0)
def test_read(self): """ check we can read pnm images""" obj = openimage(self.fn) self.assertEqual(obj.bytecode, numpy.uint16, msg="bytecode is OK") self.assertEqual(9, obj.dim1, "dim1") self.assertEqual(11, obj.dim2, "dim2") self.assert_(numpy.allclose(obj.data, self.ary), "data")
def testExecute(self): """ """ self.run() plugin = self.getPlugin() ################################################################################ # Compare XSDataResults ################################################################################ strExpectedOutput = self.readAndParseFile (self.getReferenceDataOutputFile()) EDVerbose.DEBUG("Checking obtained result...") xsDataResultReference = XSDataResultBioSaxsNormalizev1_0.parseString(strExpectedOutput) xsDataResultObtained = plugin.getDataOutput() EDAssert.strAlmostEqual(xsDataResultReference.marshal(), xsDataResultObtained.marshal(), "XSDataResult output are the same", _strExcluded="bioSaxs") ################################################################################ # Compare dictionary ################################################################################ edfRef = openimage(xsDataResultObtained.normalizedImage.getPath().value) edfObt = openimage(os.path.join(self.getTestsDataImagesHome(), "bioSaxsCorrected.edf")) headerRef = edfRef.header headerObt = edfObt.header keysRef = headerRef.keys() keysObt = headerObt.keys() keysRef.sort() keysObt.sort() # EDAssert.equal(keysRef, keysObt, _strComment="Same keys in the header dict") for key in keysRef: if not (key.startswith("History") or key in [ "Compression", "DDummy", "Dummy", "EDF_HeaderSize", "HeaderID", "Image", "filename", "RasterOrientation", "time_of_day"]): EDAssert.strAlmostEqual(headerRef[key], headerObt[key], _strComment="header value %s are the same" % key, _strExcluded="bioSaxs") ################################################################################ # Compare images ################################################################################ outputData = edfRef.data referenceData = edfObt.data EDAssert.arraySimilar(numpy.maximum(outputData, 0), numpy.maximum(referenceData, 0) , _fAbsMaxDelta=0.1, _fScaledMaxDelta=0.05, _strComment="Images-data are the same") outputData = edfRef.next().data referenceData = edfObt.next().data EDAssert.arraySimilar(numpy.maximum(outputData, 0), numpy.maximum(referenceData, 0) , _fAbsMaxDelta=0.1, _fScaledMaxDelta=0.05, _strComment="Images-ESD are the same")
def checkFile(self, filename): """ check we can read it""" obj = openimage(filename) obj2 = marccdimage() obj2.read(filename) self.assertEqual(obj.data[10, 10], obj2.data[10, 10]) self.assertEqual(type(obj), type(obj2)) self.assertEqual(abs(obj.data.astype(int) - obj2.data.astype(int)).sum(), 0)
def test_write(self): """ check we can write numpy images""" ref = NumpyImage(data=self.ary) ref.save(self.fn2) with openimage(self.fn2) as obj: self.assertEqual(obj.bytecode, numpy.uint16, msg="bytecode is OK") self.assertEqual(obj.shape, (11, 9)) self.assertTrue(numpy.allclose(obj.data, self.ary), "data")
def rdbkg(self, bkgfile=None): if bkgfile == None: bkgfile = tkFileDialog.askopenfilename(initialdir=os.getcwd()) self.bkgfile = bkgfile bkgobj = openimage(bkgfile) self.bkg = bkgobj.data.astype(np.int) self.data = self.data.astype(np.int) - self.bkg print "Got your background from", bkgfile, self.bkg.shape
def test_write(self): """ check we can write numpy images""" ref = NumpyImage(data=self.ary) ref.save(self.fn2) obj = openimage(self.fn2) self.assertEqual(obj.bytecode, numpy.uint16, msg="bytecode is OK") self.assertEqual(9, obj.dim1, "dim1") self.assertEqual(11, obj.dim2, "dim2") self.assert_(numpy.allclose(obj.data, self.ary), "data")
def process(self, _edObject=None): """ This is the main method of the plugin, it does the job, not the EDNA name conversions which were done in the preprocess. """ EDPluginExec.process(self) EDVerbose.DEBUG("EDPluginPyarchThumbnailv10.process") if self.inputFilename: # Read the image using FABIO fabioImage = openimage(self.inputFilename) # Convert an FabioImage to a PIL image npaImageRaw = fabioImage.data iMinLevel = 2 iMaxLevel = 1000 npaImageRaw = numpy.minimum(npaImageRaw, iMaxLevel * numpy.ones_like(npaImageRaw)) npaImageRaw = numpy.maximum(npaImageRaw, iMinLevel * numpy.ones_like(npaImageRaw)) npaImage = scipy.ndimage.morphology.grey_dilation(npaImageRaw, size=(5, 5)) npaImageThumb = scipy.ndimage.morphology.grey_dilation(npaImageRaw, size=(10, 10)) dtype = str(npaImage.dtype) if dtype == "uint8": NPAImageFloat = npaImage.astype("float") / 255.0 NPAImageFloatThumb = npaImageThumb.astype("float") / 255.0 elif dtype == "uint16": NPAImageFloat = npaImage.astype("float") / 65535.0 NPAImageFloatThumb = npaImageThumb.astype("float") / 65535.0 else: #for float or whatever vmin = npaImage.min() vmax = npaImage.max() NPAImageFloat = (npaImage.astype(float) - vmin) / (vmax - vmin) vminThumb = npaImageThumb.min() vmaxThumb = npaImageThumb.max() NPAImageFloatThumb = (npaImageThumb.astype(float) - vminThumb) / (vmax - vminThumb) NPAImageInt = (255.0 * (NPAImageFloat ** 0.3)).astype("uint8") NPAImageIntThumb = (255.0 * (NPAImageFloatThumb ** 0.3)).astype("uint8") pilImage = Image.fromarray(NPAImageInt, 'L') pilImageThumb = Image.fromarray(NPAImageIntThumb, 'L') # For ISPyB we use for the moment only 1024x1024 and 256x256 pilImage.thumbnail((1024, 1024), Image.ANTIALIAS) pilImageThumb.thumbnail((256, 256), Image.ANTIALIAS) pilImage = ImageOps.invert(pilImage) pilImageThumb = ImageOps.invert(pilImageThumb) self.synchronizeOn() if 1024 * 1024 > ImageFile.MAXBLOCK: ImageFile.MAXBLOCK = 1024 * 1024 pilImage.save(self.strOutputPath, "JPEG", quality=50, optimize=True) pilImageThumb.save(self.strOutputPathThumb, "JPEG", quality=85, optimize=True) self.synchronizeOff()
def test_write(self): pnmfile = os.path.join(UtilsTest.tempdir, "pnmfile.pnm") shape = (9, 11) size = shape[0] * shape[1] data = numpy.random.randint(0, 65000, size=size).reshape(shape) pnmimage(data=data).save(pnmfile) pnm = openimage(pnmfile) self.assert_(numpy.allclose(data, pnm.data), "data are the same") os.unlink(pnmfile)
def checkFile(self, filename): """ check we can read Fit2D mask with openimage""" obj = openimage(filename) obj2 = fit2dmaskimage() obj2.read(filename) self.assertEqual(obj.data[10, 10], obj2.data[10, 10]) self.assertEqual(type(obj), type(obj2)) self.assertEqual(abs(obj.data.astype(int) - obj2.data.astype(int)).sum(), 0) self.assertEqual(abs(obj.data.astype(int) - obj2.data.astype(int)).sum(), 0)
def main(): from ImageD11.rsv import readvol from fabio.openimage import openimage import numpy as np import sys #v = readvol( sys.argv[1] ) v = openimage( sys.argv[1] ).data.astype( np.float32 ) root = plotvol(v) root.mainloop()
def test_read_openimage(self): from fabio.openimage import openimage obj = openimage(self.image) if obj.data.astype(int).tostring() != self.imdata.astype(int).tostring(): logger.info("%s %s" % (type(self.imdata), self.imdata.dtype)) logger.info("%s %s" % (type(obj.data), obj.data.dtype)) logger.info("%s %s" % (obj.data - self.imdata)) self.assertEqual(obj.data.astype(int).tostring(), self.imdata.astype(int).tostring())
def add_file(self, filename): """ Include another file """ try: data_object = openimage(filename) except IOError: print(filename, "not found") return self.add_image(data_object.data)
def __openimage(self, filename=None): if filename == None: filename = self.filename try: if self.noread: if not os.path.exists(filename): raise IOError('No such file or directory %s' % filename) else: self.img = openimage.openimage(filename) except IOError: raise
def readimage(self, image): from ImageD11 import transform from fabio import openimage self.imageobj = openimage.openimage(image) # map from 2048x2048 to 1024x1024 d = self.imageobj.data.astype(numpy.float32) mi = d.mean() - d.std() * 2 mx = d.mean() * d.std() * 2 shape = self.imageobj.data.shape d = numpy.reshape(numpy.clip(self.imageobj.data, mi, mx), shape) # makes a clipped copy d = (255. * (d - mi) / (mx - mi)) # scale intensity print d.min(), d.max(), d.mean() self.image = numpy.zeros((1024, 1024), numpy.uint8) if d.shape == (2048, 2048): # rebin 2x2 im = (d[::2, ::2] + d[::2, 1::2] + d[1::2, ::2] + d[1::2, 1::2]) / 4 self.image = (255 - im).astype(numpy.uint8).tostring() self.imageWidth = 1024 self.imageHeight = 1024 # make a 2D array of x,y p = [] pk = [] step = 64 r = [[0, 0], [0, step], [step, step], [step, 0]] for i in range(0, 1024, step): for j in range(0, 1024, step): # i,j 1024x1024 texture coords # x,y spatially corrected for v in r: pk.append([i + v[0], j + v[1]]) x, y = self.corrector.correct((i + v[0]) * 2, (j + v[1]) * 2) # corrected p.append([x, y]) p = numpy.array(p).T pk = numpy.array(pk).T omega = float(self.imageobj.header['Omega']) self.pars['distance'] = float(self.pars['distance']) * 1000 tth, eta = transform.compute_tth_eta(p, **self.pars) gve = transform.compute_g_vectors(tth, eta, omega * self.pars['omegasign'], self.pars['wavelength']) self.pts = [] print "Setting up image mapping", p.shape, gve.shape for i in range(pk.shape[1]): self.pts.append([ pk[1, i] / 1024., pk[0, i] / 1024., gve[0, i], gve[1, i], gve[2, i] ]) #for p in self.pts: # print p self.setupTexture()
def testg5(): from fabio.openimage import openimage im = openimage("c_Al_s1_000__quantix_0037.edf") gaus = g((50, 60), 4.1) + g((50, 60), 10.1) * 0.1 from matplotlib.pylab import imshow, show imshow(gaus) show() rl = RichardsonLucy(gaus, im.data.shape) stuff = rl.deblur(im.data.copy()) im.data = stuff im.write("deblurred.edf", force_type=np.float32)
def dodeconv(filename_in, filename_bg, extra_bg, filename_out, pointspread_image, niter=20): from fabio.openimage import openimage im_in = openimage(filename_in) im_bg = openimage(filename_bg) # do background subtraction im_in.data = im_in.data.astype(REAL) - im_bg.data - float(extra_bg) im_in.data = np.where(im_in.data > 0, im_in.data, 0) # conv does the image convolution conv = convolver(pointspread_image, im_in.data.shape) # rl does the deconvolution rl = RichardsonLucy(conv) # im_in.data = rl.deblur(im_in.data, niter=niter).astype(np.uint16) im_in.write(filename_out)
def test_read(self): """ check we can read pnm images""" vals = self.results.split() name = vals[0] dim1, dim2 = [int(x) for x in vals[1:3]] shape = dim2, dim1 mini, maxi, mean, stddev = [float(x) for x in vals[3:]] obj = openimage(self.fn[name]) self.assertAlmostEqual(mini, obj.getmin(), 4, "getmin") self.assertAlmostEqual(maxi, obj.getmax(), 4, "getmax") self.assertAlmostEqual(mean, obj.getmean(), 4, "getmean") self.assertAlmostEqual(stddev, obj.getstddev(), 4, "getstddev") self.assertEqual(shape, obj.shape)
def test_read(self): """ check we can read kcd images""" vals = self.results.split() name = vals[0] dim1, dim2 = [int(x) for x in vals[1:3]] mini, maxi, mean, stddev = [float(x) for x in vals[3:]] obj = openimage(self.fn[self.kcdfilename]) self.assertAlmostEqual(mini, obj.getmin(), 4, "getmin") self.assertAlmostEqual(maxi, obj.getmax(), 4, "getmax") self.assertAlmostEqual(mean, obj.getmean(), 4, "getmean") self.assertAlmostEqual(stddev, obj.getstddev(), 4, "getstddev") self.assertEqual(dim1, obj.dim1, "dim1") self.assertEqual(dim2, obj.dim2, "dim2")
def readdark(self, darkfile): """ read the dark""" try: self.darkdata = openimage(darkfile) self.darkfile = darkfile self.darkimage = self.darkdata.data.astype(numpy.float32) if self.powfac != 1.0: print "apply 0.96 to dark" self.darkimage = numpy.power(self.darkimage, 0.96) except: print "No dark file" self.darkdata = None self.darkimage = None self.darkfile = None
def test_next_frames(self): """ check the legacy next API""" h5 = openimage(self.fn3) frame_nb = 1 frame_id = 0 frame = h5 while True: try: frame = frame.next() frame_id = frame.file_index frame_nb += 1 except IndexError: break self.assertEqual(frame_nb, 50) self.assertEqual(frame_id, 49)
def test_multidim(self): for shape in (10, ), (10, 15), (10, 15, 20), (10, 15, 20, 25): ary = numpy.random.random(shape).astype("float32") numpy.save(self.fn, ary) obj = openimage(self.fn) self.assertEqual(obj.bytecode, numpy.float32, msg="bytecode is OK") self.assertEqual(shape[-1], obj.dim1, "dim1") dim2 = 1 if len(shape) == 1 else shape[-2] self.assertEqual(dim2, obj.dim2, "dim2") nframes = 1 if len(shape) > 2: for i in shape[:-2]: nframes *= i self.assertEqual(nframes, obj.nframes, "nframes") if os.path.exists(self.fn): os.unlink(self.fn)
def test_multidim(self): for shape in (10,), (10, 15), (10, 15, 20), (10, 15, 20, 25): ary = numpy.random.random(shape).astype("float32") numpy.save(self.fn, ary) with openimage(self.fn) as obj: self.assertEqual(obj.bytecode, numpy.float32, msg="bytecode is OK") self.assertEqual(shape[-1], obj.dim1, "dim1") dim2 = 1 if len(shape) == 1 else shape[-2] self.assertEqual(dim2, obj.dim2, "dim2") nframes = 1 if len(shape) > 2: for i in shape[:-2]: nframes *= i #print(shape,nframes, obj.nframes) self.assertEqual(nframes, obj.nframes, "nframes") if os.path.exists(self.fn): os.unlink(self.fn)
def preProcess(self, _edObject=None): EDPluginControl.preProcess(self) EDVerbose.DEBUG("EDPluginExecMeasureOffsetv2_0.preProcess") sdi = self.getDataInput() crop = sdi.getCropBorders() if len(crop) == 2: self.tCrop = (crop[0].getValue(), crop[1].getValue()) elif len(crop) == 1: self.tCrop = (crop[0].getValue(), crop[0].getValue()) # if len(sdi.getImage()) == 2: for i in sdi.getImage(): array = openimage(i.getPath().getValue()).data shape = array.shape if (self.tCrop != [0, 0]) and (shape[0] > self.tCrop[0]) and (shape[1] > self.tCrop[1]): array = array[self.tCrop[0]:-self.tCrop[0], self.tCrop[1]:-self.tCrop[1] ] EDVerbose.DEBUG("After Crop, images have shape : (%s,%s) " % (array.shape)) self.xsdImages.append(EDUtilsArray.arrayToXSData(array)) elif len(sdi.getArray()) == 2: if (self.tCrop == [0, 0]) : self.xsdImages = sdi.getArray() else: for xsdArray in sdi.getArray(): array = EDUtilsArray.xsDataToArray(xsdArray) shape = array.shape if (shape[0] > self.tCrop[0]) and (shape[1] > self.tCrop[1]): array = array[self.tCrop[0]:-self.tCrop[0], self.tCrop[1]:-self.tCrop[1] ] EDVerbose.DEBUG("After Crop, images have shape : (%s,%s) " % (array.shape)) self.xsdImages.append(EDUtilsArray.arrayToXSData(array)) else: strError = "EDPluginExecMeasureOffsetv2_0.preProcess: You should either provide two images or two arrays, but I got: %s" % sdi.marshal() EDVerbose.ERROR(strError) self.setFailure() raise RuntimeError(strError) EDVerbose.DEBUG("EDPluginExecMeasureOffsetv2_0.xsdImages len=%i %s" % (len(self.xsdImages), self.xsdImages)) EDAssert.equal(self.xsdImages[0].getShape() , self.xsdImages[1].getShape(), "Images have the same size") self.xsdIdx = sdi.getIndex() if len(self.xsdIdx) < len(self.xsdImages): self.xsdIdx = [XSDataInteger(i) for i in range(len(self.xsdImages))]
def test_open(self): """ check we can read images from Eiger""" e = openimage(self.fn3) self.assertEqual(e.shape, (99, 101)) self.assertEqual(e.nframes, 50, "nframe: got %s!=50" % e.nframes) self.assertEqual(e.bpp, 4, "bpp OK")
def process(self, _edObject=None): EDPluginExec.process(self) EDVerbose.DEBUG("EDPluginExecMatrixReadv1_0.process") self.matOut = openimage(self.inputFile).data
def readArrayFromFile(self, _filename): """ Populate self.inputArray for a given filename and extracts as well self.fPixelSize self.fDistance self.fRadOffset self.wavelength ... """ image = openimage(_filename) self.inputArray = image.data self.dHeader = image.header if "Offset_1" in self.dHeader: offset, unit = self.dHeader["Offset_1"].split(None, 1) if unit.lower() != "pixel": EDVerbose.WARNING("I got a strange unit for offset: %s, I expected pixel." % self.dHeader["Offset_1"]) try: self.fRadOffset = float(offset) except ValueError: strErrorMessage = "EDPluginExportAsciiPowderv1_0.process: Unable to recognize this offset and convert it to float: %s" % self.dHeader["Offset_1"] EDVerbose.ERROR(strErrorMessage) self.addErrorMessage(strErrorMessage) self.setFailure() raise ValueError, strErrorMessage if "Offset_2" in self.dHeader: offset, unit = self.dHeader["Offset_2"].split(None, 1) if unit.lower() != "pixel": EDVerbose.WARNING("I got a strange unit for offset: %s, I expected pixel." % self.dHeader["Offset_2"]) try: self.fAzimOffset = float(offset) except ValueError: strErrorMessage = "EDPluginExportAsciiPowderv1_0.process: Unable to recognize this offset and convert it to float: %s" % self.dHeader["Offset_2"] EDVerbose.ERROR(strErrorMessage) self.addErrorMessage(strErrorMessage) self.setFailure() raise ValueError, strErrorMessage if "PSize_1" in self.dHeader: psize, unit = self.dHeader["PSize_1"].split(None, 1) try: fPixelSize = float(psize) except ValueError: strErrorMessage = "EDPluginExportAsciiPowderv1_0.process: Unable to recognize the radial pixel size and convert it to float: %s" % self.dHeader["PSize_1"] EDVerbose.ERROR(strErrorMessage) self.addErrorMessage(strErrorMessage) self.setFailure() else: self.fPixelSize = EDUtilsUnit.getValueLength(fPixelSize, unit) if "PSize_2" in self.dHeader: psize, unit = self.dHeader["PSize_2"].split(None, 1) try: fPixelSize = float(psize) except ValueError: strErrorMessage = "EDPluginExportAsciiPowderv1_0.process: Unable to recognize the azimuthal pixel size and convert it to float: %s" % self.dHeader["PSize_2"] EDVerbose.ERROR(strErrorMessage) self.addErrorMessage(strErrorMessage) self.setFailure() else: self.fAzimSize = EDUtilsUnit.getValueAngle(fPixelSize, unit) if ("SampleDistance" in self.dHeader): distance, unit = self.dHeader["SampleDistance"].split(None, 1) try: fDistance = float(distance) except ValueError: strErrorMessage = "EDPluginExportAsciiPowderv1_0.process: Unable to recognize the Sample Distance is and convert it to float: %s" % self.dHeader["SampleDistance"] EDVerbose.ERROR(strErrorMessage) self.addErrorMessage(strErrorMessage) self.setFailure() raise ValueError, strErrorMessage self.fDistance = EDUtilsUnit.getValueLength(fDistance, unit) if ("WaveLength" in self.dHeader): wavelenth, unit = self.dHeader["WaveLength"].split(None, 1) try: fWavelength = float(wavelenth) except ValueError: strErrorMessage = "EDPluginExportAsciiPowderv1_0.process: Unable to recognize the wavelength is and convert it to float: %s" % self.dHeader["WaveLength"] EDVerbose.ERROR(strErrorMessage) self.addErrorMessage(strErrorMessage) self.setFailure() raise ValueError, strErrorMessage self.fWavelength = EDUtilsUnit.getValueLength(fWavelength, unit) if ("Dummy" in self.dHeader): self.fDummy = float(self.dHeader.get("Dummy", 0)) if ("DDummy" in self.dHeader): self.fDeltaDummy = float(self.dHeader.get("DDummy", 0.1))
def process(self, _edObject=None): """ This is the main method of the plugin, it does the job, not the EDNA name conversions which were done in the preprocess. """ EDPluginExec.process(self) EDVerbose.DEBUG("EDPluginExecThumbnailv10.process") # try: # except Exception: # edfImage = EDF(self.inputFilename) # self.npaImage = edfImage.GetData(0) # Read the image using FABIO isRGB = False pilOutputImage = None if self.inputFilename is not None: try: fabioImage = openimage(self.inputFilename) self.npaImage = fabioImage.data except Exception: pilInputImage = Image.open(self.inputFilename) x, y = pilInputImage.size ImageFile.MAXBLOCK = x * y if pilInputImage.mode == "1": self.npaImage = numpy.asarray(pilInputImage).astype("uint8") isRGB = False elif pilInputImage.mode == "F": self.npaImage = numpy.asarray(pilInputImage) isRGB = False elif pilInputImage.mode == "L": self.npaImage = numpy.asarray(pilInputImage) isRGB = False elif pilInputImage.mode == "P": self.npaImage = numpy.asarray(pilInputImage.convert("RGB")) isRGB = True elif pilInputImage.mode == "RGB": self.npaImage = numpy.asarray(pilInputImage) isRGB = True elif pilInputImage.mode == "CMJK": self.npaImage = numpy.asarray(pilInputImage.convert("RGB")) isRGB = True dtype = self.npaImage.dtype NPAImageFloat = None # crop border if len(self.cropBorders) > 0: if len(self.cropBorders) == 1: crop0 = self.cropBorders[0] crop1 = self.cropBorders[0] else: crop0 = self.cropBorders[0] crop1 = self.cropBorders[1] if isRGB: self.npaImage = self.npaImage[crop0:-crop0, crop1:crop1, :] else: self.npaImage = self.npaImage[crop0:-crop0, crop1:crop1] # Set maxima and minima if (self.minLevelUnit is not None) or (self.maxLevelUnit is not None): sortedArray = self.npaImage.flatten() sortedArray.sort() if self.minLevel is not None: self.normalize = True if isRGB: EDVerbose.warning("It is not allowed to set Min with RGB data") else: if self.minLevelUnit in ["%", "percent"]: self.minLevel = sortedArray[int(round(float(self.minLevel) * sortedArray.size / 100.0))] if isinstance(self.npaImage[0, 0], int): self.npaImage = numpy.maximum(self.npaImage, int(self.minLevel) * numpy.ones_like(self.npaImage)) else: self.npaImage = numpy.maximum(self.npaImage, self.minLevel * numpy.ones_like(self.npaImage)) if self.maxLevel is not None: self.normalize = True if isRGB: EDVerbose.warning("It is not allowed to set Max with RGB data") else: if self.maxLevelUnit in ["%", "percent"]: self.maxLevel = sortedArray[int(round(float(self.maxLevel) * sortedArray.size / 100.0))] if isinstance(self.npaImage[0, 0], int): self.npaImage = numpy.minimum(self.npaImage, int(self.maxLevel) * numpy.ones_like(self.npaImage)) else: self.npaImage = numpy.minimum(self.npaImage, self.maxLevel * numpy.ones_like(self.npaImage)) # Scipy filters come here: if len(self.gaussianBlur) > 0: if len(self.gaussianBlur) == 1 : kernel = (self.gaussianBlur[0], self.gaussianBlur[0]) else: kernel = (self.gaussianBlur[0], self.gaussianBlur[1]) if isRGB: kernel = (kernel[0], kernel[1], 0) self.npaImage = scipy.ndimage.gaussian_filter(self.npaImage, kernel) if len(self.dilatation) > 0: if len(self.dilatation) == 1: kernel = (self.dilatation[0], self.dilatation[0]) else: kernel = (self.dilatation[0], self.dilatation[1]) if isRGB: kernel = (kernel[0], kernel[1], 0) self.npaImage = scipy.ndimage.morphology.grey_dilation(self.npaImage, kernel) #Normalization ; equalization if (self.normalize is True) or (self.equalize is True): if isRGB is True: self.npaImage = numpy.asarray(ImageOps.equalize(Image.fromarray(self.npaImage))) else: EDVerbose.DEBUG("EDPluginExecThumbnailv10: Normalization") vmin = self.npaImage.min() vmax = self.npaImage.max() NPAImageFloat = (self.npaImage.astype(numpy.float32) - float(vmin)) / (float(vmax) - float(vmin)) if (self.equalize == True): nbr_bins = 64 NPAImageFloatFlat = NPAImageFloat.flatten() imhist, bins = numpy.histogram(NPAImageFloatFlat, nbr_bins, normed=True) #get image histogram cdf = imhist.cumsum() #cumulative distribution function ncdf = cdf / cdf[-1] #normalized cumulative distribution function # print ncdf NPAImageFloat2Flat = numpy.interp(NPAImageFloatFlat, bins, [0] + ncdf.tolist()) NPAImageFloat = NPAImageFloat2Flat.reshape(NPAImageFloat.shape) EDVerbose.DEBUG("Equalize: min= %f, max= %f" % (NPAImageFloat.min(), NPAImageFloat.max())) #Gamma and logarithm scale if ((self.log is True) or (self.gamma != 1)) and (NPAImageFloat is None): # then we need the array in float if dtype == numpy.uint8: NPAImageFloat = self.npaImage.astype(numpy.float32) / 255.0 elif dtype == numpy.uint16: NPAImageFloat = self.npaImage.astype(numpy.float32) / 65535.0 else: NPAImageFloat = self.npaImage.astype(numpy.float32) if self.log is True: NPAImageFloat = numpy.log(1 - NPAImageFloat.min() + NPAImageFloat) vmin = NPAImageFloat.min() vmax = NPAImageFloat.max() NPAImageFloat = (NPAImageFloat - vmin) / (vmax - vmin) if self.gamma != 1: if dtype not in [numpy.uint8, numpy.uint16]: vmin = NPAImageFloat.min() vmax = NPAImageFloat.max() NPAImageFloat = (NPAImageFloat - vmin) / (vmax - vmin) NPAImageInt = (255.0 * (NPAImageFloat ** self.gamma)).astype("uint8") else: #if (self.gamma == 1): if NPAImageFloat is None: if dtype == numpy.uint8: NPAImageInt = self.npaImage elif dtype == numpy.uint16: NPAImageInt = (self.npaImage / 256).astype(numpy.uint8) else: #for float or a signed integer vmin = self.npaImage.min() vmax = self.npaImage.max() NPAImageInt = ((self.npaImage.astype(numpy.float32) - vmin) / (vmax - vmin) * 255.0).astype(numpy.uint8) else: vmin = NPAImageFloat.min() vmax = NPAImageFloat.max() EDVerbose.DEBUG("EDPluginExecThumbnailv10: NPAImageFloat => NPAImageInt min=%s max =%s" % (vmin, vmax)) NPAImageInt = ((NPAImageFloat - vmin) * 255.0 / (vmax - vmin)).astype(numpy.uint8) #COnversion back to PIL mode if isRGB is True: pilOutputImage = Image.fromarray(NPAImageInt, 'RGB') else: pilOutputImage = Image.fromarray(NPAImageInt, 'L') if (self.autocontrast is not None): pilOutputImage = ImageOps.autocontrast(pilOutputImage, self.autocontrast) if (self.width is not None) or (self.height is not None): if (self.width > 0) and (self.height > 0): if self.keepRatio is True: # PIL takes care of the ratio pilOutputImage.thumbnail((self.width, self.height), Image.ANTIALIAS) else: pilOutputImage = pilOutputImage.resize((self.width, self.height), Image.ANTIALIAS) else: if self.width is None: pilOutputImage.thumbnail((self.height, self.height), Image.ANTIALIAS) elif self.height is None: pilOutputImage.thumbnail((self.width, self.width), Image.ANTIALIAS) if self.invert == True: pilOutputImage = ImageOps.invert(pilOutputImage) if self.colorize == True: pilOutputImage.putpalette(EDPluginExecThumbnailv10.getPalette()) pilOutputImage = pilOutputImage.convert("RGB") self.synchronizeOn() if self.format == "jpg": self.width, self.height = pilOutputImage.size if self.width * self.height > ImageFile.MAXBLOCK: ImageFile.MAXBLOCK = self.width * self.height try: pilOutputImage.save(self.output, "JPEG", quality=85, optimize=True) except TypeError: pilOutputImage.save(self.output) else: pilOutputImage.save(self.output) self.synchronizeOff()
def test_same(self): """ check we can read bruker100 images""" ref = openimage(os.path.join(self.im_dir, REFIMAGE)) for line in TESTIMAGES.split("\n"): obt = openimage(os.path.join(self.im_dir, line.split()[0])) self.assertTrue(abs(ref.data - obt.data).max() == 0, "data are the same")