Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- def base_to_int(b):
- #Takes in a base and converts to an integer.
- # a = 0
- # c = 1
- # g = 2
- # t = 3
- total = 0
- if b == "a":
- return 0
- if b == "c":
- return 1
- if b == "g":
- return 2
- if b == "t":
- return 3
- print(b)
- raise TypeError("Not a valid base.")
- def bases_to_pixel(b):
- #Takes in 4 bases and converts them to an RGB pixel value.
- #e.g. takes in acgt, then does 4^3 * 0 + 4^2 * 2 + ...
- total = 0
- for idx in [0,1,2,3]:
- total+=4**(3-idx) * base_to_int(b[idx])
- return total
- def make_image():
- img = Image.new( 'RGB', (45,45), "black")
- geonome = ""
- with open("coronavirus geonome.txt","r") as g:
- geonome = g.read()
- chunks = [bases_to_pixel(geonome[i:i+4]) for i in range(0, len(geonome), 4)]
- pixels = img.load()
- for i in range(len(chunks)//3):
- pixels[i%45,i//45] = (chunks[i*3],chunks[i*3+1],chunks[i*3+2])
- img.show()
- img.save("coronavirus image.png","PNG")
Add Comment
Please, Sign In to add comment