PhotoshopでCSVから画像を一括生成
JavaScript October 16th, 2008CSVファイルを読んでPNG画像を作るJavaScript。要Photoshop。テキストファイルはこんな。
txt1,txt2,・・・は保存する時にファイル名やフォルダ名として使う。
txt1,てきすと1 txt2,てきすと2 txt3,てきすと3 ・ ・ ・
csvtoimg.jsx
#target photoshop /* * 画像形式:PNG * サイズ :500*500 * 背景 :透過 * 文字色 :白(255,255,255) * フォント:BookAntiqua * 文字配置:中央 */ function main() { // 単位の設定(定規:ピクセル, 文字:ポイント) app.preferences.rulerUnits = Units.PIXELS; app.preferences.typeUnits = TypeUnits.POINTS; var dataFile = File.openDialog("テキストファイルを指定してください。"); fileObj = new File(dataFile); flag = fileObj.open("r"); if (flag){ fileData = fileObj.read(); Line = fileData.split("\n"); // base file open var basefile = app.documents.add(500, 500, 72, "base", NewDocumentMode.RGB, DocumentFill.TRANSPARENT); // png save options var pngOpt = new ExportOptionsSaveForWeb(); pngOpt.format = SaveDocumentType.PNG; pngOpt.PNG8 = false; var textColor = new SolidColor; textColor.rgb.red = 255; textColor.rgb.green = 255; textColor.rgb.blue = 255; for (i=0; i < Line.length-1; i++){ var data = Line[i].split(","); var tmpfile = app.documents.add(500, 500, 72, "tmp", NewDocumentMode.RGB); var newTextLayer = tmpfile.artLayers.add(); newTextLayer.kind = LayerKind.TEXT; newTextLayer.textItem.font = "BookAntiqua"; newTextLayer.textItem.fauxBold = true; newTextLayer.textItem.size = 24; newTextLayer.textItem.color = textColor; tmpfile.activeLayer.textItem.contents = data[1]; tmpfile.selection.selectAll(); tmpfile.selection.copy(); tmpfile.close(SaveOptions.DONOTSAVECHANGES); basefile.artLayers.add(); basefile.paste(); // save var savefile = new File("\\\\path\\to\\outputdir\\" + data[0] + "\\txt.png"); basefile.exportDocument(savefile, ExportType.SAVEFORWEB, pngOpt); basefile.activeLayer.remove(); } basefile.close(SaveOptions.DONOTSAVECHANGES); }else{ alert("データファイルが見つかりません"); } } main();
jsxファイルをダブルクリック
or jsxファイルをPhotoshopにドラッグする
or Photoshopのメニューからファイル > スクリプト > 参照、jsxファイルを選択
で動かす。
つまったところ
中央配置
テキストを中央に配置するよい方法がわからなかったので"base"というドキュメントの他に"tmp"というドキュメントを作った。"tmp"にテキストレイヤーを作って文字列挿入、コピーして"base"へ張り付け。何も考えずペーストすると中央に置かれる。もっとよい方法ありそう。
ついでに、何もないレイヤーにペーストすると、新しいレイヤーが作られない。レイヤーが1個しかない状況でactiveLayer.remove()できないので、なんか無駄に basefile.artLayers.add(); とかしてる。selectAll()してから消去とかもできたのかな。
ドキュメントのフォーカス
"tmp"でコピーした後は"base"を前面に出してペースト。"tmp"を背面に押しやる方法がわからなかったのでドキュメントを閉じてる。CSVの行の分だけ、open,closeを繰り返してる。
