예제 #1
0
    def getDiffractionImage(self,
                            colorMaps,
                            colorMapName,
                            maskedPixelInfo,
                            pixel1X=None,
                            pixel1Y=None,
                            pixel2X=None,
                            pixel2Y=None,
                            width=None,
                            height=None,
                            lowerBound=0,
                            upperBound=1,
                            logScale=0,
                            invert=None):

        # only create new image if it hasn't been made yet or if any
        # of the intensity bounds or if the log scale has changed,
        # or if some of the pixel masking parameters have changed
        if self.theImage == None or \
                self.lastLowerBoundDiffractionImage != lowerBound or \
                self.lastUpperBoundDiffractionImage != upperBound or \
                self.lastLogScaleDiffractionImage != logScale or \
                colorMapName != self.lastColorMapNameDiffractionImage or \
                invert != self.lastInvert or \
                maskedPixelInfo != self.lastMaskedPixelInfo:

            self.theImage = MakeDiffractionImage.getDiffractionImage(
                self.theDiffractionData.data,
                lowerBound=lowerBound,
                upperBound=upperBound,
                logScale=logScale,
                colorMaps=colorMaps,
                colorMapName=colorMapName,
                invert=invert,
                maskedPixelInfo=maskedPixelInfo)

            self.lastLowerBoundDiffractionImage = lowerBound
            self.lastUpperBoundDiffractionImage = upperBound
            self.lastLogScaleDiffractionImage = logScale
            self.lastColorMapNameDiffractionImage = colorMapName
            self.lastInvert = invert

            # only do a shallow copy because the weird widget in the object can't be copied
            self.lastMaskedPixelInfo = copy.copy(maskedPixelInfo)

        # by default, return entire image
        if pixel1X == None or pixel1Y == None or pixel2X == None or pixel2Y == None:
            if width == None or height == None:
                return self.theImage
            return self.theImage.resize((width, height), Image.BILINEAR)

        temp = self.theImage.crop(
            (min(int(pixel1X), int(pixel2X)), min(int(pixel1Y), int(pixel2Y)),
             max(int(pixel1X), int(pixel2X)), max(int(pixel1Y), int(pixel2Y))))
        if width != None and height != None:
            temp = temp.resize((width, height), Image.BILINEAR)
        return temp
    def getDiffractionImage(self,colorMaps,colorMapName,maskedPixelInfo,
            pixel1X=None,pixel1Y=None,pixel2X=None, pixel2Y=None, width = None, height = None, 
            lowerBound=0, upperBound=1, logScale = 0,invert=None):

        # only create new image if it hasn't been made yet or if any
        # of the intensity bounds or if the log scale has changed,
        # or if some of the pixel masking parameters have changed
        if self.theImage == None or \
                self.lastLowerBoundDiffractionImage != lowerBound or \
                self.lastUpperBoundDiffractionImage != upperBound or \
                self.lastLogScaleDiffractionImage != logScale or \
                colorMapName != self.lastColorMapNameDiffractionImage or \
                invert != self.lastInvert or \
                maskedPixelInfo != self.lastMaskedPixelInfo:

            self.theImage = MakeDiffractionImage.getDiffractionImage(self.theDiffractionData.data,
                    lowerBound=lowerBound,
                    upperBound=upperBound,
                    logScale = logScale,
                    colorMaps = colorMaps,
                    colorMapName=colorMapName,
                    invert=invert, 
                    maskedPixelInfo = maskedPixelInfo)

            self.lastLowerBoundDiffractionImage=lowerBound
            self.lastUpperBoundDiffractionImage=upperBound
            self.lastLogScaleDiffractionImage = logScale
            self.lastColorMapNameDiffractionImage = colorMapName
            self.lastInvert = invert
    
            # only do a shallow copy because the weird widget in the object can't be copied
            self.lastMaskedPixelInfo = copy.copy(maskedPixelInfo)

        # by default, return entire image
        if pixel1X==None or pixel1Y==None or pixel2X==None or pixel2Y==None:
            if width==None or height==None:
                return self.theImage
            return self.theImage.resize( (width, height), Image.BILINEAR )

        temp = self.theImage.crop((
                min(int(pixel1X),int(pixel2X)), 
                min(int(pixel1Y),int(pixel2Y)), 
                max(int(pixel1X),int(pixel2X)),
                max(int(pixel1Y),int(pixel2Y))))
        if width != None and height != None:
            temp = temp.resize( (width, height), Image.BILINEAR )
        return temp
    def saveDiffractionImage(self,filename,colorMaps,colorMapName,
            maskedPixelInfo,
            pixel1X=None,pixel1Y=None, pixel2X=None, pixel2Y=None, 
            lowerBound=0, upperBound=1, logScale = None, invert = None, 
            drawQLines=None,drawdQLines=None,QData=None,calibrationData=None,
            drawPeaks=None,peakList=None, qLinesColor = None, dQLinesColor = None,
            peakLinesColor = None):

        # save EDF data specially
        if getextension(filename) == ".edf":
            edf = EdfFile(filename)
            #You can write any relevant information in the dictionnary.
            edf.WriteImage({'Title':"Edf file converted by the Area Diffraction Machine"},
                    Numeric.transpose(self.theDiffractionData.data),
                    DataType= "SignedInteger",
                    Append=0)
            del edf # to force file close
            return

        # otherwise, try to save it using the PIL

        image = self.getDiffractionImage(colorMaps,colorMapName,
                maskedPixelInfo,
                None,None,None,None,None,None,
                lowerBound,upperBound,logScale,invert)
 

        if drawQLines or drawdQLines:
            if QData == None:
                raise Exception("Cannot save the diffraction data until a q list is given.")
            if calibrationData == None:
                raise Exception("Cannot save the diffraction data until the calibration Data is given.")

            if drawQLines:
                if qLinesColor == None:
                    raise Exception('Cannot add q lines to the saved image until the q line color is set.')
                for Q,dQ in QData.getAllQPairs():
                    MakeDiffractionImage.addConstantQLineDiffractionImage(image,Q,
                            calibrationData,qLinesColor)

            if drawdQLines:
                if dQLinesColor == None:
                    raise Exception('Cannot add delta q lines to the saved image until the delta q line color is set.')
                for Q,dQ in QData.getAllQPairs():
                    MakeDiffractionImage.addConstantQLineDiffractionImage(image,Q-dQ,
                            calibrationData,dQLinesColor)
                    MakeDiffractionImage.addConstantQLineDiffractionImage(image,Q+dQ,
                            calibrationData,dQLinesColor)

        if drawPeaks and peakList != None:
            if peakLinesColor == None:
                raise Exception("Cannot  add peaks to the saved iamge until the peak color is set.")

            MakeDiffractionImage.addPeaksDiffractionImage(image,peakList,peakLinesColor)

        # by default, return entire image
        if pixel1X != None and pixel1Y != None and pixel2X != None and pixel2Y !=None:
            image = image.crop((min(int(pixel1X),int(pixel2X)), 
                    min(int(pixel1Y),int(pixel2Y)), 
                    max(int(pixel1X),int(pixel2X)), 
                    max(int(pixel1Y),int(pixel2Y))))
        try:
            image.save(filename)
        except Exception,e:
            raise UserInputException("Cannot save image: %s has an unknown file extension" % filename )
예제 #4
0
    def saveDiffractionImage(self,
                             filename,
                             colorMaps,
                             colorMapName,
                             maskedPixelInfo,
                             pixel1X=None,
                             pixel1Y=None,
                             pixel2X=None,
                             pixel2Y=None,
                             lowerBound=0,
                             upperBound=1,
                             logScale=None,
                             invert=None,
                             drawQLines=None,
                             drawdQLines=None,
                             QData=None,
                             calibrationData=None,
                             drawPeaks=None,
                             peakList=None,
                             qLinesColor=None,
                             dQLinesColor=None,
                             peakLinesColor=None):

        # save EDF data specially
        if getextension(filename) == ".edf":
            edf = EdfFile(filename)
            #You can write any relevant information in the dictionnary.
            edf.WriteImage(
                {
                    'Title':
                    "Edf file converted by the Area Diffraction Machine"
                },
                Numeric.transpose(self.theDiffractionData.data),
                DataType="SignedInteger",
                Append=0)
            del edf  # to force file close
            return

        # otherwise, try to save it using the PIL

        image = self.getDiffractionImage(colorMaps, colorMapName,
                                         maskedPixelInfo, None, None, None,
                                         None, None, None, lowerBound,
                                         upperBound, logScale, invert)

        if drawQLines or drawdQLines:
            if QData == None:
                raise Exception(
                    "Cannot save the diffraction data until a q list is given."
                )
            if calibrationData == None:
                raise Exception(
                    "Cannot save the diffraction data until the calibration Data is given."
                )

            if drawQLines:
                if qLinesColor == None:
                    raise Exception(
                        'Cannot add q lines to the saved image until the q line color is set.'
                    )
                for Q, dQ in QData.getAllQPairs():
                    MakeDiffractionImage.addConstantQLineDiffractionImage(
                        image, Q, calibrationData, qLinesColor)

            if drawdQLines:
                if dQLinesColor == None:
                    raise Exception(
                        'Cannot add delta q lines to the saved image until the delta q line color is set.'
                    )
                for Q, dQ in QData.getAllQPairs():
                    MakeDiffractionImage.addConstantQLineDiffractionImage(
                        image, Q - dQ, calibrationData, dQLinesColor)
                    MakeDiffractionImage.addConstantQLineDiffractionImage(
                        image, Q + dQ, calibrationData, dQLinesColor)

        if drawPeaks and peakList != None:
            if peakLinesColor == None:
                raise Exception(
                    "Cannot  add peaks to the saved iamge until the peak color is set."
                )

            MakeDiffractionImage.addPeaksDiffractionImage(
                image, peakList, peakLinesColor)

        # by default, return entire image
        if pixel1X != None and pixel1Y != None and pixel2X != None and pixel2Y != None:
            image = image.crop(
                (min(int(pixel1X),
                     int(pixel2X)), min(int(pixel1Y), int(pixel2Y)),
                 max(int(pixel1X),
                     int(pixel2X)), max(int(pixel1Y), int(pixel2Y))))
        try:
            image.save(filename)
        except Exception, e:
            raise UserInputException(
                "Cannot save image: %s has an unknown file extension" %
                filename)