libtiff error in Matlab when attempting to use ImageMagick

Trying to use the Imagemagick toolbox (for me, installed via MacPorts) sometimes doesn’t work (for example, converting a png image) in Matlab because Matlab for some reason uses the libraries in its path over those given in system call (this is for Mac, but probably for *nix and maybe even Windows).

This is an example call:

[s,w] = system([IMAGEMAGICK_PATH,'convert ',pwd,'/',outfile,'.png -rotate 90 ',pwd,'/',outfile,'.png']);

This is the error:

dyld: Library not loaded: /opt/local/lib/libtiff.3.dylib
Referenced from: /opt/local/bin/convert
Reason: Incompatible library version: convert requires version 12.0.0 or later, but libtiff.3.dylib provides version 11.0.0

The work around is to replace the Matlab library with the ImageMagick one (only need to do this once and will stay until you upgrade Matlab):

cd /Applications/MATLAB74/bin/maci/   (or wherever your Matlab is installed)
mv libtiff.3.7.1.dylib  libtiff.3.7.1.dylib.MATLAB  (give it a different name)
ln -s /opt/local/lib/libtiff.3.dylib libtiff.3.7.1.dylib  (create a soft link to the Imagemagick library file)

EDIT: This is the much better way to handle this:

setenv('DYLD_LIBRARY_PATH',['/opt/local/lib/:' getenv('DYLD_LIBRARY_PATH')])

17 thoughts on “libtiff error in Matlab when attempting to use ImageMagick

  1. I was quite excited when this worked, until I realized that swapping out the dylib breaks imread() with TIF files. See:

    >> f = imread(‘some_tiff_file.TIF’)
    ??? Error using ==> rtifc
    Invalid TIFF image index specified.

    Error in ==> readtif at 32
    [X, map, details] = rtifc(filename, args.index);

    Error in ==> imread at 403
    [X, map] = feval(, filename, extraArgs{:});

    Is this the case for you as well? Or is there something different about our libTIFFs?

    • Yes, same thing happens to me. I was just going to tell you to not use *.tif/*.tiff files since *.png is better, but I spent a few seconds and found out this works.

      1. Do NOT create (or undo) the softlink I describe above
      2. Instead, set the environment path to the MacPorts library

      Here’s a code snip:

      setenv('PATH', ['/opt/local/lib/;' getenv('PATH') ]);
      [s,w] = system(['/opt/local/bin/convert ',pwd,'/foo.png -rotate 90 ',pwd,'/foo2.png']);

  2. Thanks for the reply.

    TIFFs are, for better or for worse, a standard in many scientific disciplines, especially for microscopy and geographic data, so just swapping over to PNG is not an option. The TIFF format also has some unique features, like the way it handles metadata and multiple image frames per file.

    Anyway, the code snip you sent me doesn’t solve the problem, I get the same error whether or not I change the path environment variable.

    In both cases, the error (same as my above post) is:

    dyld: Library not loaded: /opt/local/lib/libtiff.3.dylib

    So I don’t think the path has anything to do with it.

    • Yes, tiff has some ‘features’ (most of them unnecessary in my opinion, but I digress) and seems to be default for many programs (even Mac’s

      OK, I had mistakenly left the soft link from this original post in place when I tested the above. Good news is I have figured it out!:

      Once for your session you need to do this:

      setenv('DYLD_LIBRARY_PATH',['/opt/local/lib/:' getenv('DYLD_LIBRARY_PATH')])

      Then call convert (e.g.):

      [s,w] = system(['/opt/local/bin/convert ',pwd,'/foo.png -rotate 90 ',pwd,'/foo2.png'])

      • worked with Yosemite and Matlab R2011b, i have to run the script every time i open Matlab, but i can live with that. Very helpful and simple reply!

  3. I want to load current libtiff in matlab 2009a running on windows.

    Does it have to be windows compiled libtiff or can I also use linux compiled libtiff, ie DLL or .so file ? Can someone PROFICIENT with this procedure test a version and post instructions that actually work as well as some debugging information ? If libtiff has dependencies, how to debug them or discover them from error messages ?

    Thanks to the star who can help


    • Andy,

      The command I provided above should work to put the path of the library your trying to use before Matlab’s libraries path (of course, your path won’t be /opt/local/lib/). You should also look into the loadlibrary command.

      As of version 7.9 (aka R2009b), there is a new Tiff Class, which acts as a gateway to the LibTIFF libarary. I have that version, and just ran it and got this:

      >> Tiff.getVersion()
      ans =
      LIBTIFF, Version 3.7.1
      Copyright (c) 1988-1996 Sam Leffler
      Copyright (c) 1991-1996 Silicon Graphics, Inc.

      Version 3.7.1 came out in… *gulps* 2004!!

      I tried to load a newer version (I’m on a Mac, so am using the latest from MacPorts) using the loadlibrary command, but ran into 64-bit problems. Hope this info helps you somewhat. If you get really stuck, I’d contact Mathworks support.

  4. Hi admin, plz send me an email. again you did not answer if mixing libraryies of operating systms woud cause problem or not AND you use setenv which is NOT on windows which is my matlab platform. Am I missing sth or you are not reading properly ?

  5. Hi
    I am new to imagemagick, but have been using matlab for some time. I use matlab 7.10.0(R2010a) and installed imagemagick via mac ports, on a mac. To crop images in matlab using imagemagick, I followed thread above.

    when inputting:
    setenv(‘DYLD_LIBRARY_PATH’,[‘/opt/local/lib/:’ getenv(‘DYLD_LIBRARY_PATH’)])

    [s,w] = system(‘/opt/local/bin/convert -crop 400×200+0+0 bb1.tif bb2.tif’);

    I get error:
    dyld: Symbol not found: __cg_jpeg_resync_to_restart
    Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO
    Expected in: /opt/local/lib//libjpeg.8.dylib
    in /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ImageIO.framework/Versions/A/ImageIO

  6. Hi,

    I had the same problem with Student version of Matlab R2011a, and OS X 10.7.2
    None of the solution above worked for me.

    What worked is actually following.

    >> !DYLD_LIBRARY_PATH=”;/opt/local/bin/convert file1.eps file2.png

    You don’t need to change any environmental variables, and symbolic links from default settings. Matlab environmental variable is not altered by this command.
    In the normal command line, DYLD_LIBRARY_PATH is not set, but it is set in MATLAB. By temporarily removing it, convert can load the library from the appropriate path.

    Hope it helps someone.

  7. Hi sorry i just lost this file libtiff.5.dylib!
    i searched a lot to download it but i didnt found it!
    Can you send me the file over email? thnx

    • I can’t because I don’t know what version of that file to send you (i.e. what package was it part of, what operating system are you using, etc.) and there very well may be other things lacking/broken besides just that file. So, instead of looking for just that file, you are much better off re-installing the package it was part of. Do a web search for others having this problem and you may get a solution. You may instead need to relink the file. On my system, that file lives at /usr/local/lib/libtiff.5.dylib (or could be at /opt/local/lib/libtiff.5.dylib) and was installed when I used Homebrew to install the ImageMagick suite of command line tools (via $brew install imagemagick), you maybe used fink or macports and need to reinstall their way.

Leave a Reply to Shinya Cancel reply

Your email address will not be published. Required fields are marked *