利用Photoshop javascript 给图片添加拍摄日期(creation date)

简单的自动化脚本,方便批量作业。

// This script is used to add creation date for each .jpg
// in folder specified as workDir and save it with the same
// name in another folder specified as saveDir.

var workDir = 'C:/Users/ABC/Pictures/Camera/' 
var saveDir = 'C:/Users/ABC/Pictures/output/' 

// Get all .jpg files in folder.
var processFolder = Folder(workDir)
var fileList = processFolder.getFiles(/\.(jpg)$/i);

app.bringToFront();
// Loop through files
for (var i = 0; i < fileList.length; i++) {
     // Only process the returned file objects
     // The filter 'should' have missed out any folder objects 
     if (fileList[i] instanceof File && fileList[i].hidden == false) {
	addCreationDateAndSave(fileList[i], new File(saveDir + fileList[i].name))
	//$.writeln(fileList[i].name)
     }
}

function addCreationDateAndSave(srcFile, saveFile) {
    // Remember current unit settings and then set units to
    // the value expected by this script
    var originalUnit = preferences.rulerUnits
    preferences.rulerUnits = Units.INCHES
    var fileRef = srcFile
    var docRef = app.open(fileRef)

    // Create a new art layer containing text
    var artLayerRef = docRef.artLayers.add()
    artLayerRef.kind = LayerKind.TEXT

    // Set the contents of the text layer.
    var textItemRef = artLayerRef.textItem

    // Set text color, font and size
    textColor = new SolidColor();
    textColor.rgb.red = 255
    textColor.rgb.green = 255
    textColor.rgb.blue = 255
    textItemRef.color = textColor
    textItemRef.font = "Myriad Pro"
    textItemRef.size = new UnitValue(24,  "pt")

    // Set position of textItem, pre-estimate the width and
    // height of textItem of contents like '2017/03/30' for
    // a specified font size.
    var tHalfWidth = new UnitValue(0.75, "in")
    var tHeight = new UnitValue(0.3, "in")
    textItemRef.position = Array(app.activeDocument.width.as("in") * 0.5 -
	tHalfWidth.as("in"), app.activeDocument.height.as("in") * 0.98 -
	tHeight.as("in"))

    // creationDate example: 20170413
    var Y = docRef.info.creationDate.substring(0,4)
    var M = docRef.info.creationDate.substring(4,6)
    var D = docRef.info.creationDate.substring(6)
    textItemRef.contents = Y + "/" + M + "/" + D

    jpgSaveOptions = new JPEGSaveOptions()
    jpgSaveOptions.embedColorProfile = true
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE
    jpgSaveOptions.matte = MatteType.NONE
    jpgSaveOptions.quality = 12
    app.activeDocument.saveAs(saveFile, jpgSaveOptions, true,
	Extension.LOWERCASE)
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES)

    // Restore original ruler unit setting
    app.preferences.rulerUnits = originalUnit

    // Release references
    docRef = null
    artLayerRef = null
    textItemRef = null
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值