Create a Photoshop format file (.psd) with python

screen.png

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.

Installation of required packages

pip install numpy scipy opencv-python Pillow six psd-tools3 pytoshop

script

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

Create a Photoshop format file (.psd) with python
Create a directory with python
[Python] Create a Tkinter program distribution file with cx_Freeze
Create a 2d CAD file ".dxf" with python [ezdxf]
Create an Excel file with Python3
Create a binary file in Python
Create a file uploader with Django
[Python] Create a file & folder path specification screen with tkinter
Create a Python function decorator with Class
Creating a simple PowerPoint file with Python
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Create a large text file with shellscript
Create a VM with a YAML file (KVM)
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Create Excel file with Python + similarity matrix
Create a word frequency counter with Python 3.4
Create a deb file from a python package
[GPS] Create a kml file in Python
I made a configuration file with Python
[ROS2] How to play a bag file with python format launch
Create a frame with transparent background with tkinter [Python]
How to read a CSV file with Python 2/3
Create a GIF file using Pillow in Python
Read and format a csv file mixed with comma tabs with Python pandas
Create a Python module
Create a GUI executable file created with tkinter
Create a LINE BOT with Minette for Python
How to create a JSON file in Python
Create a PDF file with a random page size
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
Create a color bar with Python + Qt (PySide)
Steps to create a Twitter bot with python
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a color-specified widget with Python + Qt (PySide)
Create a Python environment
Create a MIDI file in Python using pretty_midi
Create a Python console application easily with Click
Read line by line from a file with Python
I want to write to a file with Python
Open a file dialog with a python GUI (tkinter.filedialog)
Create a cylinder with open3d + STL file output
[Python] Create a ValueObject with a complete constructor using dataclasses
Why not create a stylish table easily with Python?
Create a python development environment with vagrant + ansible + fabric
Create miscellaneous Photoshop videos with Python + OpenCV ④ Deal with issues
Create a Layer for AWS Lambda Python with Docker
Read json file with Python, format it, and output json
[python] Create a date array with arbitrary increments with np.arange
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
Python script to create a JSON file from a CSV file
[Python] How to create a 2D histogram with Matplotlib
Run a Python file with relative import in PyCharm