skip to Main Content

I need to create an automation to join two images in a layered file (layered as in Photoshop layers) to further process the images through batching inside Photoshop. I’ve tried ImageMagick, psd_tools, PIL and some other things. Most of those create either a single layered file (even if it’s a TIF, for example) or a multi-page file. Photoshop won’t recognize this multi-page file and read only the first image, which makes the file useless to me.

I was able to create a layered file just as I need it to be with layeredimage.io but the only format it seems to support as a multi-layer is .ORA (open raster) which is NOT supported by Photoshop. Gimp can handle it. By the way, layeredimage.io seems to be able to save a layered PSD, but returns a "not implemented" error. I also could not save a layered file in any other format (xcf or tif, for example).

So, at this point I’m in kind of a pickle. I have two options:

  1. Open the file in Gimp and via Scriptfu saving it in XCF or PSD;
  2. Use the gimpfu import to operate it inside the python script I’m creating.

Each has its own problems:

Option 1: Opening the file in gimp and a Scriptfu to save it in a different format

I can’t seem to understand how this scriptfu works or find more detailed information on the parameters to the save-psd command. I keep running into an error related to the "image" variable. I just need to open the file (which I can do inside the python script) and run one command (export as)

Option 2: Import gimpfu and operate gimp from inside the python script

I’ve seen it’s a common issue, but I couldn’t find an answer that works for me. The from gimpfu import * returns an error: ModuleNotFoundError: No module named 'gimpfu'.
I’m currently running the script in my Fedora which has gimp and Python 2.x installed and through a quick search I could find the gimpfu.py file inside usr/lib64/gimp/2.0/python which means it exists in my computer.
I have also tested in my Windows machine (which will be used for production) and it didn’t work also.

Below, the script so far:

import layeredimage.io as layered
from PIL import Image
# from gimpfu import * 

# TODO: FIND A WAY TO MAKE VSCODE READ GIMPFU IN /USR/LIB64/GIMP/2.0/PYTHON/GIMPFU.PY

def png_to_tif(file_name):
    """
    Converts a PNG file to TIF and returns the TIFF file path
    """
    png = Image.open(file_name)
    tif_name = file_name.replace("png", "tif")
    tiff = png.save(tif_name)
    return tif_name


base_file = layered.openLayerImage("/home/user/Pictures/imaginer/test1.tif")
imageDimensions = base_file.dimensions

mask_png = "/home/user/Pictures/imaginer/alpha.png"
mask_tif = png_to_tif(mask_png)
mask = Image.open(mask_tif)

img_layered = base_file.insertImageAsLayer(mask,'python',1)

ora_file = "/home/user/Pictures/imaginer/pqpBR.ora"

layered.saveLayer_ORA(ora_file, base_file)

I’ve tried checking if I had all the packages needed, tried alternative ways to interact with gimp and so far, nothing has worked.


Some Aditional Information:

Aparently I had some misconceptions and actually by trying to simplify a bit my explanation, I guess I might have left some important information out.

I have created a GoogleDive Folder to share a real-life example.

What is this and why do I need this:
This is an attempt to automate the very first step of an image retouching process which is masking out the non-important things in the image.
I’m a product photographer and I do know a bit of programming and I am now studying more Python in order to make some projects become reality. I’ve come across a way to automate the generation of a mask that is 95% perfect, hence the need of refining it inside Photoshop. Inside photoshop I can use a tool called "refine edge" which will compare the edges of the mask with the actual image and try make the mask coincide with the edges of the product.
The first step of the image editing that I have to go through is masking out the backgrounds and isolating the product in a transparent background. In this part of the process, the generation of the mask is the most time consuming process, which now I can do semi-automatically.

About the Process

The base file (currently) is a PSD file, but it could, if needed, become another image format as long as it is an image format that retains the original information without compressing (which rules out JPG, for example) or with minimum compressing. (Later, images like this one provided go through processes like color changing, montage, etc)

The mask provided to me is a grayscale PNG but it’s kind of easy to me to batch process them into RGB files. In the GoogleDive Folder I uploaded an example of the original mask and the RGB version.

I wasn’t mentioning this mess with file formats because I though it wouldn’t be a huge deal. But apparently from a programming side of things it is.

What do I need to figure out

I have automated the generation of the mask.
I have automated the refinement and application of the mask (inside Photoshop).
I now need a way to join the two files into one layered file (the order of the layers does not matter since I can rewrite/adapt the Photoshop automation easily). This middle step cost me hours weekly (although it’s already better than manually making the masks) and when the number of files increase too much, it becomes impossíble for me to do by myself.


About Mr. fmw42’s answer

If you are reading this in the future or if you got to this thread by googling something or whatever:
Frustrated with the fact that I couldn’t get the results, I decided to download the example images he sent and try to reproduce the results: It works! His answer is actually very accurate and produce the desired results.
However, when I apply the same commands in my own files, it does not work. I guess I’ll have to dig deeper into file specifications. For now, these are the specifications of the files:

PSD/TIF(Layer1):

  • Software used to generate the file: Capture One Pro 20
  • 16bit
  • Color space: AdobeRGB
  • Size: 3151x4758px

PNG(Layer2):

  • 8bit
  • Color space: L
  • Size: 3151x4758px

I did try converting the PNG to 16bit RGB (so that both images would be equal in specifications) and applying the command, with no good results so far.

Most recent command being used:
magick _AUG1017.psd -delete 0 _AUG1017_1_maskRGB.png ( -clone 0--1 -flatten ) -reverse magick_layered.psd

2

Answers


  1. Chosen as BEST ANSWER

    So, I'm considering this problem solved since fmw42's answer does actually produce the results I need although they do not work for my files specifically.

    A quick search and I've seen that the fact that my files are 16bit is a lot more significant than I expected.

    I would like to thank fmw42 for the answer and the time spent helping me but, for now, I have to dig deeper into manipulating 16bit image files. If anyone has any resources to share, that would be very much apreciated.

    Also, cgohlke's comment on the original post would have produced very good results if not for the fact that my images are 16bit. I tried pluggin my images to the code and it returned a buuunch of errors. But it's also very viable and a good solution.


  2. In Imagemagick, to create a simple layered PSD, you must create the PSD flattened layer. You also must reverse the order of images. So for

    Input:

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    Unix IM 6 Syntax:

    convert lena.jpg mandril3.jpg zelda1.jpg redhat.jpg ( -clone 0-3 -flatten ) -reverse layered.psd
    

    Resulting Layers PSD (zip file):

    For Windows, remove the s from the parentheses. For IM 7, change convert to magick

    ADDITION

    Let’s add the following image to the resulting PSD file above. Note that one has to remove the previous psd flattened layer, add the new file, then recreate the new flattened layer.

    New layer image:

    enter image description here

    convert layered.psd -delete 0 barn.jpg ( -clone 0--1 -flatten ) -reverse layered2.psd
    

    The result is (zip file):

    Note that 0–1 means 0 to -1 where -1 means the last image and 0 means the first image in the command line (after we have deleted the previous first image).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search