Jackeea

Geonome to Image

May 7th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. from PIL import Image
  2.  
  3. def base_to_int(b):
  4. #Takes in a base and converts to an integer.
  5. # a = 0
  6. # c = 1
  7. # g = 2
  8. # t = 3
  9. total = 0
  10. if b == "a":
  11. return 0
  12. if b == "c":
  13. return 1
  14. if b == "g":
  15. return 2
  16. if b == "t":
  17. return 3
  18. print(b)
  19. raise TypeError("Not a valid base.")
  20.  
  21. def bases_to_pixel(b):
  22. #Takes in 4 bases and converts them to an RGB pixel value.
  23. #e.g. takes in acgt, then does 4^3 * 0 + 4^2 * 2 + ...
  24. total = 0
  25. for idx in [0,1,2,3]:
  26. total+=4**(3-idx) * base_to_int(b[idx])
  27. return total
  28.  
  29.  
  30. def make_image():
  31. img = Image.new( 'RGB', (45,45), "black")
  32. geonome = ""
  33. with open("coronavirus geonome.txt","r") as g:
  34. geonome = g.read()
  35. chunks = [bases_to_pixel(geonome[i:i+4]) for i in range(0, len(geonome), 4)]
  36. pixels = img.load()
  37. for i in range(len(chunks)//3):
  38. pixels[i%45,i//45] = (chunks[i*3],chunks[i*3+1],chunks[i*3+2])
  39. img.show()
  40. img.save("coronavirus image.png","PNG")
Add Comment
Please, Sign In to add comment