# 【Python超實戰】讓你的MAC動態桌布(heif格式)批量匯出成png無損格式 @ 小雨の部 :: 痞客邦 ::

By [Untitled](https://paragraph.com/@0x9c27e247b7de3d29c76b2f5d1ec500285cd89df0) · 2022-03-01

---

自從好久以前 [\[WIN10仿MAC\] WinDynamicDesktop 自訂桌面教學](https://walleyu9966.pixnet.net/blog/post/19318856-%5Bwin10%E4%BB%BFmac%5D-windynamicdesktop-%E8%87%AA%E8%A8%82%E6%A1%8C%E9%9D%A2%E6%95%99%E5%AD%B8)  後

感覺桌布非常無聊，還是mac原生動態桌布資源最多

但mac的格式都是heif，根本不支援怎辦?

這時就可以用python套件來匯出成png格式，在使用json建立個別的時間變化參數

* * *

環境：

_macOS 11.4_

_Anaconda3.8_

_Python3.8_

* * *

**特別提醒：Windows目前不支援此套件的使用喔**

**安裝之前，先確認一下有無安裝 Homebrew：**

[**https://brew.sh/index\_zh-tw**](https://brew.sh/index_zh-tw)

或複製此代碼安裝

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    

接著在macOS的終端機打入以下安裝

    conda
    brew install libffi libheif
    pip install git+https://github.com/david-poirier-csn/pyheif.git
    pip install whatimage
    

如無法安裝就代表git網址變動，請參考pypl

[https://pypi.org/project/pyheif/](https://pypi.org/project/pyheif/)

* * *

附上程式碼：

    import whatimage
    import pyheif
    import traceback
    from PIL import Image
    
    def decodeImage(bytesIo):
        try:
            fmt = whatimage.identify_image(bytesIo)
            
            if fmt in ['heic']:
                i = pyheif.read_heif(bytesIo)
                
                pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
                
                pi.save('heeh.png', format="png")
        except:
            traceback.print_exc()
    def read_image_file_rb(file_path):
        with open(file_path, 'rb') as f:
            file_data = f.read()
        return file_data
    
    if __name__ == "__main__":
        file_path = input("輸入圖片名稱")
        print('file_path = ./{}'.format(file_path))
        data = read_image_file_rb(file_path)
        
        decodeImage(data)

---

*Originally published on [Untitled](https://paragraph.com/@0x9c27e247b7de3d29c76b2f5d1ec500285cd89df0/python-mac-heif-png)*
