About psd-tools, a library that can process psd files in Python

Library psd-tools that can process PSD files in Python

▼ Official document https://psd-tools.readthedocs.io/en/latest/

Test image

This time, I will use this image as a sample. 3070374.jpg Designed by Freepik

▼ Download destination https://www.freepik.com/free-psd/restaurant-gift-voucher-template_5903685.htm#page=3&position=11

▼ Layer information seen on Photoshop image.png

How to use psd-tools

Installation

pip install psd-tools numpy scipy

How to use


from psd_tools import PSDImage
if __name__ == '__main__':
    file_path = '/Users/[uesr_name]/Downloads/restaurant-gift-voucher-template/3074963.psd'
    psd = PSDImage.open(file_path)
    psd.compose().save('example.png')

    for layer in psd:
        print(layer)

▼ Output

▼example.png example.png


Group('Background' size=1772x886)
Group('Design' size=1426x570)
Group('Images' size=1226x1196)
Group('Text' size=1627x443)
Group('Logo' size=386x107)

If you want to access all layers

All layers including folders can be accessed by using the descendants () method

for layer in list(psd.descendants()):
    print(layer)
Group('Background' size=1772x886)
SolidColorFill('Background' size=1772x886 mask effects)
Group('Design' size=1426x570)
SmartObjectLayer('Brush Stroke' size=1426x413 effects)
ShapeLayer('Rectangle' size=460x80 effects)
Group('Images' size=1226x1196)
SmartObjectLayer('Waffel' size=675x954 effects)
SmartObjectLayer('Food' size=23x27 effects)
SmartObjectLayer('Food' size=23x27 effects)
SmartObjectLayer('Food' size=27x27 effects)
SmartObjectLayer('Tomato Sauce' size=211x208 effects)
SmartObjectLayer('Tomato' size=237x239 effects)
SmartObjectLayer('Orange' size=276x272 effects)
Group('Text' size=1627x443)
TypeLayer('Title' size=506x122)
TypeLayer('20% OFF' size=441x96)
TypeLayer('Voucher ID #56893' size=379x30)
TypeLayer('Web' size=728x27)
TypeLayer('Address')
Group('Logo' size=386x107)
SmartObjectLayer('Place Your Logo Here (Double Click to Edit)' size=386x107)

Convert all layers to PIL.Image and save as layer name


    for layer in list(psd.descendants()):
        print("layer_name: ", layer.name)
        print("is_group(): ", layer.is_group())

        if not layer.is_group():
            pil_img = layer.topil()
            pil_img.save(layer.name + ".png ")

▼ Result Waffel.png Orange.png Brush Stroke.png Tomato.png Tomato Sauce.png Title.png Background.png 20% OFF.png Web.png Voucher ID #56893.png Food.png

Recommended Posts

About psd-tools, a library that can process psd files in Python
Try using APSW, a Python library that SQLite can get serious about
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
Published a library that hides character data in Python images
From a book that programmers can learn ... (Python): About sorting
A set of script files that do wordcloud in Python3
A note about mock (Python mock library)
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
Created a library for python that can easily handle morpheme division
A story about a build error in a shared library that references libusb
Introducing a library that was not included in pip on Python / Windows
Code reading of faker, a library that generates test data in Python
[Python] Get the files in a folder with Python
A memo that I wrote a quicksort in Python
A program that removes duplicate statements in Python
A memo about writing merge sort in Python
Data analysis in Python: A note about line_profiler
Think about building a Python 3 environment in a Mac environment
A memorandum about the Python tesseract wrapper library
I want to create a priority queue that can be updated in Python (2.7)
I made a familiar function that can be used in statistics with Python
How to install a Python library that can be used by pharmaceutical companies
Code reading for m3u8, a library for manipulating HLS video format m3u8 files in Python
[Python] Creating a tool that can list, select, and execute python files with tkinter & about the part that got caught
A note on the library implementation that explores hyperparameters using Bayesian optimization in Python
Daemonize a Python process
From a book that programmers can learn ... (Python): Pointer
What's in that variable (when running a Python script)
Created a Python library DateTimeRange that handles time ranges
In Python, create a decorator that dynamically accepts arguments Create a decorator
Can be used in competition pros! Python standard library
[Python] A convenient library that converts kanji to hiragana
About __all__ in python
A reminder about the implementation of recommendations in Python
A mechanism to call a Ruby method from Python that can be done in 200 lines
Publish / upload a library created in Python to PyPI
MALSS, a tool that supports machine learning in Python
Qt-based library "Poppler" that can quickly read PDF as an image in C ++ or Python
How to set up a simple SMTP server that can be tested locally in Python
A Python program in "A book that gently teaches difficult programming"
A function that divides iterable into N pieces in Python
Loop through a generator that returns a date iterator in Python
A story about how to specify a relative path in python.
Let's create a script that registers with Ideone.com in Python.
From a book that programmers can learn (Python): Decoding messages
Developed a library to get Kindle collection list in Python
I tried "a program that removes duplicate statements in Python"
Process the files in the folder in order with a shell script
Create code that outputs "A and pretending B" in python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
Try building a neural network in Python without using a library
Scripts that can be used when using bottle in Python
A story about trying to implement a private variable in Python.
About netaddr that operates IP address, CIDR, etc. in Python
Create a function in Python
Create a dictionary in Python
Overriding library functions in Python
A memorandum about correlation [Python]
Make a bookmarklet in Python
A memorandum about Python mock
Draw a heart in Python