拖动图片到 Python 脚本,自动生成 Markdown 格式链接


由于用 Markdown 写作时,插入图片十分繁琐,于是诞生了本脚本。

效果预览

准备工作

  • 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)

源代码

你可以在 github 上找到它 markdown-helper

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}"

参考:将文件拖拽到.py文件上以处理它(从DropHandler说起)

相关工作

  • 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.

你可能感兴趣的:(拖动图片到 Python 脚本,自动生成 Markdown 格式链接)