How to convert Image to base64

Hello,

I am trying to convert image to base64, to be used further with my ML algorithm, but continuously getting failed on the first step itself.

Cannot understand what the issue is !! Please review the below mentioned code snippet and guide me.

import base64
  
with open("pic.jpg", "r") as img_to_str:
    str = base64.b64encode(img_to_str.read())
  
with open('base64_str.bin', "w") as file:
    file.write(str)

Error

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_31208/2471352181.py in <module>
      2 
      3 with open("pic.jpg", "r") as img_to_str:
----> 4     str = base64.b64encode(img_to_str.read())
      5 
      6 with open('base64_str.bin', "w") as file:

C:\Program Files\Python310\lib\encodings\cp1252.py in decode(self, input, final)
     21 class IncrementalDecoder(codecs.IncrementalDecoder):
     22     def decode(self, input, final=False):
---> 23         return codecs.charmap_decode(input,self.errors,decoding_table)[0]
     24 
     25 class StreamWriter(Codec,codecs.StreamWriter):

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 46: character maps to <undefined>