1. 程式人生 > >拖動圖片到 Python 指令碼,自動生成 Markdown 格式連結

拖動圖片到 Python 指令碼,自動生成 Markdown 格式連結

由於用 Markdown 寫作時,插入圖片十分繁瑣,於是誕生了本指令碼。

效果預覽

printscreen.gif

準備工作

  • Python 2.7
  • 騰訊雲 cos SDK
  • Tinify
pip install qcloud_cos_v4 tinify

使用騰訊雲 SDK

初始化

appid = 100000                  # 替換為使用者的appid
secret_id = u'xxxxxxxx'         # 替換為使用者的secret_id
secret_key = u'xxxxxxx'         # 替換為使用者的secret_key
region_info = "sh"
# 替換為使用者的region,例如 sh 表示華東園區, gz 表示華南園區, tj 表示華北園區 cos_client = CosClient(appid, secret_id, secret_key, region=region_info)

上傳圖片

request = UploadFileRequest(bucket, u'/sample_file.txt', u'local_file_1.txt')
upload_file_ret = cos_client.upload_file(request)

複製到剪下板

echo xxxxx | clip

用 Tinify 壓縮圖片

Authentication

import tinify
tinify.key = "YOUR_API_KEY"

壓縮圖片

source = tinify.from_file("unoptimized.jpg")
source.to_file("optimized.jpg")

Sample

#! /usr/bin/python
#-*- coding: utf-8 -*-

from qcloud_cos import CosClient, UploadFileRequest
import sys
import os
# import msvcrt
os.chdir(sys.path[0]) # 設定使用者屬性, 包括appid, secret_id和secret_key # 這些屬性可以在cos控制檯獲取(https://console.qcloud.com/cos) appid = 100000 # 替換為使用者的appid secret_id = u'xxxxxxxx' # 替換為使用者的secret_id secret_key = u'xxxxxxx' # 替換為使用者的secret_key region_info = "sh" # # 替換為使用者的region,目前可以為 sh/gz/tj/sgp,分別對應於上海,廣州,天津,新加坡園區 # 設定要操作的bucket bucket = u'mybucket' md_url_result = "md_url.txt" # img_suffix = ["jpg", "jpeg", "png", "bmp", "gif"] # 上傳圖片 def upload_img(bucket, file_name, local_file): request = UploadFileRequest(bucket, u'/mdimg/{}'.format(file_name), u'{}'.format(local_file)) request.set_insert_only(0) # 設定允許覆蓋 cos_client.upload_file(request) # upload_file_ret = cos_client.upload_file(request) # print 'upload file ret:', repr(upload_file_ret) def get_img_url(bucket, file_name): # 預設 # 進入 COS 管理控制檯,點選 Bucket 名稱進入管理頁面,選擇【域名管理】頁籤,即可以看到預設域名 # img_url_default = 'http://{}-{}.cos{}.myqcloud.com/{}'.format(bucket, str(appid), region_info, file_name) # 加速 img_url_CDN = 'http://{}-{}.file.myqcloud.com/mdimg/{}'.format(bucket, str(appid), file_name) md_url = '![{}]({})\n'.format(file_name, img_url_CDN) return md_url def save_to_txt(bucket, file_name): url_before_save = get_img_url(bucket, file_name) # save to clipBoard addToClipBoard(url_before_save) # save md_url to txt with open(md_url_result, "a") as f: f.write(url_before_save) return 0 # save to clipboard def addToClipBoard(text): command = 'echo ' + text.strip() + '| clip' # print command os.system(command) if __name__ == '__main__': cos_client = CosClient(appid, secret_id, secret_key, region = region_info) imgs = sys.argv[1:] for img in imgs: # name for img with local time up_filename = os.path.split(img)[1] upload_img(bucket, up_filename, img) save_to_txt(bucket, up_filename)

原始碼

Tips

如果拖動沒有反應,可以通過{60254CA5-953B-11CF-8C96-00AA00B8708C}{86C86720-42A0-1069-A2E8-08002B30309D} 。這兩個 DropHandler 專案來控制某種副檔名上的拖拽效果。

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
@="{86C86720-42A0-1069-A2E8-08002B30309D}"
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
@="{60254CA5-953B-11CF-8C96-00AA00B8708C}"

相關工作

  • markdown-helper Drop images on python script, get markdown url in txt file.
  • WriteMarkdownLazily
    This is a Python script which using for changing references of local image source files in Markdown file to urls.