Search code examples
javascriptphotoshopphotoshop-script

Scripting Photoshop Difference Blendmode


I regularly have two sets of pictures named the same way and I would like to script the process of checking for differences. I'm looking for a basic check, if there is no differences between the two images, discard one of them, if there is a single pixel difference, keep both. For those who question the wisdom of doing this in photoshop, this is an addition to another script that is already running and this optional check will help reduce the number of files I have to upload. I would appreciate the help.


Solution

  • If you really have to do this in Photoshop, this is how I'd propose it:

    var doc1 = app.open(new File("~/Desktop/test1.bmp"));
    var doc2 = app.open(new File("~/Desktop/test2.bmp"));
    
    doc2.selection.selectAll();
    doc2.selection.copy();
    
    app.activeDocument = doc1;
    var newLayer = doc1.paste();
    newLayer.blendMode = BlendMode.DIFFERENCE;
    
    var histogram = doc1.histogram;
    for (var i = 1; i < histogram.length; ++i) {
        if (histogram[i] > 0) {
            alert('Different!');
            break;
        }
    }
    

    I paste the second picture into the first one and set the resulting layer's blend mode to difference. If the two pictures are identical, the resulting picture should be all black. I therefore check if any color values apart from 0 have any pixels in the histogram.

    I assumed the two images have the same size.