画像をPythonスクリプトにドラッグして、自動的にMarkdown形式のリンクを生成します

7333 ワード

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 = '![{}]({})
    '
    .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}を通過することができる.この2つの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.