Using imagemagick in script under Mac

Post Reply
freakpazo
Newbie
Posts: 9
Joined: Fri Nov 03, 2023 3:46 pm

Using imagemagick in script under Mac

Post by freakpazo »

Hello,

I am trying to acess imagemagick from switch script but I am getting "command not found" error.
My goal is to convert psd to png and I am using exec from "child_process" alongside promisify from "util".
Since the logic of my script is complicated I created a dummy script with only 1 purpouse to convert a psd to png.
I made sure that the imagemagick is installed correctly on the machine.
I made sure that the exact same script is running properly on Windows environment.

The error occurs only under Mac(I am testing with M1 chip)

Things I tried that work properly
- Running exact same Switch script under windows
- Running independant(outside of Switch) node.js script with the same code(to make sure that my imagemagick is installed correctly on the machine)
- Running the command in terminal to double check that imagemagick is installed correctly

Here is the dummy script I am testing with:

Code: Select all

import { exec } from 'child_process';
import { promisify } from 'util';

async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
  const filePath = await job.get(AccessLevel.ReadOnly);
  if(filePath.endsWith('.psd')) {
    await flowElement.log(LogLevel.Info, 'Before convert');
    const execAsync = promisify(exec);
    const command = `magick convert "${filePath}" "${filePath.split('.')[0]}.png"`;
    const { stdout, stderr } = await execAsync(command);
    if (stderr) {
      await flowElement.log(LogLevel.Error, `Error: ${stdout}`);
      console.error('Error:', stderr);
    } else {
      await flowElement.log(LogLevel.Info, `Image converted successfully sending to ouput: ${stdout}`);
    }
    await flowElement.log(LogLevel.Info, 'After convert');
    await job.sendToSingle();
  } else {
    await flowElement.log(LogLevel.Info, 'Sending converted file to output');
    await job.sendToSingle();
  }
}
freddyp
Advanced member
Posts: 1024
Joined: Thu Feb 09, 2012 3:53 pm

Re: Using imagemagick in script under Mac

Post by freddyp »

The environment in which your script is running is not the same as the one in Terminal/Command prompt. It is not a guarantee that 'magick' is a known command in that environment. Use the complete path.
freakpazo
Newbie
Posts: 9
Joined: Fri Nov 03, 2023 3:46 pm

Re: Using imagemagick in script under Mac

Post by freakpazo »

I tried that and I am still getting an error.
Imagemagick is installled using homebrew on M1 so it is located at /opt/homebrew/bin/magick
Just to make sure I also tried independent script using the full path(which works fine) and using the old path /usr/local/bin/magick(just in case)
freddyp
Advanced member
Posts: 1024
Joined: Thu Feb 09, 2012 3:53 pm

Re: Using imagemagick in script under Mac

Post by freddyp »

The error is "command not found". As it works on Windows but not on Mac I suggest to check if the magick file under /opt/homebrew is not a symbolic link or shortcut. If it is then try it with the real path. If that still does not work you will have to be a bit more specific on the error message and the exit status you receive.

Before you do that you should address two basic problems with the script.

You MUST NOT use the path to an input job file to create a new file because you are creating something that Switch could pick up as a new job. When the input job is a folder you can create files inside that folder without the risk of confounding Switch. It is clear that your code expects a PSD file as input, so you have to work with a temporary file path to create the PNG file.

When you create a new file you have to create a child job pointing to its path, send the child job and send the original job to null. With your current code you will always end up with the input job as a result.
freakpazo
Newbie
Posts: 9
Joined: Fri Nov 03, 2023 3:46 pm

Re: Using imagemagick in script under Mac

Post by freakpazo »

Using the real path hepled. Thank you.
Regarding the other two issues - I created this dummy script just so I can show the error in the simplest way but thank you for the input
tdeschampsBluewest
Member
Posts: 37
Joined: Tue Jun 01, 2021 11:57 am

Re: Using imagemagick in script under Mac

Post by tdeschampsBluewest »

Hi, c
Instead of using "child_process" alongside promisify, i could suggest you tu use library cross-spawn (https://www.npmjs.com/package/cross-spawn)

The command is quite simple :

Code: Select all

const args:string[] = [...]
const result = spawn.sync('magick', args, { stdio: 'inherit' });
Post Reply