I have a very simple use case where I need to pack a PNG file and a text layer into a PSD file and save it. That's it. I have tried psd-tools
for it and so far it works in principle.
The problem is that the text layer that it creates is not editable. The text layer itself is added as an image (transparent background) so I can't edit the text by opening it in Krita/Gimp.
Here's my code:
from PIL import Image, ImageDraw, ImageFont
from psd_tools.api.psd_image import PSDImage
from psd_tools.api.layers import PixelLayer
from psd_tools.constants import Compression
# Load PNG image
png_image = Image.open('fire.png').convert('RGBA')
# Create an empty PSD
psd = PSDImage.new(mode='RGBA', size=png_image.size)
# Create image layer from PNG
image_layer = PixelLayer.frompil(
pil_im=png_image,
psd_file=psd,
layer_name='Image Layer',
compression=Compression.RLE
)
psd.append(image_layer)
# Create a text image with Pillow
text = "Random Text Example"
font_size = 40
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", font_size)
except IOError:
font = ImageFont.load_default()
# Create a blank transparent image for the text
text_image = Image.new("RGBA", png_image.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(text_image)
draw.text((50, 50), text, font=font, fill=(255, 0, 0, 255)) # Red text
# Create text layer from image
text_layer = PixelLayer.frompil(
pil_im=text_image,
psd_file=psd,
layer_name='Text Layer',
compression=Compression.RLE
)
psd.append(text_layer)
# Save PSD
psd.save('output_with_text.psd')
I have looked other libs and found aspose-psd
can do editable text layer, but it's a paid feature in the python library and I need a license for it.
So, does anybody know a way to add an editable text layer to a PSD file programmatically.? Thanks.
I think the perfect answer to this Question is the description from OP mentioned alternative editors. My boldening for emphasis https://docs.krita.org/en/general_concepts/file_formats/file_psd.html
.psd, unlike actual interchange formats like *.pdf, *.tiff, *.exr, *.ora and *.svg doesn’t have an official spec online. Which means that it needs to be reverse engineered. Furthermore, as an internal file format, it doesn’t have much of a philosophy to its structure, as it’s only purpose is to save what Photoshop is busy with, or rather, what all the past versions of Photoshop have been busy with. This means that the inside of a PSD looks somewhat like Photoshop’s virtual brains, and PSD is in general a very disliked file-format.
Due to .psd being used as an interchange format, this leads to confusion amongst people using these programs, as to why not all programs support opening these. Sometimes, you might even see users saying that a certain program is terrible because it doesn’t support opening PSDs properly. But as PSD is an internal file-format without online specs, it is impossible to have any program outside it support it 100%.
So most non Adobe Creative Cloud apps could NOT reverse Adobe 1980s "Non-sensible Format"
The explanation by a modern advanced Graphics Editor (and others) related to PSD is:
".... It will likely never support vector and text layers, as these are just too difficult to program properly."
So although I have a piece of PhotoShop text over a small PNG.
The latest Gimp cannot (on file open) do anything other than convert the layers to common images.
Apart from use online Photopea which perhaps has an API for Scripting to add a vector text to an image as PSD. Then scripting a proprietary image / design app via command lines is borderline programming.