Advertisement
Guest User

Untitled

a guest
Nov 29th, 2022
264
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 1 0
  1. from io import BytesIO
  2. from random import randint
  3. import requests
  4. import urllib.request
  5. import wget
  6. from tkinter import *
  7. from PIL import ImageTk, Image
  8.  
  9. api_key = "your api key"
  10. username = "your username"
  11. collection_id = #the id number of the collection you want to view
  12.  
  13. base_url = "https://wallhaven.cc/api/v1/"
  14. opener=urllib.request.build_opener()
  15. opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
  16. urllib.request.install_opener(opener)
  17.  
  18. def getRandomWall():
  19. global static_path
  20. url = "{}collections/{}/{}?apikey={}&page={}".format(base_url, username, collection_id, api_key, 0)
  21. r = requests.get(url)
  22. meta = r.json()['meta']
  23. last_page = meta['last_page']
  24. per_page = meta['per_page']
  25. total = meta['total']
  26. wall = randint(1,total)
  27. wall_page = 1
  28. while wall_page * per_page < wall:
  29. wall_page = wall_page + 1
  30. wall_page_offset = wall - ((wall_page - 1) * per_page)
  31. r = requests.get("{}collections/{}/{}?apikey={}&page={}".format(base_url, username, collection_id, api_key, wall_page))
  32. static_path = r.json()['data'][wall_page_offset - 1]['path']
  33. return static_path
  34.  
  35. def saveWallCallback(e):
  36. wget.download(static_path)
  37.  
  38. def getWallBytes(path):
  39. global img_data
  40. response = requests.get(path)
  41. img_data = response.content
  42. bg = ImageTk.PhotoImage(Image.open(BytesIO(img_data)))
  43. return bg
  44.  
  45. window=Tk()
  46. window.title('Hello Python')
  47. window.geometry("1280x720")
  48. bg = getWallBytes(getRandomWall())
  49. my_canvas = Canvas(window)
  50. my_canvas.pack(fill="both", expand=True)
  51.  
  52. class fauxEvent:
  53. def __init__(self, width, height) -> None:
  54. self.width = width
  55. self.height = height
  56. pass
  57.  
  58. def changeWallCallback(e):
  59. getWallBytes(getRandomWall())
  60. faux_event = fauxEvent(window.winfo_width(),window.winfo_height())
  61. resizerCallback(faux_event)
  62.  
  63. def resizerCallback(e):
  64. global bg1, resized_bg, new_bg
  65. bg1 = Image.open(BytesIO(img_data))
  66. ratio = bg1.size[0] / bg1.size[1]
  67. if e.width / ratio > e.height:
  68. w = e.height * ratio
  69. h = e.height
  70. elif e.height * ratio > e.width:
  71. w = e.width
  72. h = e.width / ratio
  73. else:
  74. w = e.width
  75. h = e.height
  76. w = round(w)
  77. h = round(h)
  78. resized_bg = bg1.resize((w, h), Image.ANTIALIAS)
  79. new_bg = ImageTk.PhotoImage(resized_bg)
  80. my_canvas.create_image(round(e.width/2),round(e.height/2), image=new_bg) #image centered on screen
  81. pass
  82.  
  83. window.bind('<Configure>', resizerCallback)
  84. my_canvas.bind("<Button-1>", changeWallCallback)
  85. my_canvas.bind("<Button-3>", saveWallCallback)
  86. my_canvas.pack()
  87. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement