1. 程式人生 > >Hexo+NexT 部落格搭建相簿(一)

Hexo+NexT 部落格搭建相簿(一)

用Hexo + NexT 搭建了部落格之後,就想搭建一個相簿.ps:真是瞎折騰!.自己上網查了些資料,摸索摸索,終於搭建好了.這裡寫個教程,由於東西比較多,教程分為兩部分.

實現想法

在 github 上面建立一個相簿庫,當有更新時,提交到 github 上面,同時在部落格 resource 下面生成一個 data.json來生成所有相簿檔案的 json 檔案,部落格讀取 data.json 來展示相簿

建立相簿庫

在 github 上面建立一個倉庫,命名為 blog_back_up (倉庫名字隨便). 用 git clone 把倉庫 clone 到本地來.

cd blog_back_up

建立 photosmin_photos 兩個目錄,把要上傳的相簿圖片 放到 photos 資料夾下面.

相簿圖片命名方式 : yyyy-MM-dd_des.jpg/png/jpef/gif. eg: 2017-9-18_蝴蝶. jpg

處理圖片

圖片的處理 我用 python 指令碼來處理,這樣每次只要執行指令碼就可以了.
- 裁剪圖片

def cut_photo():
    """裁剪演算法

    ----------
    呼叫Graphics類中的裁剪演算法,將src_dir目錄下的檔案進行裁剪(裁剪成正方形)
    """
    src_dir = "photos/"
if directory_exists(src_dir): if not directory_exists(src_dir): make_directory(src_dir) # business logic file_list = list_img_file(src_dir) # print file_list if file_list: print_help() for infile in file_list: img = Image.open(src_dir+infile) Graphics(infile=src_dir+infile, outfile=src_dir + infile).cut_by_ratio() else
: pass else: print("source directory not exist!")
  • 壓縮圖片
def compress_photo():
    '''呼叫壓縮圖片的函式
    '''
    src_dir, des_dir = "photos/", "min_photos/"

    if directory_exists(src_dir):
        if not directory_exists(src_dir):
            make_directory(src_dir)
        # business logic
        file_list_src = list_img_file(src_dir)
    if directory_exists(des_dir):
        if not directory_exists(des_dir):
            make_directory(des_dir)
        file_list_des = list_img_file(des_dir)
        # print file_list
    '''如果已經壓縮了,就不再壓縮'''
    for i in range(len(file_list_des)):
        if file_list_des[i] in file_list_src:
            file_list_src.remove(file_list_des[i])
    compress('4', des_dir, src_dir, file_list_src)
  • 根據圖片資訊生成 json
def handle_photo():
    '''根據圖片的檔名處理成需要的json格式的資料

    -----------
    最後將data.json檔案存到部落格的source/photos資料夾下
    '''
    src_dir, des_dir = "photos/", "min_photos/"
    file_list = list_img_file(src_dir)
    list_info = []
    for i in range(len(file_list)):
        filename = file_list[i]
        date_str, info = filename.split("_")
        info, _ = info.split(".")
        date = datetime.strptime(date_str, "%Y-%m-%d")
        year_month = date_str[0:7]            
        if i == 0:  # 處理第一個檔案
            new_dict = {"date": year_month, "arr":{'year': date.year,
                                                                   'month': date.month,
                                                                   'link': [filename],
                                                                   'text': [info],
                                                                   'type': ['image']
                                                                   }
                                        } 
            list_info.append(new_dict)
        elif year_month != list_info[-1]['date']:  # 不是最後的一個日期,就新建一個dict
            new_dict = {"date": year_month, "arr":{'year': date.year,
                                                   'month': date.month,
                                                   'link': [filename],
                                                   'text': [info],
                                                   'type': ['image']
                                                   }
                        }
            list_info.append(new_dict)
        else:  # 同一個日期
            list_info[-1]['arr']['link'].append(filename)
            list_info[-1]['arr']['text'].append(info)
            list_info[-1]['arr']['type'].append('image')
    list_info.reverse()  # 翻轉
    final_dict = {"list": list_info}
    with open("../../blog/blog_src/source/photos/data.json","w") as fp:
        json.dump(final_dict, fp)

其中 ../../blog/blog_src/source/photos/data.json 是我部落格地址,這裡換成你的部落格地址.
完成的 python 下載地址 tool.py

使用

python3 tool.py
因為我用的是 python3 這裡可以根據你的 python 版本來使用

QA:

如果出現 from PIL import Image 這裡報錯.說明沒有 PIL 這個庫.
執行 python3 -m pip install Pillow

結束語

目前為止,相簿庫已經處理完畢,接下來會更新 hexo 怎麼使用相簿庫.
原博地址 TIM’S BLOG

很多人轉載我原博地址,也不註明來源,真的讓我很氣!!所以我在簡書中更博.效果可以去我的部落格中檢視