• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Crop Image file by AfterEffects Script

New Here ,
Oct 28, 2020 Oct 28, 2020

Copy link to clipboard

Copied

//I have a question for the first time.

//The question this time is (to create an effect like a 15 puzzle game) First, I want to //divide one image into 16 pieces and output each to a file.

//The effect of the 15 puzzle game has been made with Flash, so it would be nice if the image could be divided into 16 parts.

//Please tell me just the keywords.
//The OS I'm using is Windows.

 

 

function funcINIT(){
app
  //.newProject()
  .project.items.addComp("SeqImgToMovie",1920, 1080, 1.0, 15, 29.97)

  app
  .project.items.addComp("MovieToSeqImg", 1920, 1080, 1.0, 15, 29.97)
}

var myWin = new Window("palette", "15Puzzle");
myWin.orientation = "row";

 

var RowSlider = myWin.add("slider");
RowSlider.minvalue = 4;
RowSlider.maxvalue = 30;
RowSlider.value = 4;

var RowNum = myWin.add("statictext", [20, 70, 130, 90], "DivideRow: " + Math.round(RowSlider.value) );

RowSlider.onChanging = function(){
  if(RadioEnglish.value){
    RowNum.text = "DivideRow: " + Math.round(RowSlider.value);
  }
}

var ColSlider = myWin.add("slider");
ColSlider.minvalue = 4;
ColSlider.maxvalue = 30;
ColSlider.value = 4;

var ColNum = myWin.add("statictext", [20, 70, 130, 90], "DivideCol: " + Math.round(ColSlider.value) );

ColSlider.onChanging = function(){
  if(RadioEnglish.value){
    ColNum.text = "DivideCol: " + Math.round(ColSlider.value);
  }
}


var GapSizeSlider = myWin.add("slider");
GapSizeSlider.minvalue = 0;
GapSizeSlider.maxvalue = 5;
GapSizeSlider.value = 1;

var GapSizeNum = myWin.add("statictext", [20, 70, 130, 90], "GapSize: " + Math.round(GapSizeSlider.value) );

GapSizeSlider.onChanging = function(){
  if(RadioEnglish.value){
    GapSizeNum.text = "GapSize: " + Math.round(GapSizeSlider.value);
  }
}


var chkEase = myWin.add("checkbox", [30, 10, 150, 30],"Ease IN / OUT");
chkEase.value=false;

chkEase.onClick = function(){
  if(chkEase.value == true){

  }
}

var chkQuick = myWin.add("checkbox", [30, 10, 160, 30],"Accelerate at the end");
chkQuick.value=false;

  chkQuick.onClick = function(){
    if(chkQuick.value == true){

    }else{
  }
}

var easeIn = new KeyframeEase(0.5, 50);
var easeOut = new KeyframeEase(0.75, 85);

var btnDefault = myWin.add("button", undefined, "Default");

btnDefault.onClick = function(){
  RowSlider.value = 4;
  ColSlider.value = 4;
  GapSizeSlider.value = 1;

  chkEase.value = false;
  chkQuick.value=false;

  funcDataRefresh();
}

 

var btnImport = myWin.add("button", undefined, "Import");
var btnCreate = myWin.add("button", undefined, "Create");

var SingleSelectFLG = true;

btnImport.onClick= function(){
  var parentFileObj=new Folder("~/desktop/");
  var result=parentFileObj.openDlg("Select the IMAGE file", ["image file:*.png"], false);
  if(result!=null){
    var openFileObj=new File(result);

    funcSegmentationImage(importFile(openFileObj),"CutImage",4,4);
   funcINIT();
  }
}

function importFile(location) {
  var myImageFile = new File(location);
  var importOptions = new ImportOptions(myImageFile);
  var footageItem = app.project.importFile(importOptions);
  return footageItem;
}

function funcInstanceofItem(theLayer){ 
  //var theLayer = app.project.activeItem.layer(1);
  if (theLayer instanceof AVLayer) {
    if (theLayer.source instanceof CompItem) alert("it's a comp");
    else if (theLayer.source instanceof FootageItem) {
    if (theLayer.nullLayer) alert("it's a null");
    else if (theLayer.source.mainSource instanceof SolidSource) alert("it's a solid");
    else alert("it's footage");
    }
  } else alert("not AV");
}

