- # open an image image should be in pnm p3 format ascii
- #P3 # "P3" means this is a RGB color image in ASCII
- # 3 2 # "3 2" is the width and height of the image in pixels
- # 255 # "255" is the maximum value for each color
- # # The part above is the header
- # # The part below is the image data: RGB triplets
- # 255 0 0 # red
- # 0 255 0 # green
- #
- # file one line at a time
- fileName = "./ironbow_palette.pnm"
- fp = open(fileName,"r")
- fileType = fp.readline()
- fileComment = fp.readline()
- rawRes = fp.readline()
- rawResSplit = rawRes.split()
- print(rawResSplit)
- sizeX = int(rawResSplit[0])
- sizeY = int(rawResSplit[1])
- rawBpp = fp.readline()
- bpp = int(rawBpp)
- print("sizeX",sizeX,"sizeY",sizeY,"bpp",bpp)
- #go left to right and get pixel rgb at each point
- pyCode = "the_palette = [\n"
- theComma = ","
- theNewlineCount = 0
- for i in range(0,sizeX):
- rValRaw = fp.readline().rstrip()
- gValRaw = fp.readline().rstrip()
- bValRaw = fp.readline().rstrip()
- theComma = ","
- if i == (sizeX - 1):
- theComma = "\n"
- pyCode+=(f"\t[ {rValRaw},{gValRaw},{bValRaw}]{theComma} ")
- if not (theNewlineCount % 4):
- pyCode += "\n"
- theNewlineCount += 1
- #print("Line {}: {}".format(cnt, line.strip()))
- #line = fp.readline()
- #cnt += 1
- pyCode += "]\n"
- print(pyCode)
- #write as a python a List[List[r,g,b]]
ImageToPythonListOfListsLookupTable
Posted by Anonymous on Mon 2nd Jan 2023 20:09
raw | new post
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.