diff --git a/README.md b/README.md index 0e110ba..b0bac41 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,15 @@ - 支持多种截图格式,压缩过大图片 - 方便的图片上传工具 -## 使用 +## 使用前准备 + +### 安装依赖 + +由于七牛SDK使用了`requests`库进行网络请求,所以,**首先得安装python的`requests`库**;推荐使用pip进行安装。 + +> 注意:是`requests`库,而不是`request`库,安装错误则无法正常使用。 -### 设置七牛图床 +### 获取七牛图床信息 #### 注册七牛 选择使用七牛的图床,没有账号的话先[注册](https://portal.qiniu.com/signup?code=3ldifp9oti442); @@ -56,20 +62,29 @@ 然后,点击左边的**密钥**就可以看到你的**AK**以及**SK** -以上图床的信息拿到之后,在alfred里面输入mdimgsetup,就会弹出一个文本文档,如下: +### 配置 +#### 安装 Alfred 工作流 + +- 首先请确认`request`库安装成功; +- 然后下载并导入项目目录中的 Alfred 工作流文件; +- **设置触发热键!**,如`Cmd + Ctrl + V`,注意保证不要与其他软件的热键冲突。 + +#### 配置图床 + +在前面,图床的信息拿到之后,在 alfred 里面输入`mdimgsetup`,就会弹出一个文本文档,如下: -设置你的七牛图床的信息,AK,SK是访问密钥,url是上面配置的图床访问地址,bucket是空间名字,prefix是图床上传的前缀,这个可以随意配置,作为分类使用,比如我的时markdown +设置你的七牛图床的信息,AK,SK是访问密钥,url是上面配置的图床访问地址,bucket是空间名字,prefix是图床上传的前缀,这个可以随意配置,作为分类使用,比如我的是 markdown。 -### 使用 +## 使用 -#### 通过截图上传 +### 通过截图上传 使用任意截图工具截图之后,在任意编辑器里面你需要插入markdown格式图片的地方,按下cmd + ctrl + V即可! 另外,markdwon里面的图片链接不是标准的markdown格式,而是html的img标签;这是因为在retina屏幕下截图的话,如果不做任何处理,在非retina屏幕下面,这个图片会直接扩大两倍,非常粗糙难看;因此,需要保存图片显示大小的信息,保证截图大小和显示大小一致;这里使用mac系统自带的sips工具拿到截图大小,然后直接把宽度放在img标签里面。这样在显示的时候,可以保证是和截图时大小一致。 -#### 通过本地图片上传 +### 通过本地图片上传 如果你已经有一张图片了,希望上传到图床得到一个链接;通常的方式需要图床客户端或者浏览器插件,通过这个alfred插件: diff --git a/clipboard.py b/clipboard.py index a372f98..d5b0067 100644 --- a/clipboard.py +++ b/clipboard.py @@ -8,7 +8,8 @@ NSPasteboardTypeTIFF, NSPasteboardTypeString,\ NSFilenamesPboardType -NONE_IMG = (None, False) +# image_file, need_format, need_compress +NONE_IMG = (None, False, None) def _convert_to_png(from_path, to_path): # convert it to png file @@ -23,10 +24,6 @@ def get_paste_img_file(): pb = NSPasteboard.generalPasteboard() data_type = pb.types() - # if img file - print data_type - # always generate png format img - png_file = tempfile.NamedTemporaryFile(suffix="png") supported_image_format = (NSPasteboardTypePNG, NSPasteboardTypeTIFF) if NSFilenamesPboardType in data_type: @@ -38,15 +35,20 @@ def get_paste_img_file(): # not image file return NONE_IMG - if img_type not in ('png', 'jpeg'): - # now only support png & jpg + if img_type not in ('png', 'jpeg', 'gif'): + # now only support png & jpg & gif return NONE_IMG + is_gif = img_type == 'gif' + _file = tempfile.NamedTemporaryFile(suffix=img_type) tmp_clipboard_img_file = tempfile.NamedTemporaryFile() shutil.copy(img_path, tmp_clipboard_img_file.name) - _convert_to_png(tmp_clipboard_img_file.name, png_file.name) + if not is_gif: + _convert_to_png(tmp_clipboard_img_file.name, _file.name) + else: + shutil.copy(tmp_clipboard_img_file.name, _file.name) tmp_clipboard_img_file.close() - return png_file, False + return _file, False, 'gif' if is_gif else 'png' if NSPasteboardTypeString in data_type: # make this be first, because plain text may be TIFF format? @@ -58,6 +60,7 @@ def get_paste_img_file(): # system screen shotcut is png, QQ is tiff tmp_clipboard_img_file = tempfile.NamedTemporaryFile() print tmp_clipboard_img_file.name + png_file = tempfile.NamedTemporaryFile(suffix='png') for fmt in supported_image_format: data = pb.dataForType_(fmt) if data: break @@ -67,7 +70,7 @@ def get_paste_img_file(): _convert_to_png(tmp_clipboard_img_file.name, png_file.name) # close the file explicitly tmp_clipboard_img_file.close() - return png_file, True + return png_file, True, 'png' if __name__ == '__main__': get_paste_img_file() diff --git a/clipboard.pyc b/clipboard.pyc deleted file mode 100644 index 2dafb04..0000000 Binary files a/clipboard.pyc and /dev/null differ diff --git a/config.ini b/config.ini index 8d76d68..85c05c5 100644 --- a/config.ini +++ b/config.ini @@ -1,7 +1,7 @@ ; 详细设置见 https://github.com/tiann/markdown-img-upload [qiniu] -ak=XI-kICmu9FqcTxOwDF9YMLNKH05AbkqW0WAKkmgR -sk=DTD8ZshbK47HZWNiJyKfXY2tIE8fkLTpFXuHKN4J -url=http://7xp3xc.com1.z0.glb.clouddn.com -bucket=blog -prefix=201601 \ No newline at end of file +ak=你的七牛Access Key +sk=你的七牛Secret Key +url=你注册的七牛图床地址 +bucket=你的七牛空间名 +prefix=图片前缀,方面区分和查找 diff --git a/info.plist b/info.plist index a13bfdd..ff6261d 100644 --- a/info.plist +++ b/info.plist @@ -98,12 +98,12 @@ if not config: url = '%s/%s' % (config['url'], config['prefix']) -img_file, need_format = get_paste_img_file() +img_file, need_format, format = get_paste_img_file() if img_file: # has image # use time to generate a unique upload_file name, we can not use the tmp file name - upload_name = "%s.png" % int(time.time() * 1000) + upload_name = "%s.%s" % (int(time.time() * 1000), format) if need_format: size_str = subprocess.check_output('sips -g pixelWidth %s | tail -n1 | cut -d" " -f4' % img_file.name, shell=True) size = int(size_str.strip()) / 2 @@ -114,7 +114,7 @@ if img_file: # make it to clipboard os.system("echo '%s' | pbcopy" % markdown_url) os.system('osascript -e \'tell application "System Events" to keystroke "v" using command down\'') - upload_file = util.try_compress_png(img_file) + upload_file = util.try_compress_png(img_file, format!='gif') if not upload_qiniu(upload_file.name, upload_name): util.notice("上传图片到图床失败,请检查网络后重试") else: util.notice("剪切版里没有图片!") diff --git a/markdown img.alfredworkflow b/markdown img.alfredworkflow index efe7e5d..4392bf8 100644 Binary files a/markdown img.alfredworkflow and b/markdown img.alfredworkflow differ diff --git a/untitled b/untitled new file mode 100644 index 0000000..55cac14 --- /dev/null +++ b/untitled @@ -0,0 +1,8 @@ +import os +def notice(msg, title="notice"): + ''' notoce message in notification center''' + print "notice: %s" % msg + os.system('osascript -e \'display notification "%s" with title "%s"\'' % (msg, title)) + +if __name__ == '__main__': + notice("test image") \ No newline at end of file diff --git a/upload.pyc b/upload.pyc deleted file mode 100644 index 6d81697..0000000 Binary files a/upload.pyc and /dev/null differ diff --git a/util.py b/util.py index b5d70a6..574c945 100644 --- a/util.py +++ b/util.py @@ -44,9 +44,10 @@ def generate_config_file(): with open(CONFIG_FILE, 'w') as fp: fp.write(textwrap.dedent(config_file_init_content)) -def try_compress_png(raw_img): +def try_compress_png(raw_img, need_compress): ''' use pngquant to compress:https://github.com/pornel/pngquant''' + if not need_compress: return raw_img if not os.path.exists(raw_img.name): return raw_img tmp_file = NamedTemporaryFile() return tmp_file if not subprocess.call('pngquant/pngquant --force %s -o %s' \ - % (raw_img.name, tmp_file.name), shell=True) else raw_img \ No newline at end of file + % (raw_img.name, tmp_file.name), shell=True) else raw_img diff --git a/util.pyc b/util.pyc deleted file mode 100644 index c4d2dec..0000000 Binary files a/util.pyc and /dev/null differ