function funcSegmentationImage(Ori_image, FileName, splitY, splitX){

  var out = "~/Desktop/OutData";

  var stepY = ori_image.height/splitY;
  var stepX = ori_image.width/splitX;

  var num = 0;

 

  for ( var i = 0; i <= splitY; i++ ) {
    for ( var j = 0; j <= splitX; j++ ) {

    //pngOptを定義
    var pngOpt = new ExportOptionsPNG24();
    with (pngOpt) {
      exportFormat = "PNG";
      colorMode = "32 bit";
      crop = "true";
      cropBottom = (1+j)*stepY;
      cropLeft = i*stepX;
     cropRight = (1+i)*stepX;
     cropTop = j*stepY;
   }
   var savePath = out+"/" + FileName+ "_" + (num++) + ".png";

    var colorspace = true;
    var bm = new BitmapData(pngOpt, colorspace);
    bm.exportTo(new File(savePath), 100);

    //exportFile(savePath),ExportType.PNG24,pngOpt);
    }
  }
}

btnCreate.onClick = function(){

}


var RadioJapanese = myWin.add("radiobutton", undefined, "Japanese");
var RadioEnglish = myWin.add("radiobutton", undefined, "English");

RadioEnglish.value = true;

RadioJapanese.onClick = RadioEnglish.onClick = function(){
  //funcDataRefresh();
}

myWin.show();

 

//______________Below are the unused user functions.________________

function saveCanvas(canvas, saveType){
  var imageType = "image/png";
  var fileName = "sample.png";
  if(saveType === "jpeg"){
    imageType = "image/jpeg";
    fileName = "sample.jpg";
  }
   /////var canvas = document.getElementById("myCanvas");
  // base64エンコードされたデータを取得 「data&colon;image/png;base64,iVBORw0k~」
  var base64 = canvas.toDataURL(imageType);
  // base64データをblobに変換
  var blob = Base64toBlob(base64);
  // blobデータをa要素を使ってダウンロード
  saveBlob(blob, fileName);
}

// Base64データをBlobデータに変換
function Base64toBlob(base64){
  // カンマで分割して以下のようにデータを分ける
  // tmp[0] : データ形式(data&colon;image/png;base64)
  // tmp[1] : base64データ(iVBORw0k~)
  var tmp = base64.split(',');
  // base64データの文字列をデコード
  var data = atob(tmp[1]);
  // tmp[0]の文字列(data&colon;image/png;base64)からコンテンツタイプ(image/png)部分を取得
  var mime = tmp[0].split(':')[1].split(';')[0];
  // 1文字ごとにUTF-16コードを表す 0から65535 の整数を取得
  var buf = new Uint8Array(data.length);
  for (var i = 0; i < data.length; i++) {
    buf[i] = data.charCodeAt(i);
  }
  // blobデータを作成
  var blob = new Blob([buf], { type: mime });
  return blob;
}

// 画像のダウンロード
function saveBlob(blob, fileName){
  var url = (window.URL || window.webkitURL);
  // ダウンロード用のURL作成
  var dataUrl = url.createObjectURL(blob);
  // イベント作成
  var event = document.createEvent("MouseEvents");
  event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  // a要素を作成
  var a = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
  // ダウンロード用のURLセット
  a.href = dataUrl;
  // ファイル名セット
  a.download = fileName;
  // イベントの発火
  a.dispatchEvent(event);
}

TOPICS
Scripting

Views

246

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 28, 2020 Oct 28, 2020

Copy link to clipboard

Copied

//I'm sorry, the source code was longer than I expected. I will reply to myself only where I do not understand.

 

function funcSegmentationImage(Ori_image, FileName, splitY, splitX){

  var out = "~/Desktop/OutData";

  var stepY = ori_image.height/splitY;
  var stepX = ori_image.width/splitX;

  var num = 0;

 

  for ( var i = 0; i <= splitY; i++ ) {
    for ( var j = 0; j <= splitX; j++ ) {

      //pngOptを定義
      var pngOpt = new ExportOptionsPNG24();
      with (pngOpt) {
        exportFormat = "PNG";
        colorMode = "32 bit";
        crop = "true";
        cropBottom = (1+j)*stepY;
        cropLeft = i*stepX;
        cropRight = (1+i)*stepX;
        cropTop = j*stepY;
      }
      var savePath = out+"/" + FileName+ "_" + (num++) + ".png";

      var colorspace = true;
      var bm = new BitmapData(pngOpt, colorspace);
      bm.exportTo(new File(savePath), 100);

      //exportFile(savePath),ExportType.PNG24,pngOpt);

    }

  }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 02, 2020 Dec 02, 2020

Copy link to clipboard

Copied

LATEST
 

[Solved] I thought about an algorithm that assigns footage to components and crops them myself, and realized what I wanted to do. It seems that the following source code can also be used.

 

var crop_data = {
"Crop":true,
"Crop Bottom":0,
"Crop Left":0,
"Crop Right":8,
"Crop Top":10
};

app.project.renderQueue.item(1).outputModule(3).setSettings( crop_data );

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines