script - Macro in Adobe Photoshop for consecutive numbers

26
2013-08
  • freeze_edit

    I want to have a way where the numbers of a thumbnail image will automatically run itself from one to 300 and save each individual image as a .png file (a macro/automation/script).

    For example, I want to have a thumbnail like this:

    Then, after a while:

    Basically, every consecutive number from 1 to 300. Also, something that would be nice is to have a way to select the start and stop numbers (instead of 1 to 300, say, 1 to 9, because two digit numbers will need the image to be adjusted).

    Is there any possible way to do this (even with external programs)? If so, how could it be done?

  • Answers
  • Julian Knight

    I'm not sure this is possible using the built-in "actions" macro feature of PS. It is certainly possible with the GIMP which has complete programming support natively.

    However, have you tried using something like AutoHotKey to generate a keyboard and/or mouse macro? It certainly has the logic and variable capabilities, you'd just need to make sure that you can control PS properly. I've done this with numerous applications in the past but not with PS.

    UPDATE: Given your problems with windows not aligning, the only way AHK macros would work for you would be if everything can be done via keyboard. Though I assume that you noted the mouse control is relative to the parent window?

    In GIMP, you can do macros using "script-fu", macros are written in a language called "scheme" though There are a number of sites with many examples. Here for example. If you need to go further, you can write your own plugins using Python. The GIMP documentation site has some details. Again there are plenty of sites with examples. Have a go and perhaps raise a new, more specific question as you get stuck ;)

    For Photoshop, here is the reference I missed last time. Thanks to Alan for pointing it out, it's been a long time since I last used Photoshop.


  • Related Question

    plugins - How do I split a Photoshop Document into a grid?
  • George Profenza

    If I have a Photoshop Document containing an image for example. How can I break it into a grid ? Similar to doing: - Select a rectangle - new Layer Via Cut - move to the next rectangle vertically repeat ?

    Any scripts/plugins that automate/parameterize that ?


  • Related Answers
  • warrenkopp

    The only way I've done this is to layout a grid with guides, then slice the image using the slice tool. This should allow you to break the image into many seperate pieces. After using the slice tool you can Save For Web, and export only the slices, each as a separate image file.

  • S.gfx

    It is similar to a question I answered here: I did recommend a free tool that allows you to do this, you export the png, load the png there and hit the button....

    Other soutions were proposed, too.

    http://superuser.com/questions/112346/image-editor-that-allows-multiple-separate-simultaneous-crop-selections-to-be-s/131787#131787

  • Community

    All nice answers, thanks for the contribution.

    As I said, I wondering if there was something out there for Photoshop.

    For documentation purposes here is a little script I wrote:

    var doc  = app.activeDocument;
    var arranger = makeDialog();
    
    arranger .arrange.onClick = function(){
        if(isNaN(parseInt(arranger.w.text)) || isNaN(parseInt(arranger.h.text)) || isNaN(parseInt(arranger.c.text)) || isNaN(parseInt(arranger.r.text))){
            alert('Yo! Can you feed \' dis dialog some integer numbers ?\n For Real!');
        }else{
            sliceLayer(parseInt(arranger.w.text),parseInt(arranger.h.text),parseInt(arranger.r.text),parseInt(arranger.c.text));
        }
        arranger.close();
    }
    
    arranger.show();
    
    function sliceLayer(w,h,r,c){
        var hw = w * 0.5;
        var hh = h * 0.5;
        var l0 = doc.activeLayer;
        for(var j = 0 ; j < r ; j++){
            for(var i = 0 ; i < c; i++){
                var x = i * w; //edited
                var y = j * h; //here
                //select
                doc.selection.select([[x,y],[x+w,y],[x+w,y+h],[x,y+h]]);
                //new layer via cut
                doc.selection.cut();
                var layer  = doc.artLayers.add();
                doc.paste();
                //move
                //$.writeln('translate x: '+(y)+', y: '+(x));
                layer.translate(y,x);
                doc.activeLayer = l0;
            }
        }
        l0.remove();//remove original layer(now empty)
    }
    
    
    function makeDialog(){
            var dialog = new Window('dialog','Slice and Dice');
            dialog.frameLocation = [300,300];
            dialog.w = dialog.add('edittext',undefined,'Selection width');
            dialog.w.prefferedSize = [200,50];
            dialog.h = dialog.add('edittext',undefined,'Selection height');
            dialog.h.prefferedSize = [200,50];
            dialog.c = dialog.add('edittext',undefined,'Grid Columns(V sections)');
            dialog.c.prefferedSize = [200,50];
            dialog.r = dialog.add('edittext',undefined,'Grid Rows(H sections)');
            dialog.r.prefferedSize = [200,50];
            dialog.arrange = dialog.add('button',undefined,'Chop Chop');
            return dialog;
    }
    

    If you save this in Photoshop/Presets/Scripts, you should be able to access it through Scripts in the File menu.

    The problem with the JSX approach is, it is a bit slow for large subdivisions (100x100).

    I'm guessing it will be faster to try with MATLAB/Photoshop communication, but I'll skip that for now.

    Achieved the same goal in actionscript 3 using copyPixels() and it's pretty fast.