4

I am trying to convert base64 string into image, so far I have been following this: How to convert base64 string to image? here is pas of the code

image = stringToRGB(request.POST.get("image", None))
#request.POST.get("image", None) = /9j/4QL2RXhpZgAATU0AKgAAAAgACwEPAAIAAAAIAAAAkgEQAAIAAAAJAAAAmgESAAMAAAABAAgAAAEaAAUAAAABAAAApAEbAAUAAAABAAAArAEoAAMAAAABAAIAAAExAAIAAAA8AAAAtAEyAAIAAAAUAAAA8AITAAMAAAABAAEAAIdpAAQAAAABAAABBIglAAQAAAABAAAC6AAAAABzYW1zdW5nAFNNLU0xMTVGAAAAAABIAAAAAQAAAEgAAAABbTExcW5zeHgtdXNlciAxMCBRUDFBLjE5MDcxMS4wMjAgTTExNUZYWFUxQVRKMiByZWxlYXNlLWtleXMAMjAyMTowNDoyMCAwOTo1OTozMgAAHIKaAAUAAAABAAACWoKdAAUAAAABAAACYogiAAMAAAABAAAAAIgnAAMAAAABAi8AAJAAAAcAAAAEMDIyMJADAAIAAAAUAAACapAEAAIAAAAUAAACfpEBAAcAAAAEAQIDAJIBAAoAAAABAAACkpICAAUAAAABAAACmpIDAAoAAAABAAACopIHAAMAAAABAAIAAJIJAAMAAAABABgAAJIKAAUAAAABAAACqpKQAAIAAAAHAAACspKRAAIAAAAHAAACupKSAAIAAAAHAAACwqAAAAcAAAAEMDEwMKABAAMAAAABAAEAAKACAAQAAAABAAAFAKADAAQAAAABAAAC0KAFAAQAAAABAAACyaIXAAMAAAABAAAAAKMBAAcAAAABAQAAAKQCAAMAAAABAAAAAKQDAAMAAAABAAAAAKQFAAMAAAABAAAAAKQGAAMAAAABAAAAAAAAAAAAAAABAAAAGAAAAMgAAABkMjAyMTowNDoyMCAwOTo1OTozMgAyMDIxOjA0OjIwIDA5OjU5OjMyAAAAEegAAAPoAAAAyAAAAGT////MAAAAZAAACsgAAAPoMTYwMzA2ADAxNjAzMDYAMDE2MDMwNgAAAgABAAIAAAAEUjk4AAACAAcAAAAEMDEwMAAAAAAAAAAAAAAA/9sAhAA
def stringToRGB(base64_string):
    imgdata = base64.b64decode(str(base64_string))
    image = Image.open(io.BytesIO(imgdata))
    return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)

the result is I get an error that say "Incorrect padding" is there a way to solve this case ?

Second update

def stringToRGB(base64_string):
    base64_string += "=" * ((4 - len(base64_string) % 4) % 4)
    imgdata = base64.b64decode(str(base64_string))
    image = Image.open(io.BytesIO(imgdata))
    return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)

error for second update:

cannot identify image file <_io.BytesIO object at 0x11ba2c400>

update 3

def stringToRGB(base64_string):
    # base64_string += "===" * ((4 - len(base64_string) % 4) % 4)
    altchars = b'+/'
    base64_string = re.sub(rb'[^a-zA-Z0-9%s]+' %
                           altchars, b'', base64_string)  # normalize
    missing_padding = len(base64_string) % 4
    if missing_padding:
        base64_string += b'=' * (4 - missing_padding)

    imgdata = base64.b64decode(str(base64_string), altchars)
    print(imgdata, ";;;;;;;;")
    image = Image.open(io.BytesIO(imgdata))
    print(image, "////")
    return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)

from third update I get an error: cannot use a bytes pattern on a string-like object

wahyu
  • 1,679
  • 5
  • 35
  • 73
  • You will have to decode bas64 image like `base64_string.split(',')[1]`. Check [this](https://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding)? – Sreekant Shenoy Apr 20 '21 at 03:36
  • @SreekantShenoy I update my question and I get another error like this ```"cannot identify image file <_io.BytesIO object at 0x11ba2c400>"``` is there a way to solve this? – wahyu Apr 20 '21 at 03:58
  • Can you update the question with a complete base64 string, because the base64 image doesn't seem valid? You have online tools to check if its valid. – Sreekant Shenoy Apr 20 '21 at 04:09
  • @SreekantShenoy here is the link for base64 string: https://gist.github.com/wahyu-handayani/3c754802631bb697f96d9189f8d6e07a, I have check it online to validate the base64 string and here is the result:```According to the specification, your string should have a padding character. This is optional, but sometimes it can lead to unexpected errors.``` – wahyu Apr 20 '21 at 04:11
  • That base64 string is not a valid image. Check [similar](https://stackoverflow.com/questions/48962033/php-base64-encode-function-for-image-gives-output-different-from-similar-functio/48962081) – Sreekant Shenoy Apr 20 '21 at 04:17
  • @SreekantShenoy, actually I get this base64 string from frontend (flutter) and then I send it using api python as backend for checking and evaluating, I will try to get another way to encode the image from flutter – wahyu Apr 20 '21 at 04:31
  • Yeah, just make sure its valid. – Sreekant Shenoy Apr 20 '21 at 04:34
  • @bad_coder I have tried the answer which have 84 upvote, but I get an error like this: ```cannot use a bytes pattern on a string-like object```. I also have tried for the rest of the answer there, but I still get another error – wahyu Apr 20 '21 at 06:14
  • @bad_coder I update my question, would you like to take a look? – wahyu Apr 20 '21 at 06:20
  • Looks like with your last update, you're using `b''` bytestrings in `re` but `base64_string` is a regular Unicode string. – Mark Ransom Apr 20 '21 at 12:22
  • @MarkRansom would you like to give an example to do that ? – wahyu Apr 21 '21 at 01:04
  • Just remove the `b` from the front of the string constants: `base64_string = re.sub(r'[^a-zA-Z0-9%s]+' % altchars, '', base64_string)`. – Mark Ransom Apr 21 '21 at 02:03
  • @MarkRansom I have tried it just now and got this message: ```can only concatenate str (not "bytes") to str``` – wahyu Apr 21 '21 at 02:19
  • Probably because `altchars` is still a byte string. Or maybe the `b'='` you have later. You need to be consistent in your string types everywhere. – Mark Ransom Apr 21 '21 at 02:33

0 Answers0