0

I just started coding with Python , I have an image that is supposed to be decoded by the base64 module as I understood , but I keep getting this error :" Invalid base64-encoded string: number of data characters (13) cannot be 1 more than a multiple of 4 " , I changed the syntax of the code so I started getting an error of padding instead.

def call_dalle(url, text, num_images=1):
    data = {"text": text, "num_images": num_images}
    resp = requests.post(url + "/dalle", headers=headers, json=data)
    if resp.status_code == 200:
        return resp
    
def create_and_show_images(text, num_images):
    valid = check_if_valid_backend(URL)
    if not valid:
        st.write("Backend service is not running")
    else:
        resp = call_dalle(URL, text, num_images)
        if resp is not None:
            for data in resp.json():
                img_data = base64.b64decode(data + "=" * 3)
                st.image(img_data)

I tried most of the solutions that I found to similar problems but nothing worked so far.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Aymen Gadri
  • 1
  • 1
  • 3
  • 1
    Simple solution: `b64decode(data + "=" * 4)`. "Correct" solution: `b64decode(data + "=" * (-len(data) % 4))`. – Olvin Roght Jul 23 '22 at 08:01
  • @OlvinRoght Shouldn't it be `... + "=" *3`, since the current number of chars is 1 over a mutliple of 4? – Thierry Lathuille Jul 23 '22 at 08:11
  • I tried it and i keep getting the same error , i tried adding '-_' too , didnt work as well – Aymen Gadri Jul 23 '22 at 08:11
  • @ThierryLathuille, it could be `"=" * 100` actually, python truncates extra padding. Two equal signs is maximum possible, so minimum necessary to work stable is `"=" * 2`. – Olvin Roght Jul 23 '22 at 08:11
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551). Please *do* include a [mre] - show us the input that's failing, so that we can test the suggestions we're giving you. – IMSoP Jul 23 '22 at 08:12
  • @AymenGadri, code I've provided in [this](https://stackoverflow.com/q/73089007/10824407#comment129086657_73089007) comment should fix padding problem. Are you sure that you used it correctly? – Olvin Roght Jul 23 '22 at 08:13
  • @OlvinRoght , yes , at least I think so xD , I just updated my post with the full code – Aymen Gadri Jul 23 '22 at 08:21
  • @AymenGadri, what python version are you using? Error message is the same after you modified code? – Olvin Roght Jul 23 '22 at 08:22
  • @OlvinRoght , I am using Python 3.10 , yes the same "Invalid base64-encoded string: number of data characters (13) cannot be 1 more than a multiple of 4" – Aymen Gadri Jul 23 '22 at 08:25
  • @AymenGadri, `data + "=" * 3` definitely adds to `data` 3 more chars, so number of data characters can't be `13` before applying this modification and after. Have you saved your script? If your app is running server, you need to restart your app to apply changes. – Olvin Roght Jul 23 '22 at 08:28
  • @OlvinRoght , exactly , that's what I don't understand either , Even when I change the padding of the data that should be decoded I keep getting the same error with the same numbers ( 13 data characters , cannot be ... 1 morde ... 4 ) – Aymen Gadri Jul 23 '22 at 08:33
  • @AymenGadri, add `10 / 0` one line above decoding. If `ZeroDivisionError` thrown, your changes in source code saved and some mistery happens. If not, you simply not saving changes and/or not reloading your server. – Olvin Roght Jul 23 '22 at 08:36
  • @OlvinRoght , hahahah yes I got the zeroDivisionError . Can it be due to the version of the django and python , that maybe are not compatible together or something like that ? – Aymen Gadri Jul 23 '22 at 08:40
  • @AymenGadri, if `base64` is built-in module, so compatibility shouldn't be an issue. I have no idea what causes this exception though. Try to print `data` before decoding, maybe it's not properly formatted or smth. – Olvin Roght Jul 23 '22 at 08:44
  • @OlvinRoght , Thank you thats helpful actually , I am getting "generatedImgs" which is I am guessing a backend function – Aymen Gadri Jul 23 '22 at 08:51

0 Answers0