File:Wfm floodfill animation queue.gif

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Wfm_floodfill_animation_queue.gif (200 × 200 pixels, file size: 31 KB, MIME type: image/gif, looped, 175 frames, 19 s)

Captions

Captions

Add a one-line explanation of what this file represents

Summary

[edit]
Description Example image showing Floodfill animated using a queue structure
Date
Source Own work
Author Finlay McWalter
Other versions image:Wfm_floodfill_animation_stack.gif

Licensing

[edit]
I, the copyright holder of this work, hereby publish it under the following licenses:
GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International, 3.0 Unported, 2.5 Generic, 2.0 Generic and 1.0 Generic license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
You may select the license of your choice.

Creation

[edit]

Created using the following Python program:

#!/usr/bin/python

# Basic Python program to render animations for Wikipedia floodfill
# article (not a general floodfill example, very far from a shining example).

# This example uses the four-way floodfill algorithm, and generates 
# example animations using both a stack and a queue as intermediate
# storage schemes.

# Requirements:
# * python
# * Python Imaging Library (PIL)
# * ImageMagick (used to compile intermediate images to GIF animations)
# 
# The os.system() calls are written for unix; changing them for windows
# should be trivial

# Copyright (C) 2008 Finlay McWalter. 
# Licence: your choice from GFDL, GPLv2, GPLv3, CC-by-SA2.0
 

import os, Image, ImageColor, ImageDraw
from collections import deque

def floodfill(img, 
              (startx, starty), 
              targetcolour, 
              newcolour, 
              dumpEveryX=70,
              pattern=None,
              useQueue=False,
              startIndex=0):
    
    if useQueue:
        workpile = deque();
        getpoint = workpile.popleft
    else:
        workpile = []
        getpoint = workpile.pop

    max=0
    count=0

    workpile.append((startx,starty))
    while len(workpile)> 0:
        x,y=getpoint()
        
        if len(workpile)> max: 
            max=len(workpile)

        if img.getpixel((x,y)) == targetcolour:
            img.putpixel((x,y), newcolour)
            
            # every few pixels drawn, dump an image showing our progress
            count += 1
            if (count%dumpEveryX)==0 and pattern != None:
                img.save(pattern %(startIndex+count))

            # this demo code doesn't handle the case where we get to the edge
            if img.getpixel((x-1,y))== targetcolour: workpile.append((x-1,y))
            if img.getpixel((x+1,y))== targetcolour: workpile.append((x+1,y))
            if img.getpixel((x,y-1))== targetcolour: workpile.append((x,y-1))
            if img.getpixel((x,y+1))== targetcolour: workpile.append((x,y+1))

    print '    done with count %d, max %d' % (count,max)
    return count

def make_floodfill_example(filename, use_queue):
    print 'making image '+filename
    print '  removing old files'
    os.system('rm -f out*.png ' +filename )

    i = Image.new('RGB', (200,200), 'white')

    # draw a rough ying-yang
    draw = ImageDraw.Draw(i)
    draw.ellipse((30,30,170,170), outline='black')
    draw.arc((65,100,135,170), 90,270, fill='black')
    draw.arc((64,30,134,100), 270,90, fill='black')
    draw.ellipse((80,45,120,85), outline='black')
    draw.ellipse((80,115,120,155), outline='black')
    del draw

    print '  filling'
    redcount = floodfill(i, 
                         (100, 90), 
                         (255,255,255), #white
                         (255,0,0), #red
                         useQueue = use_queue,
                         pattern='out_%05d.png')

    print '  filling'
    bluecount = floodfill(i,
                          (110,110),
                          (255,255,255), # white
                          (0,0,255), # blue
                          useQueue = use_queue,
                          pattern='out_%05d.png',
                          startIndex=redcount)

    # push some extra frames of animation so we can see the finished fill
    for x in range(redcount+bluecount,redcount+bluecount+20):
        i.save('out_%05d.png' %(x))    

    print '  converting to animated GIF - this may take several minutes'
    os.system ('convert -loop 0 out*.png '+filename)

# draw one example image using a FIFO as the means of storing points,
# and another using a LIFO.
make_floodfill_example('wfm_floodfill_animation_queue.gif', True)
make_floodfill_example('wfm_floodfill_animation_stack.gif', False)
print 'done'

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current23:07, 8 April 2014Thumbnail for version as of 23:07, 8 April 2014200 × 200 (31 KB)CountingPine (talk | contribs)Optimise file size using GIMP
15:28, 24 August 2008Thumbnail for version as of 15:28, 24 August 2008200 × 200 (241 KB)Finlay McWalter (talk | contribs){{Information |Description= |Source= |Date= |Author= |Permission= |other_versions= }}
14:28, 24 August 2008Thumbnail for version as of 14:28, 24 August 2008200 × 200 (236 KB)Finlay McWalter (talk | contribs){{Information |Description=Example image showing Floodfill animated using a queue structure |Source=self made |Date=24th August 2008 |Author=Finlay McWalter |Permission=see below |other_versions=[[:image:Wfm_floodfill_animation_queue.gif

The following page uses this file:

File usage on other wikis

The following other wikis use this file: