Create a PSD file using a library called pytoshop. I found quite a few other things to read the psd file once with pytoshop and process it, but I could not find it to make it from scratch, so I looked at the official document and debugged it.
Basically, it is like composing a layer with OpenCV image data (numpy array) that is common in Python and writing it out.
pip install numpy scipy opencv-python Pillow six psd-tools3 pytoshop
main.py
#! env python
# -*- coding: utf-8 -*-
import os
import sys
import cv2
import pytoshop
from pytoshop import layers
import numpy as np
import cv2
def main():
#Image for layer
test_img = cv2.imread("test1.tif")
#
#Create a blank PSD file
#
psd = pytoshop.core.PsdFile(num_channels=3, height=test_img.shape[0], width=test_img.shape[1])
#Make 255 filled images(For transparency)
max_canvas = np.full(test_img.shape[:2], 255, dtype=np.uint8)
#
#Make a layer
#
#Do as many layers as you need
#Transparency np.ndarray([], dtype=np.uint8)
#255 is opaque, 0 is transparent, and you can create a layer with transparency by setting a grayscale mask image.
layer_1 = layers.ChannelImageData(image=max_canvas, compression=1)
# RGB
layer0 = layers.ChannelImageData(image=test_img[:, :, 2], compression=1) # R
layer1 = layers.ChannelImageData(image=test_img[:, :, 1], compression=1) # G
layer2 = layers.ChannelImageData(image=test_img[:, :, 0], compression=1) # B
new_layer = layers.LayerRecord(channels={-1: layer_1, 0: layer0, 1: layer1, 2: layer2}, #RGB image
top=0, bottom=test_img.shape[0], left=0, right=test_img.shape[1], #position
name="layer 1", #name
opacity=255, #Layer opacity
)
psd.layer_and_mask_info.layer_info.layer_records.append(new_layer)
#
#Export
#
with open("output.psd", 'wb') as fd2:
psd.write(fd2)
return
if __name__ == '__main__':
main()
Recommended Posts