By pittaya Writer on Tag: Special Report, Python, In-Depth
Special Report

พอดีมีเรื่องจำเป็นต้องจัดการกับรูปภาพนิดหน่อย ดูเหมือนงานจะง่ายๆ แต่ออกจะกินแรงอยู่ไม่น้อย ปัญหาคือว่า มีรูปขนาด 13500 x 13500 pixel อยู่รูปหนึ่ง ต้องการหั่นเป็นรูปย่อยๆ รูปละ 1350 x 900 ดังนั้นก็จะได้รูปออกมา 10 x 15 = 150 รูป นอกจากนี้แต่ละรูป จะต้องใส่ขอบขาวเข้าไปด้านขวา และใส่ตัวเลขเข้าไปเพื่อบอกว่าเป็นรูปหมายเลขที่เท่าไหร่ โดยเป็นตัวเลขสองตัว ตั้งแต่รูปซ้ายบนสุด (0,0) จนถึงขวาล่างสุด (9, 14) เพื่อที่จะเอารูปไปอัดจากร้าน แล้วเอามาเรียงกลับเป็นอย่างเดิมทีหลังได้โดยไม่งง

ของแบบนี้จะให้ใช้ The GIMP หรือ Photoshop ทำ อาจจะเปลืองแรงอยู่ ก็เลยลองเอา Python Image Library (PIL) ที่เคยเล่นอยู่นิดๆ หน่อยๆ มาลองเขียนดู
ทีแรกก็โหลด module แล้วก็เปิดไฟล์ภาพขึ้นมาก่อน เขียนง่ายๆ คือ
import Image
src_img = Image.open("/tmp/hires.jpg")

วนลูปก็ทำได้ไม่ยาก
for x in xrange(0, 13500, 1350):
for y in xrange(0, 13500, 900):
# do something

วิธีตัดรูปออกมา ใช้ method crop() ซึ่งจะ return Image ออกมาให้ พารามิเตอร์ใส่เป็น tuple บอกตำแหน่ง 4 มุม
piece = src_img.crop((x, y, x+1350, y+900))
ผมต้องการเพิ่มขอบด้านขวาเข้าไป เพื่อเอาไว้แสดงตัวเลขลำดับรูป แต่คิดวิธีอื่นไม่ออก เลยใช้วิธีสร้าง Image ขึ้นมาใหม่ให้มีขนาด 1400 x 900 แล้วเอารูปที่ตัดได้มาแปะชิดซ้าย ก็จะได้ขอบด้านขวาตามต้องการ
newpiece = Image.new("RGB", (1400, 900), (255, 255, 255)) # mode, size, color
newpiece.paste(piece, (0, 0)) # paste at (0, 0)

มาถึงการใส่ตัวหนังสือลงไป อันนี้ต้องใช้อีก module คือ ImageDraw จะทำให้เราสามารถใส่ตัวหนังสือ หรือลากเส้นบนรูปได้
ที่จริงตรงนี้เขียนเป็น text อันเดียวก็ได้ แต่มันจะเลยขอบ ต้องเขียนตะแคง แต่ขี้เกียจอ่าน document เรื่อง rotation ก็เลยมักง่ายเขียนเอาแบบนี้
import ImageDraw
draw = ImageDraw.Draw(newpiece)

draw.text((1355,10), "%d" % x/1350, (0, 0, 0)) # put text at (1355, 10), color = black
draw.text((1355,50), "%d" % y/900, (0, 0, 0))

ปรากฏว่าตัวหนังสือที่ได้มันเล็กเกินไปอีก ถ้าจะปรับแต่งฟอนต์ต้องให้ module ImageFont
import ImageFont
ft = ImageFont.truetype("tahoma.ttf", 30) # Tahoma, 30 point

โค้ดส่วนเขียนตัวหนังสือต้องเพิ่มพารามิเตอร์ไปอีกตัว
draw.text((1355,50), "%d" % y/900, (0, 0, 0), font=ft)
สุดท้ายก็สั่งให้เซฟ ปล่อยให้ทำงานวนลูปจนเสร็จ
newpiece.save("img_%d_%d.jpg" % (x/1350, y/900), quality=85)

สุดท้ายก็เอารูปที่ได้ทั้งหมดไปอัดที่ร้าน ตัดขอบด้านข้างทิ้ง เอามาแปะรวมกัน ก็จะเรียบร้อย

ที่มา - Don't Think, just read.

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

loptar Mon, 14/08/2006 - 22:20

กระบวนการทั้งหมด ใช้เวลาเท่าไหร่ครับ คุ้มกว่าการใช้ image editor โดยตรงมั้ย :-)

pittaya Tue, 15/08/2006 - 01:01

loptar - ใช้เวลาไม่นานนะครับ ไม่เกินชั่วโมงนึง เทียบดูกับการใช้ The Gimp (image editor ตัวเดียวที่ผมมีและใช้งานได้ไม่ค่อยคล่อง) ก็ประหยัดเวลาไปได้เยอะ

lew Tue, 15/08/2006 - 01:33

loptor - ยิ่งถ้าต้องแบ่งเป็นพันรูปล่ะ ใช้ PIL ดีกว่าแน่ๆ ครับ อย่างมากก็เปิดเครื่องทิ้งไว้คืนนึง คงดีกว่ามานั่งทำเองทั้งคืน.... ------ LewCPE