GithubHelp home page GithubHelp logo

derthorsten / python-pil-metaballs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jmickela/python-pil-metaballs

0.0 2.0 0.0 136 KB

A metaball system that works with Python/PIL.

License: GNU General Public License v2.0

Python 100.00%

python-pil-metaballs's Introduction

python-pil-metaballs

This is based on the code found at http://www.niksula.cs.hut.fi/~hkankaan/Homepages/metaballs.html and in fact borrows heavily from to code posted there. I've made a few modifications/improvements to fix a few issues where the rendering would lock up under certain conditions. I also converted this to use PIL/Pillow instead of pygame.

There are still bugs in the rendering and sometimes output isn't generated as the code gets stuck in a loop. I'm hoping that by posting this here others can help speed the code up and/or fix the various issues that I've run into.

Example Usage

Here's an example usage with five balls. With these values four should be connected together, one should be on its own. You can play with the goo and threshold values a bit, but small changes in those values cause large changes in the image. Smaller goo values cause the balls to merge more, which I suppose is counterintuitive. You'll probably want to leave the threshold as it is and mess with goo to adjust how the balls are drawn, very small changes in threshold cause large changes in the drawn metaballs.

from PIL import Image, ImageDraw
from metaballs import MetaBallManager, Ball

WIDTH = 640
HEIGHT = 480

balls = []
size = (WIDTH, HEIGHT)
RADUIS = 3
STEP_SIZE = 1

GOO = 3.0
THRESHOLD = 0.004

image = Image.new('RGBA', size)

balls.append(Ball(100, 100, RADUIS))
balls.append(Ball(110, 110, RADUIS))
balls.append(Ball(130, 130, 6*RADUIS))
balls.append(Ball(150, 130, 16*RADUIS))
balls.append(Ball(180, 180, RADUIS))

manager = MetaBallManager(balls, GOO, THRESHOLD, (255, 0, 0), image, WIDTH*HEIGHT)

manager.DrawBalls(manager.rungeKutta2, STEP_SIZE)
image.save("image.png", "PNG")

Known Issues

In the above example, if you set goo to 2, the entire image will be painted red. This is because the algorithm often produces balls that aren't completely closed, so when floodfill is called to fill in the ball it fills the whole image. I don't have time to fix this right now and am no longer working on the project that I was going to use this for, so I likely wont get to this again. If you encounter this problem, mess with the goo or threshold values, change your step size, or use one of the other algorithms for tracing the ball.

python-pil-metaballs's People

Contributors

jmickela avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.