code: def get_img_data(f, maxsize = (1200, 850), first = False):
      
      img = Image.open(f)
      img.thumbnail(maxsize)
      if first:                     # tkinter is inactive the first time
          bio = io.BytesIO()
          img.save(bio, format = "PNG")
          del img
          return bio.getvalue()
      return ImageTk.PhotoImage(img)
docs: Generate image data using PIL