Notion Hexo 利用Python進行圖片轉檔,增進SEO

圖片在網頁中大小影響大!使用Python將圖檔轉為WebP格式,保留品質,減少檔案大小。WebP格式壓縮效率高,支援透明和動畫,大多數瀏覽器都可支援。適合用於網站、社群媒體圖片等。想了解更多?可以參考[這篇文章](https://codinglu.tw/2024/07/seo-friendly-url-name-for-blog-post-title-jilu-xuexi-shiyong-hexo-jiashe-buluoge-tags-hexo-python-zidonghua/)!

圖檔在網頁中檔案大小的差別

在網頁中,圖片檔案太大會帶來多種壞處。

  • 首先,它會顯著增加網頁的加載時間,影響使用者體驗。

  • 其次,過大的圖片檔案會佔用更多的伺服器資源,增加網站的運營成本。

  • 此外,圖片過大也會影響網站的SEO排名,因為搜索引擎更青睞加載速度快的網站。

以 Python 轉換圖片類型

那麼,如何在保留圖片質量的同時減小它們的檔案大小呢?使用webp格式

WebP 是由 Google 開發的一種現代圖片格式,旨在提供優異的圖片壓縮性能,同時保持較高的圖片質量。以下是 WebP 的一些關鍵特點:

  1. 高壓縮效率:WebP 支持有損和無損壓縮,相比傳統的 JPEG 和 PNG 格式,能在相似的圖片質量下提供更小的文件大小。這有助於加快網頁加載速度,節省帶寬,特別是在移動設備上效果更為顯著。

  2. 透明和動畫支持:類似於 PNG,WebP 支持透明通道(alpha channel),這使得它適合用於需要透明背景的圖片。此外,WebP 還支持動畫功能,類似於 GIF 格式,但通常文件更小且質量更好。

  3. 瀏覽器支持:目前,大多數主流瀏覽器如 Google Chrome、Firefox 和 Microsoft Edge 都支持 WebP 格式,這使得它成為一個可靠的選擇來替代傳統的圖片格式。然而,一些較舊或非主流的瀏覽器可能不支持 WebP,因此在使用時需要考慮兼容性問題。

  4. 應用場景:WebP 特別適合用於需要高效圖片傳輸的場景,如網站圖片、網店產品圖、社交媒體圖像等。在這些應用中,使用 WebP 格式可以減少網頁的加載時間,提升用戶體驗,並且節省服務器資源。

簡單範例

1
2
3
4
5
6
7
8

from PIL import Image

# 打開一張圖片
image = Image.open("your_image.jpg")

# 優化圖片大小,設置品質為85
image.save("your_image.webp", "WEBP", quality=80)

Notion下載時以webp儲存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def download_image_as_webp(url, image_name, image_dir):
try:
print(f"Downloading image from URL: {url}")
response = requests.get(url)

if response.status_code != 200:
print(f"Failed to download image, status code: {response.status_code}")
return None

# 确保目标目录存在
os.makedirs(image_dir, exist_ok=True)
image_path = os.path.join(image_dir, image_name)

# 保存下载的图像
with open(image_path, 'wb') as file:
file.write(response.content)

print(f"Image saved to {image_path}, converting to WebP format.")

try:
# 转换为WebP格式
with Image.open(image_path) as img:
webp_image_path = os.path.splitext(image_path)[0] + '.webp'
img.save(webp_image_path, 'WEBP')

print(f"Image converted to WebP format at {webp_image_path}")
return webp_image_path

except Exception as e:
print(f"An error occurred during image conversion: {e}")
return None

except Exception as e:
print(f"An error occurred while downloading or converting the image: {e}")
return None



# Download index image
def main():
database_id = "database-id"
items = fetch_database_items(database_id, filter_status)

for item in items:
index_img_url = item["properties"]["index_img"]["files"][0]["file"]["url"] if item["properties"]["index_img"]["files"] else None

index_img_path = None
if index_img_url:
index_img_name = f"{title.replace(' ', '_')}_index.webp"
index_img_path = download_image_as_webp(index_img_url, index_img_name, index_img_dir)
index_img_path = f"/img/{os.path.basename(index_img_path)}" if index_img_path else default_image_path
else:
index_img_path = default_image_path

完整程式碼可參考:如何利用Notion與Hexo連動


Notion Hexo 利用Python進行圖片轉檔,增進SEO
https://codinglu.tw/2024/07/seo-friendly-url-name-for-the-blog-post-notion-hexo-using-python-for-image-conversion-improving-seo-hexo-python-seo/
作者
阿盧
發布於
2024年7月25日
許可協議
📌 本文瀏覽量: 0