Page 1 of 1

Extract Images From Animated Gif Rate Topic: -----

#1 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Posted 08 November 2011 - 10:16 PM

Hello.

It doesn't look to be too much information in the internet regarding this.

This is a image extractor for animated gifs.

Attached Image

There are different file formats that have multiple images in them, like some ico files and some tif files. In a gif file, the animation occurs when the sequence of images inside it are displayed. These images are extracted by the programs and then sent to the display, one at a time with some set delay. There are several decoders in the framework to interpret the bytes in the files as an image.

The different pictures in a gif animation, called frames are bitmaps that can be extracted and saved individually.

    Public Sub Strip(ByVal Prefix As String)

        Dim oDimension As New FrameDimension(OriginalGif.FrameDimensionsList(0))
        Dim FrameCount As Integer = OriginalGif.GetFrameCount(oDimension)
        For i As Integer = 0 To FrameCount - 1
            OriginalGif.SelectActiveFrame(oDimension, i)

            Using Frames As New Bitmap(OriginalGif)

                Select Case Trim(FileTypes.Text.ToUpper)
                    Case ".BMP"
                        Frames.Save(GetNewName(Prefix, FileTypes.Text), ImageFormat.Bmp)
                    Case ".JPG"
                        Frames.Save(GetNewName(Prefix, FileTypes.Text), ImageFormat.Jpeg)
                    Case ".GIF"
                        Frames.Save(GetNewName(Prefix, FileTypes.Text), ImageFormat.Gif)
                    Case ".PNG"
                        Frames.Save(GetNewName(Prefix, FileTypes.Text), ImageFormat.Png)
                    Case ".TIF"
                        Frames.Save(GetNewName(Prefix, FileTypes.Text), ImageFormat.Tiff)
                End Select

            End Using

            If i = (FrameCount - 1) Then MessageBox.Show("Done")
        Next


    End Sub




I made my program to be able to save the frames in some of the more popular formats, but there are others supported in the framework.

If you didn't create the animation and you do need to edit it or to save a still image for further manipulaton, yo do need to separate it from the other pictures. And then, probably you would like to assemble it again.

But the images inside a gif animaton have no individual names, but they have a required order to display properly, so you need to save them to disk with related names and a sequence to be able to keep that order at reassembling.

One of the best ways is to use an index that increases in every loop of the extraction. But there is a glitch: when sorting the images, that could be several dozens, the numbers are treated as characters and sorted alphabetically from the left-most characters to the right. So, a generated sequence of, lets say, 250 frames, you have several problems in the ordering:

0,1,10,100,101,102...109,11,110,112...119,12,120.... 19,190,191...199,2,20,200,201...

If you assemble them, you will have a strage behaviour, and a little jerky animation. So you need to rename them manually or name them at the creation time with a name that will keep it's place in the ordering. I did accomplish this by inserting zeros to the left of the index on the proper situations. I use a prefix, some zeros and a suffix:
Frames & 000,00,0 & i & .jpg

    Public Function GetNewName(ByVal Prefix As String, ByVal Suffix As String) As String
        Dim i As Integer
        Dim str As String = ""
        For i = 0 To 10000
            If i < 10 Then str = Prefix & "000" & i.ToString & Suffix
            If i > 9 And i < 100 Then str = Prefix & "00" & i.ToString & Suffix
            If i > 99 And i < 1000 Then str = Prefix & "0" & i.ToString & Suffix
            If i > 999 Then str = Prefix & i.ToString & Suffix
            If Not System.IO.File.Exists(str) Then
                Exit For
            End If
        Next
        Return str
    End Function



I am guessing that 3 zeros as maximum will allow you up to 10,000 frames... That's a lot of images, and probably, there are not too many gifs with few hundreds.

The suffix includes the file extension to use and it will be according to the encoder you are using.

The file type is selected from a combobox that is loaded at form load, and the prefix is typed in a textbox:

My default are Frames and jpg, because I plan to use them further, but you could select the more suitable encoder for your use. I have been playing with some code to write the animation and I have a working code, but my animations are a little bulky. You could find some free encoders on the internet.

This code is not used in the current project, but in another program I made to resize and crop animations. I think it would be interesting for some of you. It is a modification of a code from Rick van den Bosch that I found at this address:

