Skip to content

Instantly share code, notes, and snippets.

@htlin222
Created August 28, 2023 16:43
Show Gist options
  • Save htlin222/5a319c10011ca8a92cb93a1540e2dfc2 to your computer and use it in GitHub Desktop.
Save htlin222/5a319c10011ca8a92cb93a1540e2dfc2 to your computer and use it in GitHub Desktop.
upload clipboard image to imgur and get the return as markdown image syntax
import logging
import os
import subprocess
from pathlib import Path
import pyimgur
import pyperclip
# pip install Pillow
from PIL import ImageGrab
PILlogger = logging.getLogger("PIL")
PILlogger.setLevel(logging.CRITICAL)
CLIENT_ID = "GET YOU API key from: https://imgur.com/account/settings/apps"
IMAGE_PATH = f"/tmp/.tmp.png"
IMAGE = ImageGrab.grabclipboard()
def run_macos_notification(title, body):
"""Display a macOS notification with the specified title and body."""
command = (
f'osascript -e \'display notification with title "{title}" subtitle "{body}"\''
)
subprocess.run(command, shell=True)
def cmd_p():
command = f'osascript -e \'tell application "System Events" to keystroke "v" using command down\''
subprocess.run(command, shell=True)
def upload_image(image):
"""Check if image exists and upload."""
if image is not None:
run_macos_notification("🖼️ 上傳中 🆙", "跟我一起數萬, 吐, Three🎉")
image = image.save(IMAGE_PATH)
uploaded_image = pyimgur.Imgur(CLIENT_ID).upload_image(
IMAGE_PATH, title="Uploaded by PyImgur")
md_formated_result = f"\n![Figure]({uploaded_image.link})\n\n"
pyperclip.copy(md_formated_result)
os.remove(IMAGE_PATH)
run_macos_notification("🎉 登登!你的圖片網址來啦👇爽爽爽😁", md_formated_result)
# use this if you want cmd + p directly after the image uploaded
# cmd_p()
else:
run_macos_notification("📋 只有一般文字", "🌻 祝您有個美好的一天")
# cmd_p()
if __name__ == "__main__":
upload_image(IMAGE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment