Image Magick and LiveCode

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Image Magick and LiveCode

Post by hairydalek » Wed Mar 02, 2016 5:30 pm

Hi,
moving on slightly from getting LC to process images, I thought I’d try using ImageMagick. I’ve downloaded and put it on my Mac, and tested it, and it works fine (I’m using covert to apply a blur to an image). However, LiveCode really doesn’t like it.

Code: Select all

put appTemporaryFolder & "/dummy.png" into temporaryFile
put image "image" of group "my group"  into url ("binfile:" & temporaryFile)

set the defaultFolder to appTemporaryFolder
put "~/ImageMagick-6.9.3/bin/convert" into myCommand
put myCommand &&  "dummy.png" into myCommand
put myCommand && "-radial-blur 20" into myCommand
put myCommand && "dummy.png" into myCommand
put shell(myCommand)
If I run the above, a file called dummy.png is written to the Temporary folder. I then get the following error in the message window:
dyld: Library not loaded: /ImageMagick-6.9.3/lib/libMagickCore-6.Q16.2.dylib
Referenced from: /Users/paul/ImageMagick-6.9.3/bin/convert
Reason: image not found
/bin/sh: line 1: 23110 Trace/BPT trap: 5 ~/ImageMagick-6.9.3/bin/convert dummy.png -radial-blur 20 dummy.png
If I use this to call the convert tool:

Code: Select all

put appTemporaryFolder & "/dummy.png" into temporaryFile
put image "image" of group "my group"  into url ("binfile:" & temporaryFile)

set the defaultFolder to appTemporaryFolder
put "convert" into myCommand
put myCommand &&  "dummy.png" into myCommand
put myCommand && "-radial-blur 20" into myCommand
put myCommand && "dummy.png" into myCommand
put shell(myCommand)
I get this error:
/bin/sh: line 1: convert: command not found
So, providing the path to the convert command fails because it seems that an environment variable is not read: dyld: Library not loaded: /ImageMagick-6.9.3/lib/libMagickCore-6.Q16.2.dylib seems to be missing the path to the folder. I expect “convert” isn’t working in the second example because the environment variables are not seen. These were set as per instructions on the ImageMagick page here: http://www.imagemagick.org/script/binary-releases.php (I’m using the Mac OSX Binary release), and if I use Terminal to show them, they are there.

I am thinking ahead. If I release my application into the wild, this will kill it. I can guarantee that this will throw people, so I’ll want to bundle ImageMagick with the application - which means including using Stand alone Application Settings...>Copy Files and then calling the scripts included in the folder copied with (or in - if you are a Mac person) the application. I’ve done this wth another app, and it’s quite successful (people using command line tools without realising it!), so I’m looking at doing the same with this.

I did try this kind of thing to see if I can get the path to stick, but no luck:

Code: Select all

put tPath & "/Support/includes/Mac/ImageMagick" into imageMagick
put  "export MAGICK_HOME=" & quote & imageMagick & quote into myCommand
(tPath is the path to the stack)

So, how do I get this working? I’ve been looking for ImageMagick in LiveCode instructions, and while I have seen that some people have managed to get it working, I’ve not been able to find out how - usually its seems that any query involving IM seems to follow getting it working successfully in LC. Thanks.
Last edited by hairydalek on Wed Mar 02, 2016 9:14 pm, edited 1 time in total.

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Wed Mar 02, 2016 9:14 pm

<Time passes>

I managed to get it working on one of my Macs. I installed IM using MacPorts, and located the convert tool. If I give the full path to convert, it all seems to work.
put "/opt/local/bin/convert" into myCommand
It seems that in order to work on a Mac, IM also requires X11 to be present. It seems to be fine with what’s I have above, but if I want to include it in the Application, then it’s a problem. Hmm.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Image Magick and LiveCode

Post by [-hh] » Thu Mar 03, 2016 9:55 pm

Hi,
often "convert" is on MacOS located in /usr/local/bin.

One has to watch LC's $PATH ...
So add this to the preopenstack handler of your first card

Code: Select all

on preopenstack
  if "/usr/local/bin" is not in $PATH -- or "/opt/local/bin" in your case
  then put ":/usr/local/bin" after $PATH
end preopenstack
This works here, also with other progs, for example the much faster graphicsmagick ("gm").

Hermann
shiftLock happens

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Fri Mar 04, 2016 11:38 am

[-hh] wrote:Hi,
often "convert" is on MacOS located in /usr/local/bin.

One has to watch LC's $PATH ...
So add this to the preopenstack handler of your first card

Code: Select all

on preopenstack
  if "/usr/local/bin" is not in $PATH -- or "/opt/local/bin" in your case
  then put ":/usr/local/bin" after $PATH
end preopenstack
This works here, also with other progs, for example the much faster graphicsmagick ("gm").

Hermann
Thanks. Is there a way of bundling GraphicsMagick with the stack when it’s compiled as an application. I’ve done this with ExifTool, and that works, but GraphicsMagick seems to rely on a lot of dependencies, which are referenced using a fixed path (eg to libpng) which you can define when it’s built. One it’s built, if you move GM elsewhere, it loses sight of the libraries (obviously). Failing that, a really, really idiot proof way of installing it + dependencies. I have a folder with GM and libpng installed in it - it works fine if you don’t move or rename the folder. It will, obviously, change when the app is built and sent elsewhere.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Image Magick and LiveCode

Post by [-hh] » Fri Mar 04, 2016 1:14 pm

Never tried that but I am also interested.

Did you already try copyFiles in the standalone settings?

I'll have this weekend some time for looking into the graphicsmagick source code, to see what's used.
shiftLock happens

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Fri Mar 04, 2016 1:23 pm

[-hh] wrote:Never tried that but I am also interested.

Did you already try copyFiles in the standalone settings?

I'll have this weekend some time for looking into the graphicsmagick source code, to see what's used.
HI - didn’t get as far as copyFiles. Just renaming the folder I installed into broke it when I tried things in Terminal. So didn’t go much further. Looking at what’s written out in the gm file, I see that an absolute path to libraries is used. I did try rebuilding specifying relative paths for the library, but that failed at the make stage. I kind of guessed that it would, but you never know if things will magically start to work.

Code: Select all

./configure LDFLAGS="-L../libpng/lib/" CPPFLAGS="-I../libpng/include/" --prefix=/Users/paul/Desktop/gm
(I was building GraphicsMagick into a folder called gm, and libpng into gm/libpng - make didn’t like that there were no absolute paths.)

Just to test if it would do what I needed, I did install it using MacPorts. Unfortunately, there are a few things missing that ImageMagick has that I need (creating alpha channels, radial blurs and other such things), so right now, GraphicsMagick isn’t the one for me. I’ll be sticking with ImageMagick. I know it’s slower, but it does more.

Thanks.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Image Magick and LiveCode

Post by [-hh] » Sat Mar 05, 2016 4:07 am

I wonder a little bit because it is said that graphicsmagick has less dependencies than imagemagick.
Anyway you are right with the available options where imagemagick has still more.

Please let me know if and how you succeeded in attaching IM to a standalone.

-hh

p.s. I saw detailed information about PATHs on
http://www.graphicsmagick.org/INSTALL-w ... nformation
what may be useful for other OS too.
shiftLock happens

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Sun Mar 06, 2016 9:50 pm

[-hh] wrote: Please let me know if and how you succeeded in attaching IM to a standalone.

-hh
I believe I have succeeded. For the record: I’m very much in the dark when it comes to stuff like compiling and building apps. It doesn’t help that instructions are written for people who know what they are doing. Anyway, I spent a good chunk of my free time on Saturday trying to get this to work. I’m afraid that I’ve not gone that deep down that rabbit hole, and I’m not really keen to do it again.

What I do know:
• ImageMagick needs various dependencies (or delegates) to work. Some of these are in the X11 environment. Some are for PNG and JPEG file handling
• It HAS to be possible to build it into a truly standalone portable application as I know it’s used in some Photoshop filters, and they don’t direct you to the Terminal to install them
• Windows users have it easy. They get a nicely packaged .exe file with the dependencies included, and from what I gather from posts on the IM website forum, that facility won’t be extended to Mac users.

So, I kept on looking, trying builds, looking and eventually I found this page:
https://forums.adobe.com/thread/1146055?tstart=0
The Correct Answer by johnrellis is the one I followed. It’s not written very nicely, but if you follow the instructions you should get a portable IM build out of it. You will need to change the name of the prefix attribute to the folder you are building ImageMagick in, but it seemed to work. All delegates/dependencies built in. No breaking because it can’t find libpng in somewhere like/etc/bin/

I am afraid that it’s very much been a case of following instructions but not understanding everything I’m doing. It would be really helpful if someone who can translate Geek to Human can write this up more clearly as I was not quite sure how all this works, and I expect it would be of benefit to others here to have more LiveCode friendly instructions.

Anyway, I include a stack. I can’t upload it directly to the forum, so it’s in my DropBox folder. It’s for Mac only use because that’s the problematic one (though it would be easy to add the Windows .exe and include code for that in the stack). I expect it may be of help to Linux folk too - but I’ve not tested against that.

https://dl.dropboxusercontent.com/u/613685/Graphics.zip

Take note of the support folder that’s included. The contains just the tools that is likely to be used. The folder structure seems to require the etc folder with its contents if you don’t want warnings thrown at you (this is actually in the instructions linked to above, but no path structure given so I had to do more digging).

If you run the stack, or run as an application, you’ll see a window with two boxes. One should contain a picture of a camera. Click on the Test! button and the image will be processed, and a black & white image will appear in the empty box. This has been processed by IM’s Convert tool. The Reset button resets the images, as well as setting path variables (this was for my benefit). You need to include the path to the various tools, so that’s constructed when the stack is opened (or the Reset button clicked).

I have tested this on my wife’s Mac just to be sure it works. Mine has ImageMagick already onboard, but her’s has no such pollution, so I’m pretty sure that if it works on that it should work elsewhere. I’ve not been able to verify this outside of my home.

I hope this works for others, and it’s helpful overall. It speeds up image processing a lot. The stack I’ve linked to has the IM tools that I think I’ll be using. My next target will be to apply what I have learned here to my project, but it will be worth knowing if this works for others too.

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Mon Mar 07, 2016 1:12 pm

GAH! Annoyances.

I moved my stack - which I linked to - to my main Mac (been working my laptop over the weekend). Does it work? Yes. Any errors? No. Excellent.

So I copy the code I was using to the project I want to use it with. I copy the ImageMagick files I had built and the scripts to reference the new project - the file paths are the same from the stack. Everything as far as this is concerned seems the same in both projects. Does it work? No - I get delegate errors (basically telling me that the .png libraries that are not available), and PNG files won’t load or save - so nothing gets processed. I absolutely no idea why.

I need to investigate this more, but it’s frustrating me once again. I was hoping to be able to crack on with what I want to do, but I can’t.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Image Magick and LiveCode

Post by [-hh] » Mon Mar 07, 2016 9:46 pm

Hi Paul,
great work!!
It works here, it's simply a path problem (not yet fully soved, remaining is a minor problem).
What I changed:
Added a field, then

Code: Select all

-- in button "Test"
  get shell(myCommand)
  put cr & the result & cr & it after fld 1

-- in setUpImageMagick
  set itemdelimiter to slash
  if the environment is "development" then
    put "/support/Mac/" after tPath 
  else put "MacOS/support/Mac/" into item -2 to -1 of tPath
  replace "//" with "/" in tPath --> not sure if needed, see from fld 1
  set itemdelimiter to comma
  put tPath into myLocation
  put myLocation into fld 1
You have still to solve the location for the file "delegates.xml", I must stop now (but I come back).

Hermann
shiftLock happens

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Tue Mar 08, 2016 10:16 am

[-hh] wrote:Hi Paul,
great work!!
It works here, it's simply a path problem (not yet fully soved, remaining is a minor problem).
What I changed:
Added a field, then

Code: Select all

-- in button "Test"
  get shell(myCommand)
  put cr & the result & cr & it after fld 1

-- in setUpImageMagick
  set itemdelimiter to slash
  if the environment is "development" then
    put "/support/Mac/" after tPath 
  else put "MacOS/support/Mac/" into item -2 to -1 of tPath
  replace "//" with "/" in tPath --> not sure if needed, see from fld 1
  set itemdelimiter to comma
  put tPath into myLocation
  put myLocation into fld 1
You have still to solve the location for the file "delegates.xml", I must stop now (but I come back).

Hermann
Thanks - you’ll notice, though, that in MacOS/support/Mac/ there’s just the folder structure. No files. That’s why I was using special folder("resources") as that’s where they are put.

Still don’t know why this works in the stack I’ve uploaded, and yet it doesn’t work in my project, despite the code doing the same things (and the files being in the same place). I’ve been trying to build ImageMagick again - but the png delegate keeps being left off - and I have no idea why. I’m following the instructions, but it’s not going in. Ho hum. I’m waiting for some replies on their forum about this and how to build a stand alone version. It keeps referencing png libraries elsewhere on my Mac, and not the one I’m telling it to reference.

This is the frustrating part of all of this. I know this can be done, and people keep mentioning that it can, but there’s nothing that seems to be definitive.

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Tue Mar 08, 2016 1:22 pm

I have something now that promises to be more agreeable. It works outside of a LC stack - but I’ve not tried it in one yet. That will be for later. I have other things to be getting on with now.

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Tue Mar 08, 2016 2:53 pm

It seems to be working - certainly it does with the test project (not tried it anywhere else yet - but hopefully this will be all that’s needed). I’ve updated the LiveCode project to reflect these changes.

https://dl.dropboxusercontent.com/u/613685/Graphics.zip

It’s a bit bigger than it was - but that’s because it has the most of the ImageMagick output in with it. I’m not sure what can be safely jettisoned at the moment - but I think there’s a lot of dependencies in the other folders. If there is a way to build just the tools I need, then I’ll do that - but that’s for another day.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Image Magick and LiveCode

Post by [-hh] » Sun Mar 13, 2016 9:24 pm

[Sorry for the late answer, my notifications are broken.]

Thanks for sharing this. It works here also (yet it needs a *very* long time for compiling, both in LC 7/8).
Did you notice that they changed locations for the copy-files?
• In LC 7 it is the location you describe: "Contents/Resources/_MacOS/support",
• in LC 8 it is the location I used: "Contents/MacOS/support".
and the other locaton in each case contains the folder structure only.
But the standalone works also with a LC8 compilation from the unchanged settings of your input stack(+support).
shiftLock happens

hairydalek
Posts: 57
Joined: Sat Feb 01, 2014 8:57 pm

Re: Image Magick and LiveCode

Post by hairydalek » Sun Mar 13, 2016 10:09 pm

[-hh] wrote:[Sorry for the late answer, my notifications are broken.]

Thanks for sharing this. It works here also (yet it needs a *very* long time for compiling, both in LC 7/8).
Did you notice that they changed locations for the copy-files?
• In LC 7 it is the location you describe: "Contents/Resources/_MacOS/support",
• in LC 8 it is the location I used: "Contents/MacOS/support".
and the other locaton in each case contains the folder structure only.
But the standalone works also with a LC8 compilation from the unchanged settings of your input stack(+support).
I've not really used LC8. I tried it, but there were bugs that stopped my app working, so I reverted to LC7. Something about an incomplete properties palette - not all the necessary options were being presented by LC8.

The long time for compiling is down to the large number of files that ImageMagick creates. I'll have a look to see if that can be reduced. There's all the documentation that probably can go. It may be that all I need are the binaries - that will need a little investigation. It's a shame that I can't create single executable like you can for Windows. That would make things so more efficient.

Sadly, I'm a Mac down at the moment. My MBP fell foul of an issue for which there's a recall notice, so it's away being assessed and (hopefully) repaired at Apple's expense, so I can't casually tinker with this at the moment. Ho hum.

Glad this is helpful to others. If I can trim some of the fat, I'll update the download.

Post Reply

Return to “Talking LiveCode”