Created
July 24, 2017 07:49
-
-
Save kbrgl/cb97958c349db4ebb1623645e08ed4c5 to your computer and use it in GitHub Desktop.
Program to create an image with pixels corresponding to the Fibonacci series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Program to create an image with pixels corresponding to the Fibonacci series. | |
(c) 2017 Kabir Goel | |
Licensed under the terms of the MIT license. | |
""" | |
import sys | |
import argparse | |
try: | |
from PIL import Image | |
except ImportError: | |
print("Python Imaging Library is not installed\nInstall pillow to continue") | |
sys.exit(1) | |
def fib(n): | |
""" | |
Return values of the Fibonacci sequence, up to and including n. | |
""" | |
a,b = 0,1 | |
yield a | |
yield b | |
while b <= n: | |
a, b = b, a + b | |
yield b | |
def fib_pixels_till(n): | |
""" | |
Return pixel values that correspond to Fibonacci numbers. If values exceed | |
(255, 255, 255) (that's white), then they will be truncated to white. | |
""" | |
for f in fib(n): | |
if f > 255: | |
break | |
yield (f, f, f) | |
def main(): | |
parser = argparse.ArgumentParser(description='Create an image with pixels corresponding to the Fibonacci series') | |
parser.add_argument('-size', dest='size', type=int, default=1, help='size of one unit in the image') | |
parser.add_argument('-out', dest='out', help='file to save image to') | |
args = parser.parse_args() | |
pixels = [p for p in fib_pixels_till(float('inf'))] | |
im = Image.new('RGB', (len(pixels), 1)) | |
im.putdata(pixels) | |
# resize factor | |
rf = args.size | |
im = im.resize((len(pixels)*rf, rf)) | |
if args.out: | |
im.save(args.out) | |
else: | |
im.show() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment