原文鏈接:https://www.hexianwei.com/2019/01/11/hexoNextPhoto/
因為不會寫前端呀,反正也沒興趣,反正我是直接參考的,實現(xiàn)了。實現(xiàn)效果:beer photos
效果圖:

image
python 腳本
基本上都是直接參考的,我一直在用騰訊的cos,所以后面的 python 代碼改了點東西?;玖鞒?/p>
- 1、剪裁圖片(剪裁為正方形)
def cut_photo(path):
"""
剪裁圖片:取長和寬最短值,剪裁為正方形
"""
img = Image.open(path)
w, h = img.size
length = h if (w > h) else w
box = (0, 0, length, length)
imi_photo = img.crop(box)
im_path = "im_" + path
imi_photo.save(im_path)
return im_path
- 2、生成 json 文件
這邊和原作者不一樣的地方是:我每上傳一張圖片,追加到 data.json 里。原作者好像是文件夾操作吧。
這邊追加就有個邏輯是,追到到對應的日期下,而且需要倒序排序。
data.json 文件
{
"list": [
{
"date": "2019-01",
"arr": {
"year": 2019,
"month": 1,
"link": [
"2019-01-02_第二個十斤.jpg",
"2019-01-10_剁手買個macPro加油.jpg"
],
"text": [
"第二個十斤",
"剁手買個macPro加油"
],
"type": [
"image",
"image"
]
}
},
{
"date": "2018-07",
"arr": {
"year": 2018,
"month": 7,
"link": [
"2018-07-05_第一個十斤.jpg"
],
"text": [
"第一個十斤"
],
"type": [
"image"
]
}
}
]
}
- 3、提交github
簡單的 git 操作,需要注意的是 os.system 是子線程操作,所以上個 cd 命令之后,下個git 命令其實還是在當前目錄的,cd 相當于沒起作用,所以需要 && 連接,一起執(zhí)行
def git_operation():
"""
提交github
"""
shell = 'cd ' + blog_path + ' && ' + 'git add --all' + ' && ' + 'git commit -m "add photos"' + ' && ' + 'git push'
os.system(shell)
- 4、上傳圖片(騰訊cos)
cos有shell 命令,自己結(jié)合改一下(我執(zhí)行的是 cosupload 自定義了腳本,可以返回圖片上傳之后的url)
傳送:https://www.hexianwei.com/2018/12/30/coscmd/
- 5、部署
我這邊是執(zhí)行了一個腳本(blog shell)。其實就是登陸到服務器,然后 git pull && hexo g
# coding: utf-8
from PIL import Image
import os
import sys
import json
import ast
from datetime import datetime
blog_path = '/home/beer/beer_blog'
data_path = '/home/beer/beer_blog/themes/next/source/lib/album/data.json'
cos_img = '/photos/'
cos_im_img = '/min_photos/'
def handle_photo(file_name):
"""
根據(jù)圖片的文件名處理成需要的json格式的數(shù)據(jù)
json 按照date 倒序
"""
# 讀取json 文件
with open(data_path, "r", encoding='utf-8') as f:
# 第一次文件為空時候處理
try:
origin_data = json.load(f)
except:
origin_data = '''{'list':[]}'''
# 轉(zhuǎn)化為 dict
date_dict = ast.literal_eval(str(origin_data))
# 獲取 data_list
data_list = date_dict.get('list')
date_list = []
for i in range(len(data_list)):
date_list.append(data_list[i].get('date'))
new_dict, date, info = build_dict(file_name)
# date 存在 and 不存在兩種情況
if date in date_list:
index = date_list.index(date)
data_list[index]['arr']['link'].append(file_name)
data_list[index]['arr']['text'].append(info)
data_list[index]['arr']['type'].append('image')
else:
data_list.append(new_dict)
date_list.append(date)
# 按照 date 排序
date_list.sort(key=sort_date, reverse=True)
# 按照 date 重構(gòu) dict
final_data_list = []
for date in date_list:
for data in data_list:
if data.get('date') == date:
final_data_list.append(data)
# 保存
final_dict = {"list": final_data_list}
with open(data_path, "w") as f:
json.dump(final_dict, f)
def sort_date(elem):
return int(''.join(elem.split('-')))
# 根據(jù)圖片名字生成 dict
def build_dict(file_name):
date_str, info = file_name.split("_")
info, type = info.split(".")
date = datetime.strptime(date_str, "%Y-%m-%d")
year_month = '-'.join(date_str.split('-')[0:2])
new_dict = {"date": year_month,
"arr": {"year": date.year,
"month": date.month,
"link": [file_name],
"text": [info],
"type": ['image']
}
}
return new_dict, year_month, info
def cut_photo(path):
"""
剪裁圖片:取長和寬最短值,剪裁為正方形
"""
img = Image.open(path)
w, h = img.size
length = h if (w > h) else w
box = (0, 0, length, length)
imi_photo = img.crop(box)
im_path = "im_" + path
imi_photo.save(im_path)
return im_path
def git_operation():
"""
提交github
"""
shell = 'cd ' + blog_path + ' && ' + 'git add --all' + ' && ' + 'git commit -m "add photos"' + ' && ' + 'git push'
os.system(shell)
def cosupload_operation(file_name, im_file_name):
"""
cosupload 上傳到 騰訊cos
"""
print("cosupload" + " " + file_name + " " + cos_img + file_name)
os.system("cosupload" + " " + file_name + " " + cos_img + file_name)
os.system("cosupload" + " " + im_file_name + " " + cos_im_img + file_name)
def blog_operation():
"""
部署
blog shell 腳本:登錄到服務器,打包
"""
os.system("blog")
if __name__ == "__main__":
'''
1、剪裁圖片(剪裁為正方形)
2、上傳圖片(騰訊cos)
3、生成 json 文件
4、提交github
5、部署
'''
# 獲取參數(shù)
file_name = sys.argv[1]
im_file_name = cut_photo(file_name)
handle_photo(file_name)
git_operation()
cosupload_operation(file_name, im_file_name)
blog_operation()