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

自從好久以前 [WIN10仿MAC] WinDynamicDesktop 自訂桌面教學  後

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

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

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


環境:

macOS 11.4

Anaconda3.8

Python3.8


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

安裝之前,先確認一下有無安裝 Homebrew:

或複製此代碼安裝

/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


附上程式碼:

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)
Subscribe to 0x9C27…9Df0
Receive the latest updates directly to your inbox.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from 0x9C27…9Df0

Skeleton

Skeleton

Skeleton