Advertisement
DECROMAX

Image from URL

Aug 20th, 2022 (edited)
1,380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. import import PySimpleGUI as sg
  2. from PIL import Image
  3. from io import BytesIO
  4.    
  5. def disp_image(val: str) -> Union[None, str]:
  6.     """Arg: url of image, Returns .png image for use in PySimpleGUI"""
  7.     try:
  8.         image = Image.open(requests.get(val, stream=True).raw)
  9.         image.thumbnail((300, 300)) # resize as required
  10.         bio = io.BytesIO()
  11.         image.save(bio, format='PNG')
  12.         return bio.getvalue()
  13.    
  14.     except requests.exceptions.RequestException as e:  
  15.         return f"Error {e}"
  16.  
  17.     """Example of use"""
  18.  
  19.     layout = [[sg.Image(data=disp_image(vals.image))]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement