โดยครั้งนี้เราจะมาลองการใช้
module Pillow ในการเขียน เรียกไฟล์ img เฉพาะนามสกุล
.jpg/jpeg หรือนามสกุลอื่นๆ ขึ้นมาเพื่อที่เราจะทำการปรับเปลี่ยนที่อยู่ในการบันทึกและ เปลี่ยนนามสกุล file img ทีละหลายๆ file โดยใช้ภาษา
python
ตำแหน่งของ ไฟล์ต่างๆ
- Python Knowledge-1.png (28.74 KiB) Viewed 769 times
1. ขั้นแรกเราจะเริ่มจากการ import module os กับ pillow
2. กำหนดตัวแปร เพื่อที่จะเก็บ Folder ที่อยู่ของ file ภาพ
3. ทำการ loop เพื่อเก็บชื่อของไฟล์ img โดยกำหนดให้ว่าจะเก็บเฉพาะ นามสกุล .jpeg เท่านั้น
โค้ด: เลือกทั้งหมด
for i in os.listdir ( parth ):
if i.endswith ( '.jpeg' ):
ผลลัพธ์
- Python Knowledge-1.png (9.01 KiB) Viewed 769 times
4. จากนั้นทำการเรียก รูปภาพ ขึ้นมาเพื่อที่เราจะทำการจัดการกับรูปภาพ โดยเราจะทำการ split ชื่อ รูปภาพกับนามสกุลออกจากกัน
โค้ด: เลือกทั้งหมด
f = Image.open(parth + i)
p, p1 = os.path.splitext ( i )
ผลลัพธ์
- Python Knowledge-1.png (12.36 KiB) Viewed 769 times
5. ทำการ save โดยกำหนดตำแหน่ง และ นามสกุลโดยใช้ .format
ผลลัพธ์
- Python Knowledge-1.png (7.51 KiB) Viewed 769 times
full code
โค้ด: เลือกทั้งหมด
from PIL import Image
import os
parth = "images/"
for i in os.listdir ( parth ):
if i.endswith ( '.jpeg' ):
f = Image.open(parth + i)
p, p1 = os.path.splitext ( i )
f.save("Testimg/{}.png".format(p))
อ้างอิง
https://pillow.readthedocs.io/en/stable/reference/index.html
https://note.nkmk.me/en/python-pillow-basic/
https://www.pythonforbeginners.com/gui/how-to-use-pillow
โดยครั้งนี้เราจะมาลองการใช้ [url=https://www.mindphp.com/forums/viewtopic.php?f=79&t=37198]module[/url] [url=https://www.mindphp.com/forums/viewtopic.php?f=144&t=64276]Pillow[/url] ในการเขียน เรียกไฟล์ img เฉพาะนามสกุล [url=https://mindphp.com/developer/67-%E0%B8%A3%E0%B8%B9%E0%B8%9B%E0%B8%A0%E0%B8%B2%E0%B8%9E/1954-jpg-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html].jpg/jpeg[/url] หรือนามสกุลอื่นๆ ขึ้นมาเพื่อที่เราจะทำการปรับเปลี่ยนที่อยู่ในการบันทึกและ เปลี่ยนนามสกุล file img ทีละหลายๆ file โดยใช้ภาษา [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/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]python[/url]
ตำแหน่งของ ไฟล์ต่างๆ
[attachment=3]Python Knowledge-1.png[/attachment]
1. ขั้นแรกเราจะเริ่มจากการ import module os กับ pillow
[code]from PIL import Image
import os
[/code]
2. กำหนดตัวแปร เพื่อที่จะเก็บ Folder ที่อยู่ของ file ภาพ
[code]parth = "images/"[/code]
3. ทำการ loop เพื่อเก็บชื่อของไฟล์ img โดยกำหนดให้ว่าจะเก็บเฉพาะ นามสกุล .jpeg เท่านั้น
[code]for i in os.listdir ( parth ):
if i.endswith ( '.jpeg' ):[/code]
ผลลัพธ์
[attachment=2]Python Knowledge-1.png[/attachment]
4. จากนั้นทำการเรียก รูปภาพ ขึ้นมาเพื่อที่เราจะทำการจัดการกับรูปภาพ โดยเราจะทำการ split ชื่อ รูปภาพกับนามสกุลออกจากกัน
[code] f = Image.open(parth + i)
p, p1 = os.path.splitext ( i )[/code]
ผลลัพธ์
[attachment=1]Python Knowledge-1.png[/attachment]
5. ทำการ save โดยกำหนดตำแหน่ง และ นามสกุลโดยใช้ .format
[code] f.save("Testimg/{}.png".format(p))[/code]
ผลลัพธ์
[attachment=0]Python Knowledge-1.png[/attachment]
full code
[code]from PIL import Image
import os
parth = "images/"
for i in os.listdir ( parth ):
if i.endswith ( '.jpeg' ):
f = Image.open(parth + i)
p, p1 = os.path.splitext ( i )
f.save("Testimg/{}.png".format(p))[/code]
อ้างอิง
https://pillow.readthedocs.io/en/stable/reference/index.html
https://note.nkmk.me/en/python-pillow-basic/
https://www.pythonforbeginners.com/gui/how-to-use-pillow