http://bloggingabout...05/10/3830.aspx


    Public Sub Assemble(ByVal But As Integer)
        'Variable declaration
        Dim memoryStream As MemoryStream
        Dim binaryWriter As BinaryWriter
        Dim image As Image
        Dim buf1 As [Byte]()
        Dim buf2 As [Byte]()
        Dim buf3 As [Byte]()
        memoryStream = New MemoryStream()
        buf2 = New [Byte](18) {}
        buf3 = New [Byte](7) {}
        buf2(0) = 33
        'extension introducer
        buf2(1) = 255
        'application extension
        buf2(2) = 11
        'size of block
        buf2(3) = 78
        'N
        buf2(4) = 69
        'E
        buf2(5) = 84
        'T
        buf2(6) = 83
        'S
        buf2(7) = 67
        'C
        buf2(8) = 65
        'A
        buf2(9) = 80
        'P
        buf2(10) = 69
        'E
        buf2(11) = 50
        '2
        buf2(12) = 46
        '.
        buf2(13) = 48
        '0
        buf2(14) = 3
        'Size of block
        buf2(15) = 1
        '
        buf2(16) = 0
        '
        buf2(17) = 0
        '
        buf2(18) = 0
        'Block terminator
        buf3(0) = 33
        'Extension introducer
        buf3(1) = 249
        'Graphic control extension
        buf3(2) = 4
        'Size of block
        buf3(3) = 9
        'Flags: reserved, disposal method, user input, transparent color
        buf3(4) = 10 ' 88 

        'Delay time low byte
        buf3(5) = 0 '1 
        'Delay time high byte
        buf3(6) = 255

        'Transparent color index
        buf3(7) = 0
        'Block terminator
        '  If File.Exists("result.gif") Then
        'MessageBox.Show("Deleting Old File")
        ' File.Delete("result.gif")
        ' Thread.Sleep(5000)
        ' End If

        NewName = GetNewName()
        binaryWriter = New BinaryWriter(File.Open(NewName, FileMode.Create))

        For picCount As Integer = 0 To FNameAry.Count - 1 'stringCollection.Count - 1
            Select Case But
                Case 1
                    'Animate
                    image = Bitmap.FromFile(FNameAry(picCount))

                Case 2
                    If MemPic IsNot Nothing Then
                        image = MemPic(picCount)
                    Else
                        MessageBox.Show("Please Select Something")
                    End If

            End Select
            image.Save(memoryStream, ImageFormat.Gif)
            buf1 = memoryStream.ToArray()

            If picCount = 0 Then
                'only write these the first time....
                binaryWriter.Write(buf1, 0, 781)
                'Header & global color table
                'Application extension
                binaryWriter.Write(buf2, 0, 19)
            End If
            binaryWriter.Write(buf3, 0, 8)
            'Graphic extension
            binaryWriter.Write(buf1, 781, buf1.Length - 782)
            'Image data
            ' If picCount = stringCollection.Count - 1 Then
            If picCount = FNameAry.Count - 1 Then
                'only write this one the last time....
                'Image terminator
                binaryWriter.Write(";")
            End If


            memoryStream.SetLength(0)
        Next

        binaryWriter.Close()
        PictureBox1.Invalidate()
        memoryStream.Close()
    End Sub



There is an attached project.

Please take a look and comment.

Thank you.

ricardosms.

Attached File(s)



Is This A Good Question/Topic? 3
  • +

Replies To: Extract Images From Animated Gif

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 221
  • View blog
  • Posts: 1,478
  • Joined: 16-March 11

Posted 02 December 2011 - 07:11 PM

Appreciate the insight ricardosms. I've recently begun tinkering with images.

This post has been edited by trevster344: 02 December 2011 - 07:11 PM

Was This Post Helpful? 0
  • +
  • -

#3 ricardosms  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 73
  • View blog
  • Posts: 301
  • Joined: 02-April 10

Posted 03 December 2011 - 05:37 AM

View Posttrevster344, on 02 December 2011 - 09:11 PM, said:

Appreciate the insight ricardosms. I've recently begun tinkering with images.


Thank you.
You might want to check this other tutorial of mine: Gif Animation Editing And Processing Program
at
http://www.dreaminco...1&#entry1489234

It is an evolution of this one and is much more complete. And also I can give you my new version.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1