diff --git a/README.md b/README.md index 9c8b8f9..df1e768 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,5 @@ ## TODO -0. 添加"带有md格式的复制"选项: `[![imgName](base64)](imgurl)` 1. 本地中文文件名压缩转换 2. 转换参数可配置 \ No newline at end of file diff --git a/guistarter.py b/guistarter.py index 57f2873..19e047b 100644 --- a/guistarter.py +++ b/guistarter.py @@ -39,13 +39,18 @@ def createWidgets(self): self.Combo1List = ['不压缩','压缩至webp','压缩至png'] self.Combo1 = ttk.Combobox(self.top,state="readonly", values=self.Combo1List, font=('微软雅黑',9)) self.Combo1.current(0) - self.Combo1.place(relx=0.040, rely=0.300, relwidth=0.400, relheight=0.150) + self.Combo1.place(relx=0.040, rely=0.300, relwidth=0.350, relheight=0.150) # 选择框 self.auto_compress = StringVar(value='1') self.style.configure('Check1.TCheckbutton',font=('微软雅黑',9)) self.Check1 = ttk.Checkbutton(self.top, text='小于60k不压缩', variable=self.auto_compress, style='Check1.TCheckbutton') - self.Check1.place(relx=0.480, rely=0.300, relwidth=0.400, relheight=0.150) + self.Check1.place(relx=0.420, rely=0.300, relwidth=0.300, relheight=0.150) + + self.with_md = StringVar(value='1') + self.style.configure('Check1.TCheckbutton',font=('微软雅黑',9)) + self.Check1 = ttk.Checkbutton(self.top, text='md格式', variable=self.with_md, style='Check1.TCheckbutton') + self.Check1.place(relx=0.760, rely=0.300, relwidth=0.200, relheight=0.150) self.copytext = StringVar(value='点击复制') self.style.configure('Command2.TButton',font=('微软雅黑',9)) @@ -69,8 +74,9 @@ def doupload(self, event=None): checkdata = self.Combo1.get() checkindex = self.Combo1List.index(checkdata) ifauto = int(self.auto_compress.get()) + with_md = int(self.with_md.get()) - self.result,showlen = mytool.work_file(local_file_path,checkindex,ifauto) + self.result,showlen = mytool.work_file(local_file_path,checkindex,ifauto,with_md) if len(self.result)>50: addToClipBoard(self.result) self.copytext.set("点击复制, "+showlen) @@ -86,8 +92,12 @@ def dotrans(self, event=None): checkdata = self.Combo1.get() checkindex = self.Combo1List.index(checkdata) ifauto = int(self.auto_compress.get()) + with_md = int(self.with_md.get()) + + if len(dataurl)==0: + return - self.result,showlen = mytool.work_url(dataurl,checkindex,ifauto) + self.result,showlen = mytool.work_url(dataurl,checkindex,ifauto,with_md) if len(self.result)>50: addToClipBoard(self.result) self.copytext.set("点击复制, "+showlen) diff --git a/mytool.py b/mytool.py index 131f3be..fc34c37 100644 --- a/mytool.py +++ b/mytool.py @@ -35,8 +35,16 @@ def _base64_getheader(filename): ex = os.path.splitext(filename)[1] return imagemapping.base64headers[ex] +def _with_md(result_line,imgname,url=None): + '''为base64结果添加md格式''' + if url is None: + pass + return "![{}]({})".format(imgname,result_line) + else: + return "[![{}]({})]({})".format(imgname,result_line,url) + -def work_url(url,index,ifauto): +def work_url(url,index=0,ifauto=1,with_md=0): '''0不压缩,1webp,2png''' response,imgname = downloader.get_response_imgname(url) bytes = response.read() @@ -45,7 +53,7 @@ def work_url(url,index,ifauto): index = 0 if index==0: result_line = dobase64_with_bytes(bytes,imgname) - showlen = "nochange {}k".format(s,s) + showlen = "nochange {}k".format(s) else: source_path = psworkspace+imgname downloader.download_by_bytes(bytes,source_path) @@ -60,9 +68,11 @@ def work_url(url,index,ifauto): showlen = "img_size {}k to {}k".format(s,r) os.remove(source_path) os.remove(result_path) + if with_md: + result_line = _with_md(result_line,imgname,url) return result_line,showlen -def work_file(source_path,index,ifauto): +def work_file(source_path,index=0,ifauto=1,with_md=0): '''0不压缩,1webp,2png''' imgname = os.path.basename(source_path) s = "{:.2f}".format(os.path.getsize(source_path)/1024.0) @@ -70,7 +80,7 @@ def work_file(source_path,index,ifauto): index = 0 if index==0: result_line = dobase64(source_path) - showlen = "nochange {}k".format(s,s) + showlen = "nochange {}k".format(s) else: is_to_png = index==2 if is_to_png: @@ -82,6 +92,8 @@ def work_file(source_path,index,ifauto): r = "{:.2f}".format(os.path.getsize(result_path)/1024.0) showlen = "img_size {}k to {}k".format(s,r) os.remove(result_path) + if with_md: + result_line = _with_md(result_line,imgname) return result_line,showlen