|
| 1 | +"""This example demonstrates how to export a document with different formats. |
| 2 | +
|
| 3 | +References: |
| 4 | + https://github.com/loonghao/photoshop-python-api/issues/368 |
| 5 | +""" |
| 6 | + |
| 7 | +# Import built-in modules |
| 8 | +import os |
| 9 | + |
| 10 | +# Import third-party modules |
| 11 | +from photoshop import Session |
| 12 | +from photoshop.api.enumerations import DitherType |
| 13 | +from photoshop.api.enumerations import ExportType |
| 14 | +from photoshop.api.enumerations import SaveDocumentType |
| 15 | +from photoshop.api.save_options.png import PNGSaveOptions |
| 16 | +from photoshop.api.save_options.jpg import JPEGSaveOptions |
| 17 | +from photoshop.api.save_options.png import ExportOptionsSaveForWeb |
| 18 | + |
| 19 | + |
| 20 | +def main(): |
| 21 | + """Export document with different formats.""" |
| 22 | + psd_file = os.path.join(os.path.dirname(__file__), "files", "export_layers_as_png.psd") |
| 23 | + if not os.path.exists(psd_file): |
| 24 | + raise FileNotFoundError( |
| 25 | + f"File not found: {psd_file}" |
| 26 | + ) |
| 27 | + |
| 28 | + # Start a new photoshop session |
| 29 | + with Session(psd_file, "open") as ps: |
| 30 | + doc = ps.active_document |
| 31 | + |
| 32 | + # Export as PNG-24 |
| 33 | + png_path = os.path.join(os.path.dirname(__file__), "exported_png24.png") |
| 34 | + png_options = PNGSaveOptions() |
| 35 | + png_options.interlaced = False # Disable interlacing for better quality |
| 36 | + png_options.compression = 0 # Set compression to 0 for maximum quality |
| 37 | + doc.saveAs(png_path, png_options, True) # True for saving as copy |
| 38 | + print(f"Exported PNG-24: {png_path}") |
| 39 | + |
| 40 | + # Export as JPEG with high quality |
| 41 | + jpg_path = os.path.join(os.path.dirname(__file__), "exported_jpeg.jpg") |
| 42 | + jpg_options = JPEGSaveOptions() |
| 43 | + jpg_options.quality = 12 # Set quality to maximum (12) |
| 44 | + jpg_options.embedColorProfile = True # Preserve color profile |
| 45 | + jpg_options.formatOptions = 1 # Use standard baseline format |
| 46 | + jpg_options.scans = 3 # Enable progressive scanning |
| 47 | + jpg_options.matte = 1 # No background color (matte) |
| 48 | + doc.saveAs(jpg_path, jpg_options, True) # True for saving as copy |
| 49 | + print(f"Exported JPEG: {jpg_path}") |
| 50 | + |
| 51 | + # Export as GIF using Save for Web |
| 52 | + gif_path = os.path.join(os.path.dirname(__file__), "exported_gif.gif") |
| 53 | + gif_options = ExportOptionsSaveForWeb() |
| 54 | + gif_options.format = SaveDocumentType.CompuServeGIFSave # Set format to GIF |
| 55 | + gif_options.colors = 256 # Use maximum number of colors (256) |
| 56 | + gif_options.dither = DitherType.NoDither # Disable dithering for sharper edges |
| 57 | + gif_options.transparency = True # Preserve transparency in the GIF |
| 58 | + doc.exportDocument(gif_path, ExportType.SaveForWeb, gif_options) |
| 59 | + print(f"Exported GIF: {gif_path}") |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + main() |
0 commit comments