Convert psd file to png in Python

Use kmike / psd-tools. First, pip the following.

pip install psd-tools
pip install Pillow
pip install packbits

I'd be happy if you could specify the path from the command line and convert it, so make it that way.

from psd_tools import PSDImage
import sys
import os

def isfilepath(path):
    print(path)
    return os.path.isfile(os.path.abspath(os.path.expanduser(path)))

def ispsd(path):
    if path[-3:] == 'psd':
        return True
    return False

def pngpath(path):
    return path[:-3] + 'png'

file_paths = [file_path for file_path in sys.argv if isfilepath(file_path) and ispsd(file_path)]
for file_path in file_paths:
    psd = PSDImage.load(file_path)
    merged_image = psd.as_PIL()
    merged_image.save(pngpath(file_path))

Save this as psdtopng.py and hit it from the command line.

python psdtopng.py ~/Desktop/hoge.psd

Then, ~ / Desktop / hoge.png is output: congratulations:

sqrtxx/psdtopng

Recommended Posts

Convert psd file to png in Python
Convert svg file to png / ico with Python
Convert markdown to PDF in Python
Convert Excel file to text in Python for diff purposes
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Convert HEIC files to PNG files with Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
File operations in Python
File processing in Python
File operations in Python
Convert FBX files to ASCII <-> BINARY in Python
Convert PDF to image (JPEG / PNG) with Python
Convert PDFs to images in bulk with Python
[Python] Convert from DICOM to PNG or CSV
From file to graph drawing in Python. Elementary elementary
How to create a JSON file in Python
How to convert SVG to PDF and PNG [Python]
Batch convert PSD files in directory to PDF
Convert exponential notation float to str in Python
Sample to put Python Kivy in one file
Convert cubic mesh code to WKT in Python
Convert timezoned date and time to Unixtime in Python2.7
How to convert / restore a string with [] in python
[python] Convert date to string
To flush stdout in Python
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Download the file in Python
Convert NumPy array "ndarray" to lilt in Python [tolist ()]
Convert HTML to text file
Convert CIDR notation netmask to dotted decimal notation in Python
Login to website in Python
A memorandum to run a python script in a bat file
How to convert floating point numbers to binary numbers in Python
Convert the image in .zip to PDF with Python
I want to randomly sample a file in Python
How to convert JSON file to CSV file with Python Pandas
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert callback-style asynchronous API to async / await in Python
Convert CIDR notation in Python
Speech to speech in python [text to speech]
Convert / return class object to JSON format in Python
[Python] Created a method to convert radix in 1 second
[Work efficiency] How to change file names in Python
How to develop in Python
Convert Webpay Entity type to Dict type (recursively in Python)
Convert genbank file to gff file
Convert python 3.x code to python 2.x
Post to Slack in Python
Assigned scaffolding macro in Python script file to F12 key
How to import a file anywhere you like in Python
Convert images passed to Jason Statham-like in Python to ASCII art
[Python] How to change EXCEL file saved in xlsb to xlsx
File / folder path manipulation in Python
[Python] Write to csv file with Python
How to collect images in Python
Linebot creation & file sharing in Python
How to use SQLite in Python