Python Pillow: putpixel() does not record the color correctly

Viewed 46

I started writing a small library for my game. The essence of the library is to read config from the image. And I ran into such a problem, when recording colors are not recorded correctly! Here is the code:

from PIL import Image

class CustomConfigReader:
    def __init__(self, path):
        self.configPath = path
        self.laters = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m", " ", ".", ",", "!", "?"]
    def edit(self, y, value):
        l1 = len(value)
        configImage = Image.open(self.configPath).convert("RGB")
        for i in range(0, l1):
            l2 = value[i]
            l3 = self.laters.index(l2)
            configImage.putpixel((i, y), (l3, l1, l1))
        configImage.save(self.configPath)
    def read(self, y):
        configImage = Image.open(self.configPath).convert("RGB")
        l1 = configImage.getpixel((0, y))[1]
        value = ""
        for i in range(0, l1-1):
            l3 = configImage.getpixel((i, y))[0]
            value += self.laters[l3]
        return value

1 Answers

The problem as it turned out was in JPEG format, I tried to change it to png and everything worked! Thanks Mark Setchell

Related