Skip to content

Commit

Permalink
directly copy file to upload complete
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Jan 29, 2016
1 parent 3ae3ed8 commit 90afb25
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.DS_Store
*pyc
*.pyc
config.ini
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ markdown图片上传&插入实用工具
2. 支持retina的截图处理,在非retina的显示器上不会变大
3. 自动图片上传,失败通知栏通知
4. 支持多种截图格式,压缩过大图片
5. 方便的图片上传工具

## 使用

Expand Down Expand Up @@ -48,7 +49,7 @@ markdown图片上传&插入实用工具

## TODO
1. 支持剪切版里面的文本格式的图片链接先下载然后上传到图床
2. 如果复制的直接时图片文件,能直接上传生成URL.
2. ~~如果复制的直接时图片文件,能直接上传生成URL.~~
2. ~~图片过大时自动压缩~~
3. ~~qq截图的tiff格式暂时有bug~~
4. ~~临时文件夹不使用/tmp 用TempFile解决。~~
50 changes: 38 additions & 12 deletions clipboard.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
# -*- coding: utf-8 -*-
import os
import tempfile
import imghdr
import shutil

from AppKit import NSPasteboard, NSPasteboardTypePNG, NSPasteboardTypeTIFF, NSPasteboardTypeString
from AppKit import NSPasteboard, NSPasteboardTypePNG,\
NSPasteboardTypeTIFF, NSPasteboardTypeString,\
NSFilenamesPboardType

NONE_IMG = (None, False)

def _convert_to_png(from_path, to_path):
# convert it to png file
os.system('sips -s format png %s --out %s' % (from_path, to_path))

def get_paste_img_file():
''' get a img file from clipboard;
Expand All @@ -19,29 +29,45 @@ def get_paste_img_file():
png_file = tempfile.NamedTemporaryFile(suffix="png")

supported_image_format = (NSPasteboardTypePNG, NSPasteboardTypeTIFF)
if NSFilenamesPboardType in data_type:
# file in clipboard
img_path = pb.propertyListForType_(NSFilenamesPboardType)[0]
img_type = imghdr.what(img_path)

if not img_type:
# not image file
return NONE_IMG

if img_type not in ('png', 'jpeg'):
# now only support png & jpg
return NONE_IMG

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)
tmp_clipboard_img_file.close()
return png_file, False

if NSPasteboardTypeString in data_type:
# make this be first, because plain text may be TIFF format?
# string todo, recognise url of png & jpg
pass
elif any(filter(lambda f: f in data_type, supported_image_format)):

if any(filter(lambda f: f in data_type, supported_image_format)):
# do not care which format it is, we convert it to png finally
# system screen shotcut is png, QQ is tiff
tmp_clipboard_img_file = tempfile.NamedTemporaryFile()
print tmp_clipboard_img_file.name
data = pb.dataForType_(NSPasteboardTypePNG)
for fmt in supported_image_format:
data = pb.dataForType_(fmt)
if data: break
ret = data.writeToFile_atomically_(tmp_clipboard_img_file.name, False)
if not ret: return

# convert it to png file
os.system('sips -s format png %s --out %s' % (tmp_clipboard_img_file.name, png_file.name))
if not ret: return NONE_IMG

_convert_to_png(tmp_clipboard_img_file.name, png_file.name)
# close the file explicitly
tmp_clipboard_img_file.close()
return png_file
return png_file, True

if __name__ == '__main__':
get_paste_img_file()




Binary file modified clipboard.pyc
Binary file not shown.

0 comments on commit 90afb25

Please sign in to comment.