Exemple #1
0
def jp2CreationInfo(jp2ImageOut):
    # Check if output image was created (Aware exit status doesn't really help here),
    # and print info to screen 
    if os.path.isfile(jp2ImageOut)==False:
        jp2Created=False
        shared.printWarning(jp2ImageOut + " not created! \n")
    else:
        shared.printInfo("created output image " + jp2ImageOut + "\n")
        jp2Created=True
    
    return jp2Created 
Exemple #2
0
def convertOneImageToJP2(imageIn,imageOut,awareOptionsString,exifToolApp,j2kDriverApp,extractMetadataFlag):

    # Convert one image to JP2. Arguments:
    #
    # imageIn: name of input image
    # imageOut: name of output JP2 image
    # awareOptionsString: text string with Aware options
    # exifToolApp: path to ExifTool executable
    # j2kDriverApp: path to Aware executable
    # extractMetadataFlag: True if metadata extraction is needed, False otherwise

    # Initialise exit status flag + output strings for ExifTool /Aware
    # (in case extractMetadataFlag equals 0)
    exifExitStatus=0
    awareExitStatus=0
    exifStdOut=""
    exifStdErr=""
    awareStdOut=""
    awareStdErr=""

    # Does input image exist?
    imageInExists=os.path.isfile(imageIn)
       
    #if imageInExists==False:
   
    # If output image already exists, delete it! Why this? -->
    # 1. If the conversion proceeds as planned, it will be overwritten anyway, BUT ...
    # 2. If something goes wrong and the image cannot be created, the absence of 
    #    the output image may be the only way to find out about this afterwards
    #    (because we cannot rely on Aware's exit status for this, see below)
    shared.removeFile(imageOut)
   
    # Absolute path to output image (location also used for temporary file)
    pathOut=os.path.abspath(os.path.split(imageOut)[0])

    # Metadata extraction (optional)

    # Initialise Aware command line option for metadata embedding
    aMetadata=""

    if extractMetadataFlag==True:
        # Generate name for (temporary) xmp file
        fileXMP=shared.constructFileName(imageIn,pathOut,"xmp","")
        
        # If previous run of aWrapper was terminated by the user, a file with this
        # name could still be there (and this would raise an error in ExifTool)
        shared.removeFile(fileXMP)

        # Extract metadata from input image and store result in XMP format
        exifSysString=shared.quoteString(exifToolApp) + " " + imageIn + " -o " + fileXMP
        shared.printInfo("running ExifTool")
        
        exifExitStatus,exifStdOut,exifStdErr=launchSubProcess(exifSysString)
                
        # Generate Aware command line option for metadata embedding
        aMetadata=awareMetadataString(fileXMP)

    # Construct encoder command line 
    aInput=awareInputImageString(imageIn)
    aOutput=awareOutputImageString(imageOut)
    awareSysString=(shared.quoteString(j2kDriverApp) + " " + aInput + " " + awareOptionsString + " " +
                aMetadata + " " + aOutput)

    # Run encoder, but only if ExifTool didn't give any errors.
   
    if exifExitStatus==0:
        shared.printInfo("running Aware j2kdriver ...")
        awareExitStatus,awareStdOut,awareStdErr=launchSubProcess(awareSysString)
    else:
        shared.printWarning(imageIn + " not converted because ExifTool exited with errors! \
        See log file for details.")
        
    # Check if output image was really created and print info to screen
    # (Note that Aware can give exit code 0 even if things go seriously wrong!)
    jp2Created=jp2CreationInfo(imageOut)
    
    if extractMetadataFlag==True and exifExitStatus==0:
        # Clean up temporary file
        os.remove(fileXMP)
       
    # Create element object for storing conversion info
    conversionInfo=ET.Element('image')
    
    # Pre-format all output
    timeStr=time.asctime()  
    imageIn=shared.toUTF8(os.path.abspath(imageIn))
    imageOut=shared.toUTF8(os.path.abspath(imageOut))
    jp2Created=str(jp2Created)
    exifExitStatus=str(exifExitStatus)
    exifStdOut=shared.toUTF8(exifStdOut)
    exifStdErr=shared.toUTF8(exifStdErr)
    awareExitStatus=str(awareExitStatus)
    awareStdOut=shared.toUTF8(awareStdOut)
    awareStdErr=shared.toUTF8(awareStdErr)
    
    # Add to element  
    conversionInfo.appendChildTagWithText("time", timeStr)       
    conversionInfo.appendChildTagWithText("imageIn", imageIn)
    conversionInfo.appendChildTagWithText("imageOut", imageOut)
    conversionInfo.appendChildTagWithText("jp2Created", jp2Created)
    conversionInfo.appendChildTagWithText("exifExitStatus", exifExitStatus)
    conversionInfo.appendChildTagWithText("exifStdOut", exifStdOut)
    conversionInfo.appendChildTagWithText("exifStdErr", exifStdErr)
    conversionInfo.appendChildTagWithText("awareExitStatus", awareExitStatus)
    conversionInfo.appendChildTagWithText("awareStdOut", awareStdOut)
    conversionInfo.appendChildTagWithText("awareStdErr", awareStdErr)
        
    # Return conversion info
    return(conversionInfo)