1

I'm trying to generate images for artistic purposes. The images I want to generate show the process of diffusion. Basically I want the image to start in a state of low entropy and move to a state of maximum entropy. I'd like 3 images:

  1. A circle, half black and half transparent (minimal entropy)
  2. The black and transparent pixels have partially mixed (medium entropy).
  3. A circle filled randomly with half black and half transparent pixels (maximum entropy)

My first attempt at this is to start with the black semicircle and then for each pixel, if it's black, move to a random adjacent clear pixel. This works okay, but not perfectly. If I run for enough iterations, it starts to form clumps of black pixels. Here's the relevant part of the code:

for i in range(im.size[0]):    # for every col:
        for j in range(im.size[1]):    # For every row
            if pixels[i,j] == (0, 0, 0, 255):
                xs = random.randint(-1,1)
                ys = random.randint(-1,1)
                try:
                    target = pixels[i+xs, j+ys]
                except IndexError:
                    continue
                if (i+xs, j+ys) in badpixels: # badpixels is a hacky way to keep all pixels in a circle
                    continue

                if target == (0, 0, 0, 0): # move if unoccupied
                    try:
                        pixels[i+xs,j+ys] = (0, 0, 0, 255)
                        pixels[i,j] = (0, 0, 0, 0) 
                    except IndexError:
                        pass

With this process after 500 iterations I get this: enter image description here

After 1000 iterations I get this: enter image description here

And after 3000 iterations I get this: enter image description here

As you can see, there are unwanted clumps at the end. Please let me know how I should proceed to do this properly.

user1302130
  • 456
  • 5
  • 9
  • Interesting! I think your question would be improved by showing a proper [mcve] rather than extracts of code with aspects missing. Also, I think your final output consists purely of black or transparent pixels, i.e. just two possible *"colours"* so I would suggest you use a palette/indexed image for memory and processing efficiency https://stackoverflow.com/a/52307690/2836621 – Mark Setchell Nov 30 '22 at 10:10

0 Answers0