Search code examples
pythonimage-processingimagemagickphotoshopclipping

How can I batch convert pictures with existing clipping path to JPEGs with a white background in Photoshop (or a free software like ImageMagick)?


To be more detailed here: They are TIFF images, have one path - which is a clipping path - but still have their background, for some reasons.

There are hundreds of them so I cant just open it and select path -> make selection -> invert selection -> fill selection with white color..

I tried doing a Photoshop action which I could use as a action available at the batch processing mode. It didnt work, seem to be to sophisticated for the "action" module or I did it wrong.

Also tried to make a script for "ImageMagick" which worked - after I finally had the right parameters - for many pictures, but interestingly not for all - altough they are all made the same. All clipping paths from Adode Photoshop, saved as TIFF pictures.

If someone is a pro in ImageMagick I can post this script here and you may comment if I did something wrong here.

Hope someone can help. Please only possibilities which are free of cost like ImageMagick or can be done with Photoshop itself!

I am also in scripting with Python or Shellscript for example, anything based on that is also okay - but I searched for libraries and found nothing for Python :/


Solution

  • Clipping path doesn't change image representation in Photoshop: it only affects when you place it in inDesign/Illustrator/etc.

    But if you still want to fill everything outside the clipping path with white you can use this script:

    (function() {
        var sourceFolder = Folder.selectDialog("choose folder");
    
        fileList = sourceFolder.getFiles("*.tif");
        for (i = 0; i < fileList.length;i++) {
            open(fileList[i]);
            var doc = activeDocument;
            try {
                var workPath = activeDocument.pathItems[0].name
            } catch (e) {
                alert(e);
                return
            }
            loadSelection(workPath)
            doc.selection.invert();
            var white = new SolidColor;
            white.gray.gray = 0;
            doc.selection.fill(white)
            doc.selection.deselect();
            doc.close(SaveOptions.SAVECHANGES)
        }
    
        function loadSelection(name) {
            var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putProperty(c2t("Chnl"), c2t("fsel"));
            desc.putReference(c2t("null"), ref);
            var ref1 = new ActionReference();
            ref1.putName(c2t("Path"), name);
            desc.putReference(c2t("T   "), ref1);
            desc.putInteger(c2t("Vrsn"), 1);
            desc.putBoolean(s2t("vectorMaskParams"), true);
            executeAction(c2t("setd"), desc, DialogModes.NO);
        }
    
        function c2t(c) {
            return charIDToTypeID(c)
        }
    
        function s2t(c) {
            return stringIDToTypeID(c)
        }
    
    })()