Application in Go

See the entire codebase here on GitHub

Purpose and overview

I wanted to use my plug-in without opening up Gimp myself, selecting the plug-in from the menu and setting all the parameters manually, so I created this application to automate this. That way, I have a fully automated process which would handle everything for me. The application opens up Gimp automatically in the background and runs my plug-in for the given image. Parameters such as background color, padding and export path are all handled by the app, and I got it hardcoded as I have one style for my images at the moment. This could be improved on in the future if there is a need for dynamically changing them when using the application. Here is a quick overview of the application:

  • An image can be dropped onto the app icon or selected using a file dialog. Only jpg files are supported.
  • Creates an output folder on the desktop if it doesn't already exist.
  • Sets a new image name and ensures there isn't already an image with the same name in the output folder. If there is, the name is appended with number.
  • Runs a command to open GIMP in the background and use the plug-in.
  • Saves the new image in the output directory.
  • Uses toast notifications to inform the user of ongoing actions.

Key parts of the application


func getImagePath() (string, error) {
 argumentsPassed := len(os.Args)

 if argumentsPassed > 2 {
  return "", errors.New("Dropping multiple files is not supported")
 }

 if argumentsPassed > 1 {
  // File drop
  return os.Args[1], nil
 }

 // File dialog
 imagePath, err := dialog.File().Filter("Images", "jpg", "jpeg").Load()

 if err != nil {
  return "", err
 }

 return imagePath, nil
}
                                
Handling a drag and drop of an image or opening a file dialog if a user runs the application. If more than one picture is dropped, a toast notification with an error message appears.

func setFileExportPath(overlayImagePath string) (string, error) {
 homeDir, err := os.UserHomeDir()
 if err != nil {
  return "", err
 }

 outputFolder := filepath.Join(homeDir, "Desktop", "InstaPictures")

 if _, err := os.Stat(outputFolder); os.IsNotExist(err) {
  err = os.MkdirAll(outputFolder, 0755)
  if err != nil {
   return "", err
  }
 }

 overlayImageExt := filepath.Ext(overlayImagePath)
 baseName := strings.TrimSuffix(filepath.Base(overlayImagePath), overlayImageExt)
 outputImageName := baseName + "-output" + overlayImageExt
 exportPath := filepath.Join(outputFolder, outputImageName)
 return ifPathExistsReturnNewValidOne(exportPath), nil
}
                                
Setting file export path. Checking if the output folder already exists and creating one if it doesn't. Also, making sure that the final image name is unique, so there is no way of overriding images by accident.

func runGimpPlugin(padding, r, g, b int, imageOverlayPath, exportPath string) {
 batchCmd := fmt.Sprintf(`pdb.python_fu_create_new_insta_image(%d, %d, %d, %d, "%s", "%s")`,
  padding, r, g, b,
  strings.ReplaceAll(imageOverlayPath, `\`, `/`),
  strings.ReplaceAll(exportPath, `\`, `/`))

 cmd := exec.Command("gimp-2.10", "--batch-interpreter=python-fu-eval", "-i", "-b", batchCmd)

 // Hide GIMP console window on Windows
 cmd.SysProcAttr = &syscall.SysProcAttr{
  HideWindow: true,
 }

 cmd.Run()
}
                                
Executing the command which spins up Gimp and runs the plug-in.

Application demo

gimp1
File dialog
gimp2
Drag and drop file
An unhandled error has occurred. Reload 🗙