วันนี้เราจะมาสอนวิธีการเพิ่มตัวอักษรเข้าไปในภาพ โดยหลักๆ เลยเราจำเป็นจะต้องเตรียมไฟล์ รูปแบบตัวอักษรเข้าไปไว้ใน folder ของ
project ก่อน ส่วนวิธีเขียนจะมีขั้นตอนดังนี้
1.import module
Code: Select all
from PIL import Image , ImageDraw , ImageFont
2.เปิดรูปที่เราต้องการจะเพิ่มตัวอักษร
Code: Select all
image = Image.open("static/images/maxresdefault.jpg")
3. สร้างตัวแปรเพื่อเก็บรูปแบบและขนาดของตัวอักษรโดยเราจะทำการเลือกจากไฟล์ font ที่เรานำมาเตรียมไว้
Code: Select all
font_type = ImageFont.truetype('Salmon White - Personal Use.otf' , 50)
4. สร้างตัวแปรเพื่อเก็บฟังก์ชั่น
draw
5.กำหนด ตำแหน่ง ,
ข้อความ , สี แล้วก็ใส่ตัวแปร font ที่เราทำการ สร้างเอาไว้ ตามลำดับ
Code: Select all
draw.text(xy = (200,200),text="Test text" , fill=(255,60,0),font=font_type)
ผลลัพธ์
ภาพต้นแบบ

- Python Knowledge-1.png (452.54 KiB) Viewed 374 times
ภาพที่ได้ทำการเพิ่มตัวอักษรแล้ว

- Python Knowledge-2.png (305.43 KiB) Viewed 374 times
เป็นยังไงบ้างครับ หวังว่าคงจะพอมีประโยชน์กับเพื่อนๆที่กำลังศึกษาเจ้าตัว module pillow อยู่เพื่อนำไปใช้ในการทำโปรเจคนะครับ
ศึกษาเพิ่มเติม
แชร์ความรู้เกี่ยวกับ python
Pillow library ฟังก์ชั่น Filters สำหรับการปรับ Filters ของภาพให้มีลูกเล่นมากขึ้น
วีดีโอสอนเขียน python
บทความเทคนิคเกี่ยวกับ python
อ้างอิง
https://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html
https://stackoverflow.com/questions/41405632/draw-a-rectangle-and-a-text-in-it-using-pil/41406147
https://www.geeksforgeeks.org/python-pil-imagedraw-draw-text/
วันนี้เราจะมาสอนวิธีการเพิ่มตัวอักษรเข้าไปในภาพ โดยหลักๆ เลยเราจำเป็นจะต้องเตรียมไฟล์ รูปแบบตัวอักษรเข้าไปไว้ใน folder ของ [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/159-crm-software/6113-project-management-software.html]project[/url] ก่อน ส่วนวิธีเขียนจะมีขั้นตอนดังนี้
1.import module
[code]from PIL import Image , ImageDraw , ImageFont[/code]
2.เปิดรูปที่เราต้องการจะเพิ่มตัวอักษร
[code]image = Image.open("static/images/maxresdefault.jpg")[/code]
3. สร้างตัวแปรเพื่อเก็บรูปแบบและขนาดของตัวอักษรโดยเราจะทำการเลือกจากไฟล์ font ที่เรานำมาเตรียมไว้
[code]font_type = ImageFont.truetype('Salmon White - Personal Use.otf' , 50)[/code]
4. สร้างตัวแปรเพื่อเก็บฟังก์ชั่น [url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/google-for-work/225-google-drawings/3843-google-drawings-1-creating-banner.html]draw[/url]
[code]draw = ImageDraw.Draw(image)[/code]
5.กำหนด ตำแหน่ง , [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2206-pm-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]ข้อความ[/url] , สี แล้วก็ใส่ตัวแปร font ที่เราทำการ สร้างเอาไว้ ตามลำดับ
[code]draw.text(xy = (200,200),text="Test text" , fill=(255,60,0),font=font_type)[/code]
ผลลัพธ์
ภาพต้นแบบ
[attachment=1]Python Knowledge-1.png[/attachment]
ภาพที่ได้ทำการเพิ่มตัวอักษรแล้ว
[attachment=0]Python Knowledge-2.png[/attachment]
เป็นยังไงบ้างครับ หวังว่าคงจะพอมีประโยชน์กับเพื่อนๆที่กำลังศึกษาเจ้าตัว module pillow อยู่เพื่อนำไปใช้ในการทำโปรเจคนะครับ
ศึกษาเพิ่มเติม
[url=https://www.mindphp.com/forums/viewforum.php?f=144]แชร์ความรู้เกี่ยวกับ python[/url]
[url=https://www.mindphp.com/forums/viewtopic.php?f=144&t=64675&p=179378#p179378]Pillow library ฟังก์ชั่น Filters สำหรับการปรับ Filters ของภาพให้มีลูกเล่นมากขึ้น[/url]
[url=https://www.mindphp.com/vdo-tutorial-python.html]วีดีโอสอนเขียน python[/url]
[url=https://www.mindphp.com/developer/tips-python.html]บทความเทคนิคเกี่ยวกับ python[/url]
อ้างอิง
https://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html
https://stackoverflow.com/questions/41405632/draw-a-rectangle-and-a-text-in-it-using-pil/41406147
https://www.geeksforgeeks.org/python-pil-imagedraw-draw-text/