在编写 markdown 时,经常出现很小的图片被放得很大,为了解决这个问题,让视觉上变得好看点,决定编写一个压缩图片的 Alfred Workflow。
文章目录
- 一、Python 程序的编写
- 二、新建 Alfred Workflow
- 三、使用
一、Python 程序的编写
在 Mac 上获取剪贴板的图片可查看这里:mac 上 Python 读写剪贴板图片、文字。
编写好的代码如下:
            
              
                # coding=utf-8
              
              
                import
              
               os
              
                import
              
               io
              
                from
              
               PIL 
              
                import
              
               Image
              
                ,
              
               ImageGrab
              
                from
              
               AppKit 
              
                import
              
               NSPasteboard
              
                ,
              
               NSArray
              
                ,
              
               NSData
              
                ,
              
               NSImage
              
                # 压缩图片
              
              
                def
              
              
                resize_image
              
              
                (
              
              img
              
                ,
              
               target_width
              
                =
              
              
                720
              
              
                )
              
              
                :
              
              
    origin_width
              
                ,
              
               origin_height 
              
                =
              
               img
              
                .
              
              size
    target_height 
              
                =
              
              
                int
              
              
                (
              
              origin_height 
              
                *
              
               target_width 
              
                /
              
               origin_width
              
                )
              
              
                return
              
               img
              
                .
              
              resize
              
                (
              
              
                (
              
              target_width
              
                ,
              
               target_height
              
                )
              
              
                ,
              
               Image
              
                .
              
              ANTIALIAS
              
                )
              
              
                # 获取图片 bytes
              
              
                def
              
              
                get_img_bytes
              
              
                (
              
              img
              
                )
              
              
                :
              
              
    img_bytes 
              
                =
              
               io
              
                .
              
              BytesIO
              
                (
              
              
                )
              
              
    img
              
                .
              
              save
              
                (
              
              img_bytes
              
                ,
              
              
                format
              
              
                =
              
              
                'PNG'
              
              
                )
              
              
                return
              
               img_bytes
              
                # 保存图片至剪贴板
              
              
                def
              
              
                save_img_bytes_to_pasteboard
              
              
                (
              
              img_bytes
              
                )
              
              
                :
              
              
    pb 
              
                =
              
               NSPasteboard
              
                .
              
              generalPasteboard
              
                (
              
              
                )
              
              
                # 必须先清除
              
              
    pb
              
                .
              
              clearContents
              
                (
              
              
                )
              
              
    imgNsData 
              
                =
              
               NSData
              
                .
              
              alloc
              
                (
              
              
                )
              
              
                .
              
              initWithBytes_length_
              
                (
              
              img_bytes
              
                .
              
              getvalue
              
                (
              
              
                )
              
              
                ,
              
               img_bytes
              
                .
              
              tell
              
                (
              
              
                )
              
              
                )
              
              
    imgNsImage 
              
                =
              
               NSImage
              
                .
              
              alloc
              
                (
              
              
                )
              
              
                .
              
              initWithData_
              
                (
              
              imgNsData
              
                )
              
              
    array 
              
                =
              
               NSArray
              
                .
              
              arrayWithObject_
              
                (
              
              imgNsImage
              
                )
              
              
    pb
              
                .
              
              writeObjects_
              
                (
              
              array
              
                )
              
              
query 
              
                =
              
              
                "720"
              
              
                # 读剪贴板的图片
              
              
img 
              
                =
              
               ImageGrab
              
                .
              
              grabclipboard
              
                (
              
              
                )
              
              
                if
              
               img 
              
                is
              
              
                not
              
              
                None
              
              
                :
              
              
                print
              
              
                "orginWH "
              
              
                ,
              
               img
              
                .
              
              size
    resizedImg 
              
                =
              
               resize_image
              
                (
              
              img
              
                ,
              
              
                int
              
              
                (
              
              query
              
                )
              
              
                )
              
              
                print
              
              
                "resizedWH"
              
              
                ,
              
               resizedImg
              
                .
              
              size
    resizedImgBytes 
              
                =
              
               get_img_bytes
              
                (
              
              resizedImg
              
                )
              
              
    save_img_bytes_to_pasteboard
              
                (
              
              resizedImgBytes
              
                )
              
              
                else
              
              
                :
              
              
                print
              
              
                "pasteboard no img"
              
            
          
          二、新建 Alfred Workflow
            打开 Alfred,
            
              cmd + ;
            
             进入设置,选择 Workflows。
          
            在左侧面板的下方点击 
            
              +
            
            ,弹出 Workflow 的创建面板,点击 
            
              Blank Workflow
            
            ,新建一个空白的 Workflow。
            
            
               
            
            
             在弹出的配置面板填写 Workflow 的名称、描述、分类、id、创建者、网站,保存。
            
            
               
            
            
             在 Workflow 的图形界面上通过
            
              右键-Inputs-Keyword
            
            ,新建一个输入动作,设置关键字,这里我设置关键字为 cp。
            
            
               
            
            
            
               
            
            
             生成输入动作后,点击输入图块左侧的凸起,创建一个后续动作。选择 
            
              Run Script
            
            ,运行一个脚本。
            
            
               
            
            
             在脚本的配置界面,选择语言为 python,下方的输入框中填入准备好的 Python 脚本,保存。
            
            
               
            
            
             点击脚本图块右侧的凸起,添加一个结束动作,选择 
            
              Post Notification
            
            ,弹一个通知。
            
            
               
            
          
三、使用
            
              ctrl + cmd + shift + 4
            
             截图,或者复制一张网页的图片。
          
唤起 Alfred,输入 cp,即可完成压缩,并重新写入剪贴板。


 
             
					 
					