Talk:Python Imaging Library
| This article is rated Stub-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||
Vote for Deletion
[edit]This article survived a Vote for Deletion. The discussion can be found here. -Splash 01:56, 12 July 2005 (UTC)
Scipy and Numpy Don't Use PIL
[edit]Please see:
before arguing. Maxerickson 00:21, 13 November 2006 (UTC)
Pillow
[edit]Hello all, from a quick Google search it turns out that Pillow is actually a fork of PIL that is very updated. Perhaps this could be reflected in the article, I'm not too sure about how i should do this Ajlee2006 (talk) 12:41, 8 June 2020 (UTC)
- @Ajlee2006: The page already mentions Pillow, what did you have in mind? The page should probably be move to Pillow (library) with Pillow as main focus and PIL mentioned in the history?. Jonpatterns (talk) 11:29, 25 August 2020 (UTC)
from PIL import Image, ImageDraw, ImageFont import os
- --- การตั้งค่าเบื้องต้น ---
output_dir = 'stickers_output' os.makedirs(output_dir, exist_ok=True)
WIDTH, HEIGHT = 512, 512
- รายการสติกเกอร์
stickers_data = [
{"text": "คุณ ok ไม่", "color": (255, 180, 180)},
{"text": "เซ็งว่ะ! ขอเมาดีกว่า", "color": (180, 180, 255)},
{"text": "ผมจะตัดเงินเดือนคุณ 25%", "color": (255, 255, 180)},
{"text": "ผมไม่แคร์", "color": (200, 200, 200)},
{"text": "ทุกคนไม่ทำงาน เล่นแต่ Facebook", "color": (255, 200, 150)},
{"text": "ขอปี๊บหม่าลุก 1 ชุด", "color": (150, 255, 150)},
{"text": "คอแห้ง ขอจัดชา 1 ป๋อง", "color": (150, 255, 255)}
]
- --- ฟังก์ชันโหลดฟอนต์ ---
def get_font(font_path, size):
try:
return ImageFont.truetype(font_path, size)
except IOError:
print(f"หาฟอนต์ {font_path} ไม่เจอ จะใช้ฟอนต์ Default (ภาษาไทยอาจไม่แสดงผล)")
return ImageFont.load_default()
- *** สิ่งที่ต้องทำ: ดาวน์โหลดฟอนต์ภาษาไทย (.ttf) มาวางไว้ที่เดียวกับไฟล์นี้ ***
- แนะนำ: 'Sarabun-Bold.ttf' หรือ 'Prompt-Regular.ttf' จาก Google Fonts
thai_font_path = "Sarabun-Bold.ttf" eng_font_path = "arial.ttf"
font_main = get_font(thai_font_path, 40) # ฟอนต์คำพูดหลัก font_chest = get_font(eng_font_path, 24) # ฟอนต์ชื่อที่หน้าอก
- --- ฟังก์ชันหลัก ---
print(f"กำลังสร้างสติกเกอร์ {len(stickers_data)} รูป...")
- โหลดรูปตัวละคร (ถ้าคุณไดคัทรูปแล้ว ให้แก้ชื่อไฟล์ตรงนี้)
character_image_path = "mr_blai_cutout.png" has_character_img = os.path.exists(character_image_path)
for i, data in enumerate(stickers_data, 1):
# 1. สร้าง Canvas ใส
img = Image.new('RGBA', (WIDTH, HEIGHT), (255, 255, 255, 0))
draw = ImageDraw.Draw(img)
# 2. วาดพื้นหลังวงกลมสีจาง
bg_color = data["color"] + (150,)
draw.ellipse([30, 30, WIDTH-30, HEIGHT-100], fill=bg_color)
# 3. วาดตัวละคร
if has_character_img:
# --- กรณีมีไฟล์รูปจริง (.png พื้นใส) ---
char_img = Image.open(character_image_path).convert("RGBA")
# ย่อรูปให้พอดี (รักษาอัตราส่วน)
char_img.thumbnail((400, 400))
# คำนวณตำแหน่งวางกึ่งกลาง
img_w, img_h = char_img.size
pos_x = (WIDTH - img_w) // 2
pos_y = (HEIGHT - img_h) // 2 - 20 # ขยับขึ้นนิดหน่อย
# แปะรูปลงไป
img.paste(char_img, (pos_x, pos_y), char_img)
else:
# --- กรณีไม่มีรูป (วาดวงกลมแทนไปก่อน แบบเดิม) ---
draw.ellipse([150, 50, 350, 280], fill=(245, 210, 180)) # หน้า
draw.rectangle([120, 280, 380, 450], fill=(160, 160, 170)) # เสื้อ
# ตา/ปาก จำลอง
draw.ellipse([180, 130, 210, 160], fill="black")
draw.ellipse([290, 130, 320, 160], fill="black")
draw.arc([200, 200, 300, 250], 0, 180, fill="black", width=3)
# 4. เขียนชื่อที่หน้าอก (Mr B'Lai) # ถ้ามีรูปจริง อาจต้องปรับตำแหน่ง (x,y) ให้ตรงกับเสื้อในรูป draw.text((250, 350), "Mr B'Lai", fill=(50, 50, 50), font=font_chest, anchor="mm")
# 5. วาดกล่องข้อความคำพูด
text = data["text"]
# วัดขนาดข้อความ
bbox = draw.textbbox((0, 0), text, font=font_main)
text_w = bbox[2] - bbox[0]
text_h = bbox[3] - bbox[1]
# วาดพื้นหลังข้อความ
pad_x, pad_y = 30, 15
box_x1 = (WIDTH - text_w) / 2 - pad_x
box_y1 = HEIGHT - text_h - 60 # ระยะจากขอบล่าง
box_x2 = (WIDTH + text_w) / 2 + pad_x
box_y2 = HEIGHT - 20
draw.rectangle(
[box_x1, box_y1, box_x2, box_y2],
fill=(255, 255, 255, 230),
outline=(100,100,100), width=2
)
# เขียนข้อความ
draw.text(
(WIDTH/2, (box_y1 + box_y2)/2), # กึ่งกลางกล่อง
text,
fill="black",
font=font_main,
anchor="mm"
)
# บันทึก
filename = f"sticker_{i:02d}.png"
img.save(os.path.join(output_dir, filename))
print(f"บันทึก: {filename}")
print("\nเรียบร้อย! อย่าลืมตรวจสอบไฟล์ในโฟลเดอร์ stickers_output") ~2026-28064-0 (talk) 03:42, 14 January 2026 (UTC)
- Stub-Class computer graphics articles
- Unknown-importance computer graphics articles
- WikiProject Computer graphics articles
- Stub-Class Computing articles
- Unknown-importance Computing articles
- Stub-Class software articles
- Unknown-importance software articles
- Stub-Class software articles of Unknown-importance
- All Software articles
- Automatically assessed Computing articles
- All Computing articles

