Advertisement
steve-shambles-2109

Display thumbnail of clipboard image.

Jan 6th, 2020
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. """Display thumbnail of clipboard image.
  2. stevepython.wordpress.com
  3. code-snippet-vol_41-snip_204
  4.  
  5. By Steve Shambles
  6.  
  7. required:
  8. pip3 install pillow
  9. """
  10.  
  11. from tkinter import Label, Tk
  12. import webbrowser
  13.  
  14. from PIL import Image, ImageGrab, ImageTk
  15.  
  16. root = Tk()
  17.  
  18. def grab_cb_img():
  19.     """Grab image from clipboard, create thumb and display with original"""
  20.     img = ImageGrab.grabclipboard()
  21.     if img is not None:
  22.         # Save the image.
  23.         img.save('cbimage.jpg')
  24.  
  25.         # Create frame for the image.
  26.         img_frame = root
  27.         img_frame.grid()
  28.         img_frame.title('Thumbnail')
  29.         img_frame.resizable(False, False)
  30.  
  31.         # Load back image.
  32.         img = Image.open('cbimage.jpg')
  33.  
  34.         # Make a 256x256 thumbnail and display.
  35.         img.thumbnail((256, 256), Image.ANTIALIAS)
  36.         PHOTO = ImageTk.PhotoImage(img)
  37.         la_bel = Label(img_frame, image=PHOTO)
  38.         la_bel.img = PHOTO
  39.         la_bel.grid()
  40.         webbrowser.open('cbimage.jpg')
  41.     else:
  42.         root.destroy()
  43.         print('No image found on clipboard.')
  44.  
  45. grab_cb_img()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement