Skip to content

Instantly share code, notes, and snippets.

@Jinnrry
Created February 3, 2021 08:51
Show Gist options
  • Save Jinnrry/3104280ce7f5c3382047f97e251cda25 to your computer and use it in GitHub Desktop.
Save Jinnrry/3104280ce7f5c3382047f97e251cda25 to your computer and use it in GitHub Desktop.
监听剪切板展示图片
import pyperclip
import time
import requests
from PIL import Image
class Listen():
def isImgUrl(self, txt):
# 判断是否是url
txt = str.strip(txt)
return txt[0:4] == 'http'
def downloadImg(self, url):
# 下载图片
res = requests.get(url)
with open("tmp.png", "wb") as code:
code.write(res.content)
def clipboard_get(self):
"""获取剪贴板数据"""
data = pyperclip.paste()
return data
def show_img(self):
# img = mpimg.imread('tmp.png')
# plt.imshow(img) # 显示图片
# plt.axis('off') # 不显示坐标轴
# plt.show()
im = Image.open('tmp.png')
im.show()
def main(self):
last_txt = ""
while True:
# txt 存放当前剪切板文本
txt = self.clipboard_get()
if last_txt != txt:
last_txt = txt
if self.isImgUrl(txt):
self.downloadImg(txt)
self.show_img()
time.sleep(0.5)
if __name__ == '__main__':
Listen().main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment