AutoHotkey + ImageMagick 实现 剪贴版图像复制 / PNG 转 JPG 图像压缩
ref
https://www.jianshu.com/p/ae048b5090f8
点击查看代码
#SingleInstance, Force
global DEFAULT_IMAGE_FOLDER := "D:\Data\Programs\UserData\AutoHotkey\tempImages"
global DEFAULT_LOG_FOLDER := "D:\Data\Programs\UserData\AutoHotkey\tempImages"
global local_img
global clip_type
#Include, Gdip_All.ahk
; 压缩剪贴版图片
OnClipboardChange("ClipChanged")
#Persistent
ClipChanged(Type) {
clip_type := Type
}
!^+\::
compress_img_in_clip()
; picgo
RunWait, PowerShell.exe -Command &{picgo u} 2>&1 >> %DEFAULT_LOG_FOLDER%\ps.txt, , Max, ; Run Powershell Script
TrayTip, 图片已上传至gitee,
Return
!^\::
compress_img_in_clip()
Return
compress_img_in_clip(){
; TODO 剪贴版或者本地文件
if(clip_type=1)
{
; Msgbox, 压缩本地图片
copied_file := clipboard
len:=StrLen(copied_file)-3
if(len<3)
return
pos0:=InStr(copied_file,".jpg")
pos1:=InStr(copied_file,".png")
pos2:=InStr(copied_file,".bmp")
pos3:=InStr(copied_file,".gif")
if(pos0=len) or (pos1=len) or (pos2=len) or (pos3=len)
{
}
else
{
return
}
AttributeString := FileExist(copied_file)
if(AttributeString="")
{
return
}
; 文件复制到仓库
SplitPath, copied_file, name, dir, ext, name_no_ext, outdrive
FileCopy, %copied_file%, %DEFAULT_IMAGE_FOLDER%
local_img = %DEFAULT_IMAGE_FOLDER%\%name%
}
; 剪切板有图片内容
if(clip_type=2)
{
; msgbox, 压缩剪贴版图片
FormatTime, TimeString , , yyMMddhhmmss
;local_img 是本地图片的完整路径
local_img:= DEFAULT_IMAGE_FOLDER "\"TimeString ".png"
; 保存剪切板的图片
; MsgBox, %local_img%
createPicFromClip(local_img)
}
; 剪切板接收路径
; clipboard := compress_by_ImageMagick(local_img)
; msgbox, 图片路径: %local_img%
compressed_img_path := compress_by_ImageMagick(local_img)
pToken := Gdip_Startup()
Gdip_SetBitmapToClipboard(pBitmap := Gdip_CreateBitmapFromFile(local_img))
Gdip_DisposeImage(pBitmap)
Traytip, 压缩图片已复制到剪贴版中, `n可删除本地文件 %compressed_img_path%
Gdip_Shutdown(pToken)
Return %compressed_img_path%
}
compress_by_ImageMagick(img_path){
; https://www.xiaoz.me/archives/11581
SplitPath, img_path, name, dir, ext, name_no_ext, outdrive
script =
(
$CONFIG_PATH = '%DEFAULT_LOG_FOLDER%'
$TRUE_FALSE= Test-Path $CONFIG_PATH
if($TRUE_FALSE -eq $False)
{
New-Item '%DEFAULT_LOG_FOLDER%' -type Directory
}
D:\ProgramFiles\ImageMagick-7.1.0-Q16-HDRI\magick.exe convert %img_path% -quality 75 %DEFAULT_IMAGE_FOLDER%\%name_no_ext%_compressed.jpg
)
; (
; $CONFIG_PATH = '%DEFAULT_LOG_FOLDER%'
; $TRUE_FALSE= Test-Path $CONFIG_PATH
; if($TRUE_FALSE -eq $False)
; {
; New-Item '%DEFAULT_LOG_FOLDER%' -type Directory
; }
; D:\ProgramFiles\mozjpeg-v4.0.3-win-x64\static\Release\cjpeg-static.exe %img_path% > %DEFAULT_IMAGE_FOLDER%\%name_no_ext%_compressed.jpg
; )
; 输出所有信息
; msgbox, MOZJPG 开始压缩 %script%
RunWait, PowerShell.exe -Command &{%script%} 2>&1 >> %DEFAULT_LOG_FOLDER%\ps.txt, , Hide, ; Run Powershell Script
; Run, D:\Data\Programs\UserData\AutoHotkey\ps.txt, , , ; Run Powershell Script
path = %DEFAULT_IMAGE_FOLDER%\%name_no_ext%_compressed.jpg
Return path ; expression method
}
; 从剪切板的内容创建png图片文件
; https://www.jianshu.com/p/ae048b5090f8
#Include Gdip_All.ahk
createPicFromClip(PicPath)
{
pToken := Gdip_Startup() ; Start gdi+
; pBitmapAlpha := Gdip_CreateBitmapFromFile(PicPath)
;pBitmapAlpha := Gdip_BitmapFromScreen(0, "")
;pBitmapAlpha := Gdip_BitmapFromScreen(x "|" y "|" width "|" height)
pBitmapAlpha := Gdip_CreateBitmapFromClipboard()
ImgWidth := Gdip_GetImageWidth(pBitmapAlpha) ; 获取宽度,高度,可省略
ImgHeight := Gdip_GetImageHeight(pBitmapAlpha)
Gdip_SaveBitmapToFile(pBitmapAlpha, PicPath,"255") ;第三个参数控制图片质量
Gdip_DisposeImage(pBitmapAlpha)
Gdip_Shutdown(pToken) ; close gdi+
}
本博文本意在于记录个人的思考与经验,部分博文采用英语写作,可能影响可读性,请见谅
本文来自博客园,作者:ZXYFrank,转载请注明原文链接:https://www.cnblogs.com/zxyfrank/p/15331464.html