เช็กความยาวของ Meta Title ด้วย Pillow ใน Python

ใครที่ติดตามงานเขียนของผม จะสังเกตว่าบทความของผมมีชื่อ (meta title) ยาวหลาย 10 บรรทัด จนกระทั่งเมื่อไม่นานมานี้:

นั่นเป็นเพราะว่า ผมเข้าใจคำว่า long-tail keyword ผิดมาตลอด 😅

ผมเข้าใจว่า long-tail keyword คือ การเขียนชื่อบทความให้ยาวและมีความแตกต่าง ❌

แต่จริง ๆ long-tail keyword ไม่ต้องยาว แค่ต้องมีความเจาะจงเพื่อให้เข้าถึงผู้อ่านได้ง่าย ✅

เช่น:

❌ Python (กว้างเกินไป)

❌ วิธีเขียนโปรแกรม Python สำหรับวิเคราะห์ข้อมูลและพัฒนาเว็บไซต์แบบมืออาชีพ พร้อมตัวอย่างโค้ดที่ใช้งานได้จริงทุกระดับตั้งแต่เริ่มต้นจนถึงขั้นสูง (ยาวเกินไป)

✅ เช็กความยาวของ Meta Title ด้วย Pillow ใน Python (เจาะจง ตรงประเด็น และยาวพอดี)

ชื่อบทความควรมีความยาว 50–60 ตัวอักษร หรือ 580–600 pixels เพื่อให้แสดงผลบน Google ได้โดยไม่ถูกตัดคำเป็น “…” แบบนี้:

เราสามารถนับตัวอักษรได้ง่าย ๆ แต่ถ้าจะดี เราควรจะนับความยาว pixel ซึ่งไม่ตรงไปตรงมาเหมือนกับการนับตัวอักษร

หลังจาก Google ดู ผมก็เจอการคำนวณ pixel ใน Python ด้วย package ชื่อ Pillow ซึ่งใช้ง่ายใน 2 ขั้นตอนดังนี้



1️⃣ ขั้นที่ 1. สร้าง Font Object

ในขั้นแรก เราต้องสร้าง Font object ที่ Pillow ใช้คำนวณความยาว

ก่อนจะสร้าง font object เราจะต้องโหลด font ที่ต้องการ เช่น Arial Bold ซึ่งเป็น font เดียวกันกับหน้า Google Search:

Python
# Import the package
from pathlib import Path
# Set the font path
font_path = Path("/System/Library/Fonts/Supplemental/Arial Bold.ttf

จากนั้น สร้าง Font object โดยใส่ font และ font size (18) ลงไป:

Python
# Import the package
from PIL import ImageFont
# Set the default font
default_font = ImageFont.truetype(
str(font_path),
18
)

2️⃣ ขั้นที่ 2. คำนวณความยาว

ในขั้นที่ 2 เราจะคำนวณความยาว ด้วย .getlength() และชื่อบทความที่ต้องการ:

Python
# Set the title
my_title = "เช็กความยาวของ Meta Title ด้วย Pillow ใน Python"
# Estimate the title length in pixel
title_length = default_font.getlength(my_title)

จากนั้น แสดงผลการคำนวณ:

Python
# Set the recommended length
recommended_length = 580
# Print the title length
if title_length <= recommended_length:
print(f"🟢 Title is within the recommended length ({title_length} pixels).")
else:
print(f"🔴 Title is too long ({title_length} pixels).")

ผลลัพธ์:

🟢 Title is within the recommended length (501.0 pixels).

🍩 Bonus: เขียน Function เปรียบเทียบความยาว

ในกรณีที่เรามีหลายชื่อ และไม่รู้จะเลือกชื่อไหน เราสามารถเขียน function ไว้เปรียบเทียบความยาวได้แบบนี้:

Python
# Import the package
import polars as pl
# Convert to a function
def estimate_title_length(titles):
# Instantiate a collector
results = []
# Loop through the titles
for title in titles:
# Estimate the length
title_length = default_font.getlength(title)
# Append to the results
results.append({
"Title": title,
"Length (px)": title_length,
"Is Within Range": (
"🟢" if title_length <= recommended_length else "🔴"
)
})
# Return the results as a Polars DataFrame
return pl.DataFrame(results)

ทดสอบ function:

Python
# Test the function
titles = [
"เช็กความยาวของ Meta Title ด้วย Pillow ใน Python",
"วิธีวิเคราะห์ Meta Title Length ด้วย Pillow ใน Python",
"คู่มือฉบับสมบูรณ์สำหรับการคำนวณความยาวของ Meta Title ด้วย Pillow ใน Python พร้อมตัวอย่างการวิเคราะห์",
]
# Call the function
df = estimate_title_length(titles)
# Display the results
df

ผลลัพธ์:

จะเห็นได้ว่า การเช็กความยาวของ meta title ใน Python ง่ายนิดเดียว


🫵 Your Turn

หลังจบบทความนี้แล้ว ลองมาคำนวณความยาวชื่อบทความใน Python กันครับ


📄 อ้างอิง

Fediverse Reactions

Comments

Leave a comment