Using ImageMagick from Liberty & Just Basic

Image Magick ( & the closely related GraphicMagick) is a major project to create, modify & improve an OpenSource piece of software which creates, modifies and converts images of every conceivable format. With it you can load and save as pdf or ps or png, as well as the space-saving gif & jpg.

Available on Windows & Linux, it is 'driven' by commands ( scripts) from the command line ( Linux terminal). Since the command line is 'run'nable from LB or JB ( & RunBASIC too!), all of ImageMagick's strength is available to the programmer.

I use it to convert image types, so BASIC can load or save more than just bmp images. I use it to rotate images; create animated GIFs. I use it to join images together... etc......

Here is a LB program to load and show a bitmap. It does no resizing. It offers the chance to resave- which will give you again a bmp of the ( probably clipped) original.


    nomainwin

    graphicbox #w.g, 10, 10, 300, 200

    menu #w, "File", "Open", [load], "Save", [save]

    WindowWidth  =330
    WindowHeight =270

    open "Using IM to load and save" for window as #w

    #w, "trapclose [quit]"

    scan
    wait

  [load]
    filedialog "Open bmp", "*.bmp", fileName$
    if fileName$ <>"" then
        loadbmp "image", fileName$
        #w.g, "drawbmp image 0 0"
        #w.g, "flush"
    end if
    wait

  [save]
    filedialog "Save graphic as bmp...", "*.bmp", fileName2$
    if fileName2$ <>"" and fileName2$ <> fileName$ then
        #w.g, "getbmp graphic 1 1 300 200"
        bmpsave "graphic", fileName2$
    end if
    wait

  [quit]
    close #w
    end


Now see how I'd use IM to allow any image to be loaded, sized to fit, and re-saveable under any name.



    nomainwin

    graphicbox #w.g, 10, 10, 300, 200

    menu #w, "File", "Open", [load], "Save", [save]

    WindowWidth  =330
    WindowHeight =270

    open "Using IM to load and save" for window as #w

    #w, "trapclose [quit]"

    scan

    wait

  [load]
    filedialog "Open any image file eg ", "*.gif;*.jpg;*.bmp", fileName$

    if fileName$ <>"" then
        '   load chosen image; remove transparency; resize; save temporarily as bmp.
        '   Note if your directory or file names include spaces you have to add the quote here..
        '       ie put chr$( 34) either side of the file/pathname.
        IM$ ="convert +matte -resize 300x200 " +chr$( 34) +fileName$ +chr$( 34) +" R:\temp.bmp "
        print IM$
        run "cmd.exe /c "; chr$( 34); IM$; chr$( 34), HIDE
        '   Give it 2 seconds to execute (may not be enough for large images)
        timer 2000, [on]
        wait
      [on]
        timer 0

        loadbmp "image", "R:\temp.bmp"
        #w.g, "drawbmp image 0 0"
        #w.g, "flush"

        kill "R:\temp.bmp"
    end if
    wait

    [save]
    filedialog "Save graphic to RamDisk as another image type eg gif, tif, png, jpg, bmp...", "*.*", fileName2$
    if fileName2$ <>"" then
        #w.g, "getbmp graphic 1 1 300 200"
        bmpsave "graphic", "R:\temp.bmp"
        IM$ ="convert R:\temp.bmp "; fileName2$
        print IM$
        run "cmd.exe /c "; chr$( 34); IM$; chr$( 34), HIDE
        '   Give it 2 seconds to execute (may not be enough for large images)
        timer 2000, [on2]
        wait
      [on2]
        timer 0
        kill "R:\temp.bmp"
    end if
    wait

    [quit]
    close #w
    end




