title: How to display created thumbnail image in tkinter without saving it first?
body: <p>I'm trying to display a thumbnail image in tkinter by saving the image in memory using <code>io.BytesIO()</code>. I'm getting <code>_tkinter.TclError: format error in bitmap data</code>. The <code>BitmapImage</code> <a href="http://effbot.org/tkinterbook/bitmapimage.htm" rel="nofollow noreferrer">supports</a> X11 bitmap image. So my question is how to convert my images to <code>xbm</code> before pass it to <code>BitmapImage</code> method?</p>
  
  <pre><code>from PIL import Image, ImageTk
  import tkinter.scrolledtext as tkst
  import tkinter as tk
  import os, os.path
  import io
  
  class Example(tk.Frame):
      def __init__(self, master):
          tk.Frame.__init__(self, master)
  
      img = Image.open("1.jpg")
      image_size = 256, 256
      img.thumbnail(image_size,Image.ANTIALIAS)
      b = io.BytesIO()
      img.save(b, 'gif')
      p = b.getvalue()
      photo = tk.BitmapImage(data=p)
      self.photo = photo
      self.imageview = tk.Label(self, image = self.photo, background="black")
      self.imageview.grid(row=0, column=0, rowspan=2, sticky="nsew")
  
  if __name__ == "__main__":
      root = tk.Tk()
      Example(root).pack(fill="both", expand=True)
      root.geometry("800x400")
      root.mainloop()
  </code></pre>
  
  <p>Last time, I solved the problem using this code but sometimes the height of the image will get cut off.</p>
  
  <pre><code>    image = Image.open("1.jpg")
      basewidth = 900
      wpercent = (basewidth/float(image.size[0]))
      hsize = int((float(image.size[1])*float(wpercent)))
      image = image.resize((basewidth,hsize),Image.ANTIALIAS)
      self.photo = ImageTk.PhotoImage(image)
      self.imageview.configure(image=self.photo)
  </code></pre>