I decided to pony up for the paid version of ChatGPT, which includes access to the GPT-4 model. And I’m glad I did, it’s code-generation is exponentially better than GPT 3.5. I’ve had it writing all kinds of things for me in various languages, including Xojo.
For example, I asked it to write me a Xojo method that would extract the individual layers from a Photoshop PSD file as PNG files on MacOS, here’s what it gave me:
To extract the layers of a PSD file into individual PNG files using Xojo, you’ll need a third-party library, as Xojo does not have built-in support for handling PSD files. The ImageMagick library is a powerful tool that can be used for this purpose.
To use the ImageMagick library with Xojo on macOS, you’ll first need to install the library. You can use Homebrew to install it:
brew install imagemagick
Then, add a declare statement to call the ImageMagick library. Here’s a Xojo method that extracts the layers of a PSD file and saves them as individual PNG files:
Function ExtractPSDLayersToPNGs(psdFilePath As String, outputFolder As String) As Boolean
Declare Function MagickWandGenesis Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickWandGenesis" () As Integer
Declare Function NewMagickWand Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "NewMagickWand" () As Ptr
Declare Function MagickReadImage Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickReadImage" (wand As Ptr, filename As CString) As Integer
Declare Function MagickSetIteratorIndex Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickSetIteratorIndex" (wand As Ptr, index As Integer) As Integer
Declare Function MagickGetNumberImages Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickGetNumberImages" (wand As Ptr) As Integer
Declare Function MagickWriteImage Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "MagickWriteImage" (wand As Ptr, filename As CString) As Integer
Declare Sub DestroyMagickWand Lib "/opt/homebrew/Cellar/imagemagick/7.1.1-6/lib/libMagickWand-7.Q16HDRI.dylib" Alias "DestroyMagickWand" (wand As Ptr)
var NulInt as integer
NulInt = MagickWandGenesis()
Var wand As Ptr = NewMagickWand()
Var readResult As Integer = MagickReadImage(wand, psdFilePath)
If readResult = 0 Then
MessageBox("Failed to read the PSD file. Make sure the file path is correct.")
DestroyMagickWand(wand)
Return False
End If
Var layerCount As Integer = MagickGetNumberImages(wand)
For layerIndex As Integer = 0 To layerCount - 1
NulInt = MagickSetIteratorIndex(wand, layerIndex)
Var outputPath As String = outputFolder + "/Layer_" + Str(layerIndex + 1) + ".png"
NulInt = MagickWriteImage(wand, outputPath)
Next
DestroyMagickWand(wand)
Return True
End Function
This method initializes the ImageMagick library, reads the PSD file, iterates through its layers, and saves each layer as a separate PNG file in the specified output folder. Note that you should adjust the library name and location depending on your platform and configuration.
Usage example:
Var psdFilePath As String = "C:/path/to/your/psd/file.psd"
Var outputFolder As String = "C:/path/to/output/folder"
If ExtractPSDLayersToPNGs(psdFilePath, outputFolder) Then
MessageBox("PSD layers have been successfully extracted to individual PNG files.")
Else
MessageBox("Failed to extract PSD layers.")
End If
This code all just worked “out of the box” and required no modifications. It’s not very complicated, it’s just calling some dylibs, but even the more tedious string parsing and database code I’ve asked it for has come out mostly perfect, with only minor corrections needed here and there.
It’s all quite scary, really, for the obvious reasons of career disruption. But I’m blown away at how productive it’s making me for now.