I can't find a way around merging multiple pngs if they're not in the same directory as main.py.
I'd use:
from PIL import Image
#myFolder has layer1.png, layer2.png, myFolder is in same directory as main.py
Image.alpha_composite(-,-).save("test1.png")
to merge the pngs together.
CodePudding user response:
Just use the full path:
img1 = "/path/to/layer1.png"
img2 = "/path/to/layer2.png"
Image.alpha_composite(img1,img2).save("test1.png")
Note that this is untested as I don't use cv2
very often.
CodePudding user response:
Python has several modules that can build path names. pathlib
is a good choice. Python scripts and modules include a magic variable __file__
that names the file being executed. You can use that to find the script's directory and then pathlib
to get the path to myFolder
and its contents. Path
overrides the division operator to join path names. Clever... if not a bit too clever. After that, you need to open the two images to get the composite.
from PIL import Image
from pathlib import Path
# get this script's directory and then png directory underneath
script_folder = Path(__file__).absolute().parent
my_folder = script_folder/"myFolder"
Image.alpha_composite(Image.open(my_folder/"layer1.png"),
Image.open(my_folder/"layer2.png")).save("test1.png")