Adding photo EXIF data to Steemit postssteemCreated with Sketch.

in #code7 years ago

EXIF to Steemit markdown generator

Normally, I like to show the photo EXIF metadata along with images I post, but Steemit doesn't do that for you automatically. Until today, I had sometimes been copying and pasting the data, but not always in a consistent format given that there's quite a bit of EXIF data that isn't really important or useful. Then, once you get it into the markdown editor, it is tedious to format.

Since I'm lazy and also know how to code, I decided to do something about this. The result is a small bit of NodeJS code that pulls out a few select bit of EXIF data, and then prints them out on the command line in a format that can be copy/pasted into the Steemit markdown editor in one shot. No further formatting required.

Usage

The code is really simple, in part, because it doesn't do anything too fancy. For now, it just pick out the cama type, the lens type, and the exposure settings. I suppose could get the flash settings, but I haven't needed that yet.

F:\Projects\Steemit>node exif2md.js "C:\Some\Path\P1020270.jpg"

Setting | Value
- | -
File | P1020270.jpg
Software | Adobe Photoshop Lightroom 6.9 (Windows)
Camera | Panasonic DMC-GX1
Lens | LUMIX G 20/F1.7 II
Focal Length | 20mm (40mm equivalent on 35mm camera)
Apature | ƒ1.7
ISO | 160
Shutter | 1/160s
Date | 2017-08-06


F:\Projects\Steemit>

Result

When copy/pasted into the Steemit markdown editor window, this is what it looks like...

SettingValue
FileP1020270.jpg
SoftwareAdobe Photoshop Lightroom 6.9 (Windows)
CameraPanasonic DMC-GX1
LensLUMIX G 20/F1.7 II
Focal Length20mm (40mm equivalent on 35mm camera)
Apatureƒ1.7
ISO160
Shutter1/160s
Date2017-08-06

Code

To use this yourself, copy the code below to your PC and save as "exif2md.js". You'll also need to install the EXIF module, if you haven't already done that (type npm install exif).

//exif2md.js - simple NodeJS code to read an image file, extract select EXIF metadata fields, and then convert them into steemit markdown table format
//requires the exif library
//https://www.npmjs.com/package/exif#installation


var ExifImage = require('exif').ExifImage;
 
try {
    console.log( '' )

    //check if we have a file to work with
    if(!process.argv[2]) {
        console.log( 'Usage: ' + process.argv[0] + ' ' + process.argv[1] + " SoMeFileName.jpg")
    } else {
        var imageFileName = process.argv[2]
    } //end file name check if
    
    //open the exif library
    new ExifImage({ image : imageFileName }, function (error, exifData) {
        if (error)
            console.log('Error: '+error.message);
        else
            //console.log(exifData); //show everything
        
            //lets check a few things and set some vars
            //some vlaues may be blank for people that use older manual focus lenses
            if(exifData.exif.LensModel == '----' || !exifData.exif.LensModel) {
                lensModel = 'Not recorded'
            } else {
                lensModel = exifData.exif.LensModel
            } //end LensModel check if
            if(exifData.exif.FocalLength == 0) {
                focalLength =  'Not recorded'
            } else {
                focalLength =  exifData.exif.FocalLength +'mm (' + exifData.exif.FocalLengthIn35mmFormat+ 'mm equivalent on 35mm camera)'
            } //end FocalLength check if
            if(exifData.exif.FocalLength == 0 || !exifData.exif.LensModel) {
                fNumber = 'Not recorded'
            } else {
                fNumber = 'ƒ' + exifData.exif.FNumber
            } //end LensModel check if
            if(!exifData.exif.ShutterSpeedValue) {
                if(!exifData.exif.ExposureTime) {
                    shutterSpeedValue = 'Not recorded'
                } else {
                    shutterSpeedValue = calculateExposureSpeed(exifData.exif.ExposureTime)
                }
            } else {
                shutterSpeedValue = calculateShutterSpeed(exifData.exif.ShutterSpeedValue)
            } //end ShutterSpeedValue check if
            
            
            //trim the path off the file name
            imageFileName = imageFileName.replace(/\\/g, '\/') //convert Windows paths to LINIX paths
            imageFileName = imageFileName.replace(/^(.*)\/(.*)/, '$2') //now remove the path
            
            //start formating things in markdown with the values from above
            console.log( 'Setting | Value' )
            console.log( '- | -' )
            console.log( 'File | ' + imageFileName )
            console.log( 'Software | ' + exifData.image.Software )
            console.log( 'Camera | ' + exifData.image.Make + ' ' + exifData.image.Model )
            console.log( 'Lens | ' + lensModel )
            console.log( 'Focal Length | ' + focalLength )
            console.log( 'Apature | ' + fNumber )
            console.log( 'ISO | ' + exifData.exif.ISO )
            console.log( 'Shutter | ' + shutterSpeedValue )
            console.log( 'Date | ' + exifData.exif.CreateDate.replace(/^(\d+)(:)(\d+)(:)(\d+)\s.*/, '$1-$3-$5') )
            
            //all done
            console.log( '' )

    });
} catch (error) {
    console.log('Error: ' + error.message);
} //end try catch


//calculate the shutter speed value from the APEX value
function calculateShutterSpeed(shutterSpeedValue) {
    if (!shutterSpeedValue) {
        return '';
    }
    
    var apex = parseFloat(shutterSpeedValue);
    var shutter = Math.pow(2, -apex);
    if (shutter == 0) {
        return false;
    }
    if (shutter >= 1) {
        return Math.round(shutter) + 's';
    }
    return '1/' + Math.round(1 / shutter) + 's';
}

//calculate the exposure speed value from the APEX value (used in place of shutter speed on some cameras)
function calculateExposureSpeed(exposureSpeedValue) {
    if (!exposureSpeedValue) {
        return '';
    }
    
    var apex = parseFloat(exposureSpeedValue);
    var shutter = apex;
    if (shutter == 0) {
        return false;
    }
    if (shutter >= 1) {
        return Math.round(shutter) + 's';
    }
    return '1/' + Math.round(1 / shutter) + 's';
}

//end

Example

When paired with a previously uploaded image (you have to upload the image yourself), here is what the result looks like...

P1020270.jpg

SettingValue
FileP1020270.jpg
SoftwareAdobe Photoshop Lightroom 6.9 (Windows)
CameraPanasonic DMC-GX1
LensLUMIX G 20/F1.7 II
Focal Length20mm (40mm equivalent on 35mm camera)
Apatureƒ1.7
ISO160
Shutter1/160s
Date2017-08-06

Comment below if you have any questions, though if you unsure how to install NodeJS or related modules, you might want to first have a look at the NodeJS site.

That's it now.

Coin Marketplace

STEEM 0.22
TRX 0.20
JST 0.035
BTC 91483.13
ETH 3152.10
USDT 1.00
SBD 3.10