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

Script for divide split slices into arrays of certain length?

Community Beginner ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

Hello everyone, I'm looking for i need a script that can split an image into equal horizontal slices, with each slice length greater than 1000px and less than 2000px. By the way, I could not use "pixels per slice" at divide slice dialog because I can't control the last slice of the image. I usually look at the length of the image, then divide by 2000 and get the nearest higher value.

For example, I have a 600x8000px file. 8000 divide by 2000, the result is 4, and I will divide the file into 5 slices. There are many files with different sizes, I can't split each of the multiple files. So if there is any possible solution, please point me out. Thank you very much!

TOPICS
Actions and scripting , Windows

Views

850

Likes

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

correct answers 1 Correct answer

Community Expert , Nov 18, 2022 Nov 18, 2022

Try this.

#target photoshop 

var doc = activeDocument;
var oldPrefs = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS;
var dH = doc.height.value;
var splits = parseInt( dH/2000 + 1);

var sliceAmt = dH/splits

for (var i=0;i<splits;i++){
    if(i + 1 == splits){
        sliceImg (i*sliceAmt, 0, doc.height.value, doc.width.value)
        }
    sliceImg (i*sliceAmt, 0, (i+1) * sliceAmt, doc.width.value)
    }

app.preferences.rulerUnits = oldPrefs;

function sliceImg(topS, le
...

Likes

Translate

Translate
Community Expert ,
Nov 17, 2022 Nov 17, 2022

Copy link to clipboard

Copied

Likes

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
Community Beginner ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

Thank you for the answer. I just looked through the links, they don't seem to suit my intentions well, sorry about my poor wording (but they are very useful anyway, thanks a lot)

Do you know any solution to create slices (not export them, just divide into slice) via canvas size? 

Likes

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
Community Expert ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

Try this.

#target photoshop 

var doc = activeDocument;
var oldPrefs = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS;
var dH = doc.height.value;
var splits = parseInt( dH/2000 + 1);

var sliceAmt = dH/splits

for (var i=0;i<splits;i++){
    if(i + 1 == splits){
        sliceImg (i*sliceAmt, 0, doc.height.value, doc.width.value)
        }
    sliceImg (i*sliceAmt, 0, (i+1) * sliceAmt, doc.width.value)
    }

app.preferences.rulerUnits = oldPrefs;

function sliceImg(topS, leftS, bottomS, rightS){
    var idMk = charIDToTypeID( "Mk  " );
        var desc4 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idslice = stringIDToTypeID( "slice" );
            ref1.putClass( idslice );
        desc4.putReference( idnull, ref1 );
        var idUsng = charIDToTypeID( "Usng" );
            var desc5 = new ActionDescriptor();
            var idType = charIDToTypeID( "Type" );
            var idsliceType = stringIDToTypeID( "sliceType" );
            var iduser = stringIDToTypeID( "user" );
            desc5.putEnumerated( idType, idsliceType, iduser );
            var idAt = charIDToTypeID( "At  " );
                var desc6 = new ActionDescriptor();
                var idTop = charIDToTypeID( "Top " );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idTop, idPxl, topS );
                var idLeft = charIDToTypeID( "Left" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idLeft, idPxl, leftS );
                var idBtom = charIDToTypeID( "Btom" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idBtom, idPxl, bottomS );
                var idRght = charIDToTypeID( "Rght" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idRght, idPxl, rightS );
            var idRctn = charIDToTypeID( "Rctn" );
            desc5.putObject( idAt, idRctn, desc6 );
        var idslice = stringIDToTypeID( "slice" );
        desc4.putObject( idUsng, idslice, desc5 );
    executeAction( idMk, desc4, DialogModes.NO );    
    }

Likes

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
Community Beginner ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

Many thanks! This script works for me, really thank you!

Likes

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
Community Expert ,
Nov 18, 2022 Nov 18, 2022

Copy link to clipboard

Copied

LATEST

Glad it works for you!

Likes

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