def process(): """主流程""" # 检查七牛相关配置是否已配置 if not all((URI_PREFIX, ACCESS_KEY, SECRET_KEY, BUCKET_NAME)): notice('请先设置七牛相关配置!') open_with_editor('config.py') return try: img_path = clipboard.get_pasteboard_img_path() except CLIPBOARD_EXCEPTIONS as error: notice(str(error)) return file_name = os.path.split(img_path)[-1] file_type = file_name.split('.')[-1] if file_type == 'tiff': new_img_path = '/tmp/{}.png'.format(int(time.time())) # tiff --> png _convert_to_png(img_path, new_img_path) img_path = new_img_path else: new_img_path = '/tmp/{}-{}.{}'.format(file_name, int(time.time()), file_type) os.system('cp {} {}'.format(img_path, new_img_path)) img_path = new_img_path # 获取图片尺寸 # width, height = _get_img_size(img_path) try: # 上传到七牛 upload_result = uploader.upload(img_path, ACCESS_KEY, SECRET_KEY, BUCKET_NAME) if not upload_result: notice('上传图片到七牛失败,请检查七牛相关配置是否正确!') return # 完整的七牛图片URI img_file_name = os.path.split(img_path)[-1] img_uri = '{}/{}'.format(URI_PREFIX, img_file_name) notice('上传成功!') except Exception as error: notice('上传图片到七牛异常!{}'.format(str(error))) return # markdown使用html格式,保证图片大小 markdown_img = IMG_TPL.format(img_uri, SCALE_RATE) # 写入剪贴板 write_to_pasteboard(markdown_img) # 打印出markdown格式的图片地址 print_pasteboard_content()
def process(): """主流程""" # 检查相关配置是否已配置 if not all((GITHUB_REPO,LOCAL_GIT_REPO_PATH,SUBDIR)): notice('请先设置相关配置!') open_with_editor('config.py') return try: img_path = clipboard.get_pasteboard_img_path() except CLIPBOARD_EXCEPTIONS as error: notice(str(error)) return file_name = os.path.split(img_path)[-1] file_type = file_name.split('.')[-1] if file_type == 'tiff': new_img_path = '/tmp/{}.png'.format(int(time.time())) # tiff --> png _convert_to_png(img_path, new_img_path) img_path = new_img_path # 获取图片尺寸 width, height = _get_img_size(img_path) try: cmd = 'cp {} {}'.format(img_path, LOCAL_GIT_REPO_PATH + '/' + SUBDIR, LOCAL_GIT_REPO_PATH) os.system(cmd) # 完整的图片URI img_file_name = os.path.split(img_path)[-1] img_uri = '{}/{}/{}'.format(GITHUB_REPO, SUBDIR, img_file_name) notice('上传成功!') except Exception as error: notice('上传图片异常!{}'.format(str(error))) return # markdown使用html格式,保证图片大小 markdown_img = IMG_TPL.format(img_file_name, img_uri) # 写入剪贴板 write_to_pasteboard(markdown_img) # 打印出markdown格式的图片地址 print_pasteboard_content()
def upload_img2github(): if not check_config(): return img_path = get_pasteboard_img_path() if img_path: notify("开始上传图片到github,请稍等") result = upload2github() if result: notify(result) os.remove(img_path) else: # width, height = get_img_size(img_path) image_name = os.path.split(img_path)[-1] # markdown_img = IMG_TPL.format(image_url(image_name), width, height) markdown_img = IMG_TPL.format(image_url(image_name)) print markdown_img write_to_pasteboard(markdown_img) notify('Markdown格式Image已在Clipboard')