pastebin - collaborative debugging tool
python.kpaste.net RSS


ImageToPythonListOfListsLookupTable
Posted by Anonymous on Mon 2nd Jan 2023 20:09
raw | new post

  1. # open an image  image should be in pnm p3 format ascii
  2. #P3           # "P3" means this is a RGB color image in ASCII
  3. # 3 2          # "3 2" is the width and height of the image in pixels
  4. # 255          # "255" is the maximum value for each color
  5. # # The part above is the header
  6. # # The part below is the image data: RGB triplets
  7. # 255   0   0  # red
  8. #   0 255   0  # green
  9. #
  10. # file one line at a time
  11. fileName = "./ironbow_palette.pnm"
  12. fp = open(fileName,"r")
  13. fileType = fp.readline()
  14. fileComment = fp.readline()
  15. rawRes = fp.readline()
  16. rawResSplit = rawRes.split()
  17. print(rawResSplit)
  18. sizeX = int(rawResSplit[0])
  19. sizeY = int(rawResSplit[1])
  20.  
  21. rawBpp = fp.readline()
  22. bpp = int(rawBpp)
  23.  
  24. print("sizeX",sizeX,"sizeY",sizeY,"bpp",bpp)
  25.  
  26.  
  27. #go left to right and get pixel rgb at each point
  28. pyCode = "the_palette = [\n"
  29.  
  30. theComma = ","
  31. theNewlineCount = 0
  32. for i in range(0,sizeX):
  33.         rValRaw = fp.readline().rstrip()
  34.         gValRaw = fp.readline().rstrip()
  35.         bValRaw = fp.readline().rstrip()
  36.  
  37.         theComma = ","
  38.         if i == (sizeX - 1):
  39.                 theComma = "\n"
  40.  
  41.         pyCode+=(f"\t[ {rValRaw},{gValRaw},{bValRaw}]{theComma} ")
  42.  
  43.         if not (theNewlineCount % 4):
  44.                 pyCode += "\n"
  45.  
  46.         theNewlineCount += 1
  47.  
  48.         #print("Line {}: {}".format(cnt, line.strip()))
  49.         #line = fp.readline()
  50.         #cnt += 1
  51.  
  52. pyCode += "]\n"
  53.  
  54. print(pyCode)
  55.  
  56.  
  57. #write as a python a List[List[r,g,b]]

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at