Skip to main content
[Edit removed during grace period]
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

Your constants should be in UPPER_SNAKE_CASE

MAX_COUNT = 200
WIDTH = img.size[0]
HEIGHT = img.size[1]

Instead of nested for loops, you can use itertools.product which basically will perform the nested iteration you need:

from itertools import product

for row, col in product(range(height), range(width))

You don't use str_output, you should clean up unused values.

You should have whitespace either side of your mathematical operators and have a space after each comma in a comma separated list of values:

(col - float(width) / 2.0) * 5.0 / float(width),
draw.point((col, row), fill=(255 - iteration, 255 - iteration, 255 - iteration))

You should also keep lines below 79 characters as the Python style guide dictates. You can split lines quite easily when they're in paretheses:

    draw.point((col, row), fill=(255 - iteration, 255 - iteration,
                                 255 - iteration))

Also your mileage may vary on whether or not this is more readable but you could use a ternary for your final if condition. Like this:

    draw.point((col, row), fill=("black" if abs(z) < 2 else 
                                 (255 - iteration, 255 - iteration,
                                  255 - iteration))
                                  

The formatting is definitely more awkward, but I personally like these structures because it makes it clear that the only difference in the two results is that one condition.