The program listed here next is to show how you put together a series of commands to send to IM. It assembles a temporary series of gif images of text at various consecutive rotated positions; strings these together into an animated gif; deletes the intermediate images and displays the animation, whixch appears at the head of this page.
Experiment with small changes, like changing font, colours, text, position, image size, image type. You'll also find IM used in later versions of my Life, HodgePodge, Rasterbator & spinning spiral programs... see the links from Diga Me



     '   ___________________________________________________________________________
     '   /////////////////////////// ImageMagick \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
     '   ---------------------------------------------------------------------------

    '   Demonstrator of calling ImageMagick- here to make 36 gifs of rotated text,
    '       then turn them into an animated gif.

    '   No dll's are needed, so it works in LB & JB. But you DO have to have IM loaded
    '       on your system. Go to    http://www.imagemagick.org/script/index.php
    '   Download and install binary release /Windows version. Test it from a command window.
    '   To verify ImageMagick is working properly, type:
    '       > convert logo: logo.gif  ( create a gif version of the included IM logo)
    '       > identify logo.gif       ( show info about this image)
    '       > imdisplay logo.gif      ( use IM's own display window to show the gif)
    '   Now check that your file browser shows the gif file in the expected place!
    '   To use from LB or JB it's often easiest to use full pathnames.

    '   General procedure is to assemble the command string you'd use from the cmd window,
    '       then use the 'run' command of LB/JB to send them to a ( hidden) cmd window.

    '   NB get the strings right- mistakes can cause Windows to hang.
    '   When debugging it helps to enable the 'print' lines to see... you can copy/paste
    '       from mainwindow to an open cmd window.
    '   PS I only recently realised how to copy /paste from Windows to cmd line!

    '   I use RAMdisk, permanently set up, at R:. Alter to say C:\Temp or similar..

    '   Also it's necessary to use delays where LB or IM may not have a stage completed
    '       when you call a new command that relies on its results, or when you delete
    '       a file before it is finished with!

    '   I've used IM a lot for image conversion; to make animated gifs; do convolutions;
    '   It usefully concatenates images; produces annotated thumbnails; 
    '	It handles bulk ops, such as thumbnailing a directory full of images..
    '   I Intend to play with its 2D fft routines.... etc.


    nomainwin

    tick$ =chr$( 39)    '   Can be helpful to assemmble strings.
    '   Check manual on nesting quotes and other relevant rules of LB syntax.


    '   We are going to assemble command strings telling IM to print at a series of angles
    '   to successive images.
    '   I usually have a RAM disk set up for this but hdd is fine... but remember to delete
    '   any unwanted files afterwards if they were just intermediate....


    for angle =0 to 350 step 10 '   IM works in degrees
        IM$ = "convert -size 200x200 "_
        +" xc:red "_
        +" -gravity Center" _
        +" -font arial "_
        +" -pointsize 18 "_
        +" -fill blue "_
        +" -annotate "; str$( angle); "x"; str$( angle); "+40+60"  +"  Liberty! "_
        +" R:/text"; right$( "000" +str$( angle), 3); ".gif"

        'print IM$

        run "cmd.exe /c "; chr$( 34); IM$; chr$( 34), HIDE

        timer 2000, [on1]    '   Allow say 2 sec for the GIF rotation process to complete.
        wait
   [on1] timer 0

    next angle

    IM$ ="convert   -delay 20   -loop 0   R:\text*.gif    R:\AnimatedText.gif"
    'print IM$

    run "cmd.exe /c "; chr$( 34); IM$; chr$( 34), HIDE

    timer 30000, [on3]    '   Allow say 30 more sec for the animation assembly to complete.
    wait
  [on3] timer 0

    run "explorer "; chr$(34); "R:\AnimatedText.gif"; chr$(34)

    timer 30000, [on4]    '   Allow say 10 more sec for explorer to load & display.
    wait
  [on4] timer 0

    for angle =0 to 350 step 10 '   Remove the individual frames.
        kill "R:\text" +right$( "000" +str$( angle), 3) +".gif"
    next angle

    end


Moire

The next example creates animated gifs of a sequence of images which take too long to plot real-time in LB/JB


'

nomainwin

WindowWidth  = 430
WindowHeight = 460
UpperLeftX   =  50
UpperLeftY   =  50

graphicbox #w.g1,      10,  10, 400, 400

open "Animated Moire patterns" for window as #w

#w, "trapclose [quit]"
#w.g1, "down ; fill darkblue ; color white ; size 2"


[drawThem]
for wby2 =0 to 20 step 1  ' move centres left and right to sep'n of w

    #w.g1, "place "; 200 -wby2; " 170"

    for circle =1 to 60
        #w.g1, "circle "; circle *6
    next circle

    #w.g1, "place "; 200 +wby2; " 170"

    for circle =1 to 60
        #w.g1, "circle "; circle *6
    next circle


    #w.g1, "getbmp graphic 1 1 400 400"
    bmpsave "graphic", "R:\graphic.bmp"
    timer 4000, [on0]    '   Allow say 2 sec for the GIF rotation process to complete.
    wait
  [on0] timer 0

    IM$ = "convert R:\graphic.bmp R:\graphic"; right$( "000" +str$( wby2), 3); ".gif"
    run "cmd.exe /c "; chr$( 34); IM$; chr$( 34), HIDE

    timer 4000, [on1]    '   Allow say 2 sec for the GIF rotation process to complete.
    wait
  [on1] timer 0
    kill "R:\graphic.bmp"
    #w.g1, "fill darkblue"
next wby2

    IM$ ="convert   -delay 80   -loop 0   R:\graphic*.gif    R:\Animation.gif"
    run "cmd.exe /c "; chr$( 34); IM$; chr$( 34), HIDE

    timer 30000, [on3]    '   Allow say 30 more sec for the animation assembly to complete.
    wait
  [on3] timer 0

    run "explorer "; chr$(34); "R:\Animation.gif"; chr$(34)

    for wby2 =0 to 20 step 1 '   Remove the individual frames.
        kill "R:\graphic" +right$( "000" +str$( wby2), 3) +".gif"
    next wby2

[quit]
    close #w
    end



To learn more, it will be necessary of course to use the examples linked to from the IM site, and to be prepared to do some experimentation. Crashes from dodgy command strings may take LB down with them- so remember to save versions before running them, with sound parent/daughter filenames! Errors due to careless use of pathnames which include spaces ( which have to be 'in quotes' to use at the command line) or invalid filetypes or failure to scan for close messages or to wait for image processing to complete can lead to crashes which may need CTRL-ALT-DEL to exit.... If all else fails, remember you should have a copy of the most recent version in the 'bak' folder...

If you have problems, remember to use the forum, wiki, etc... or e-mail me.