Search code examples
pdfimagemagickanimated-gifflip

Convert PDF/Images to a flip based effect in GIF


I want to convert a PDF or sequence-of-images to a flip based effect in GIF (similar to the one below).

enter image description here

Is there any softwares available handy I could use to produce this output? or do I have to write scripts using imageMagicK? please suggest?

Thanks in advance!


Solution

  • Cool project! This isn't production-ready, military-hardened, bullet-proof code, but the following will do most of the heavy lifting as regards getting set up, appending the individual pages, distorting pages as they turn and finally putting the whole lot together in an animated GIF sequence.

    #!/bin/bash
    ################################################################################
    # flipbook
    # Mark Setchell
    #
    # Give me 4 pages as parameters and I create an animated GIF book out of them
    # called book.gif.
    #
    # Requires ImageMagick
    ################################################################################
    # Names of the 4 pages
    p0=${1:-page0.gif}  # Use first arg, or "page-0.gif" if none given
    p1=${2:-page1.gif}
    p2=${3:-page2.gif}
    p3=${4:-page3.gif}
    
    # Get width and height of images - I assume, but do not check all are identical sizes
    read w h < <(convert "$p0" -format "%w %h" info: )
    ((twow=w+w))
    
    # Layout first and last flat double-page spreads 
    convert "$p0" "$p1" +append frame0.png
    convert "$p2" "$p3" +append frame4.png
    
    # Make right page taller and thinner and save as "distorted.png"
    ((deltah=20*h/100))
    ((deltaw=20*w/100))
    ((hplusdeltah=h+deltah))
    ((wminusdeltaw=w-deltaw))
    ((hplus2deltah=h+deltah+deltah))
    points="0,0 0,$deltah $wminusdeltaw,0 $wminusdeltaw,0 $wminusdeltaw,$hplus2deltah $wminusdeltaw,$hplus2deltah 0,$hplus2deltah 0,$hplusdeltah"
    
    convert "$p1" +matte -virtual-pixel transparent         \
          -resize ${wminusdeltaw}x${hplus2deltah}! +repage  \
          -distort Perspective "$points" +repage distorted.png
    
    # Make second frame by overlaying distorted right page ontop of pages 0 and 3
    convert "$p0" "$p3" +append                  \
            -bordercolor white -border 0x$deltah \
            +repage                              \
            distorted.png                        \
            -geometry +${w}x                     \
            -composite frame1.png
    
    # Make left page taller and thinner and save as "distorted.png"
    ((deltaw=70*w/100))
    ((wminusdeltaw=w-deltaw))
    points="0,0 0,0 $wminusdeltaw,0 $wminusdeltaw,$deltah $wminusdeltaw,$hplus2deltah $wminusdeltaw,$hplusdeltah 0,$hplus2deltah 0,$hplus2deltah"
    
    convert "$p2" +matte -virtual-pixel transparent         \
          -resize ${wminusdeltaw}x${hplus2deltah}! +repage  \
          -distort Perspective "$points" +repage distorted.png
    
    # Make third frame by overlaying distorted left page ontop of pages 0 and 3
    convert "$p0" "$p3" +append                  \
            -bordercolor white -border 0x$deltah \
            +repage                              \
            distorted.png                        \
            -geometry +${deltaw}x                \
            -composite frame2.png
    
    # Make left page taller and thinner and save as "distorted.png"
    ((deltaw=20*w/100))
    ((wminusdeltaw=w-deltaw))
    points="0,0 0,0 $wminusdeltaw,0 $wminusdeltaw,$deltah $wminusdeltaw,$hplus2deltah $wminusdeltaw,$hplusdeltah 0,$hplus2deltah 0,$hplus2deltah"
    
    convert "$p2" +matte -virtual-pixel transparent         \
          -resize ${wminusdeltaw}x${hplus2deltah}! +repage  \
          -distort Perspective "$points" +repage distorted.png
    
    # Make fourth frame by overlaying distorted right page ontop of pages 0 and 3
    convert "$p0" "$p3" +append                  \
            -bordercolor white -border 0x$deltah \
            +repage                              \
            distorted.png                        \
            -geometry +${deltaw}x                \
            -composite frame3.png
    
    # Make final animation from frame0.png...frame4.png
    convert -gravity center -delay 100 frame*.png -background white -extent ${twow}x${hplus2deltah} book.gif
    

    So, if you start with the following as page0.gif, page1.gif, page2.gif and page3.gif...

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    You will get this as book.gif

    enter image description here

    If your book has more than 4 pages, you can do four at a time and then append the animations quite simply.

    Updated Answer

    It seems you are unfortunate enough to have to use Windows - which is very cumbersome in BATCH. I am no expert, but can get around in BATCH a little. I think the script above is pretty easy to translate though. I'll get you started but you will need to do some yourself - you can always ask a new question if you get stuck - questions are free!

    The first part of the script just picks up the parameters supplied on the command line, so it'll look like this:

    REM Pick up commandline parameters
    set p0=%1
    set p1=%2
    set p2=%3
    set p3=%4
    

    Then we need to work out the width and height of the input images, something like this:

    REM Get width and height of images in variable "w" and "h"
    FOR /F %%A IN ('identify -format "w=%%w\nh=%%h" %p0%') DO set %%A
    

    All the stuff in my original script inside ((..)) is just simple maths which can be done in BATCH using SET /A, so the lines that look like this:

    ((twow=w+w))
    ((deltah=20*h/100))
    

    will look like this:

    SET /A TWOW=w+w
    SET /A DELTAH=20*h/100
    

    The rest is just convert commands - you will need to do a couple of things there:

    • Replace line continuations at ends of lines, so change \ to ^

    • Where I use $variable or ${variable}, replace it with %variable%

    • Double any % signs I have, so % becomes %%

    • Change \( to ^( - I think

    • change any single quotes ' to double quotes "

    Best to just work through it and see what happens as you convert each line and ask another question if you can't work it out.

    There is some good info at these places - ss64 - general, ss64 - set command on BATCH in general. Also, an English guy called Alan Gibson, uses IM with Windows very competently and you can see his scripts here, and also more generally here for inspiration on how to be effective with IM under Windows.