Make a stamped for a Photo; ทำกรอบรอบรูป

Posted by Revolution Wed, 06 Aug 2008 10:18:16 GMT

หลังจากที่ได้ D80 มาสมใจอยาก เห็นหลายๆคนเวลาแสดงรูปจะมีการแสดงความเป็นเจ้าของโดยใส่ลายน้ำหรือใส่ข้อมูล exif หรือลายเซ็นไว้ ก็เลยอยากจะทำมั่ง ใน Photoshop ก็สามารถเขียน script(java script) ให้ทำทีละหลายๆรูปได้ หรือจะใช้ ACDSee ก็ทำได้ ส่วนสำหรับบน Linux ไม่ค่อยมีข้อมูลเท่าไหร่ แต่ Tools และ Libs เพียบ เลยตกงว่าจะออกกำลังเขียน ไหนๆก็ไหนๆ แล้วเขียนด้วย ruby ซะเลย นี่เป็น version ใช้เอง แต่ก็สามารถเขียนให้ดีกว่านี้เพื่อเอาไปใช้กับงานอื่นๆได้ code License GPL2


After I got D80, I saw many people show up their photos with a water mark, signature, exif data. I think that I have to do same way. Photoshop can do with a script, ACDSee can do also but Linux have no idea about that ( I may not know). By the way ,Linux come wiht many tools and Libs so I make a decision to write a ruby script for do that. This code is under GPL2 License. This version is for my own purpose, it may not be well design.


Requirement: Ruby 1.8.6, RMagick

Usage: run script at same Photo Directory, script will generate a directory "stamped" and file name is "stamped-.jpg"
#!/usr/bin/env ruby

require 'RMagick'
include Magick

PLACE = "Place of photo, It will appear at right-bottom "

NEWSIZE = 0.5 #Percent of Photo, 1 is equal to original
BGCOLOR = "#000000"
TEXTSPACE = 129

#Font for Exif Data Show at left-bottom
Font = "arial"
FontColor = "orange"
FontSize = 28 

#Font for Place Show at right-bottom
Font2 = "arial"
FontColor2 = "green"
FontSize2 = 28 

BD_SIZE = 20 #Border Size
BD_COLOR = "#FFFFFF" #Border Color

exif_info = ["Model","ExposureTime","FNumber","FocalLength","ISOSpeedRatings"]
exif_show = [" ","T: ","f/","Length ","ISO "]

author = " http://revolution.rubybox.net by rubybox@gmail.com"

def makecal(value)
	return "-" if value.nil?
	e = value.split("/")
	return "1/" + (e[1].to_f / e[0].to_f).to_s if e[1].to_f > e[0].to_f
	return  (e[0].to_f / e[1].to_f).to_s if e.size > 1
	return value
end

def orientation(photo)
	return photo.get_exif_by_entry("Orientation").assoc("Orientation")[1]
end

def transform(photo)
	o = orientation(photo)
	return photo if o == "1"
	return photo.flop! if o == "2"
	return photo.rotate!(180) if o == "3"
	return photo.flip! if o == "4"
	return photo.tranpose! if o == "5"
	return photo.rotate!(90) if o == "6"
	return photo.transverse! if o == "7"
	return photo.rotate!(270) if o == "8"
	return photo
	
end

def frameit(raw)
	raw.background_color = BGCOLOR
	orient = orientation(raw)
	photo = transform(raw)
	photo.resize!(NEWSIZE) 
	photo.border!(BD_SIZE,BD_SIZE,BD_COLOR)

	nphoto = photo.extent(photo.columns,photo.rows + TEXTSPACE,0,0)

	return nphoto
end



getImg = Dir['*.jpg'].sort
Dir.mkdir("stamped") if !File.exist?("stamped")

for p in getImg
	rawphoto = Image.read(p).first
	photo = frameit(rawphoto)

	exif = photo.get_exif_by_entry
	info = String.new
	for n in exif_info
		if exif.assoc(n).nil? then exif_value = "-" else exif_value = exif.assoc(n)[1] end
		info += exif_show[exif_info.index(n)] + makecal(exif_value) + ", "
	end
	info += "\n" + author

	ann = Draw.new
	ann.annotate(photo,photo.columns,photo.rows,10,10, info) {
		self.stroke = 'transparent'
		self.gravity = SouthWestGravity
		self.encoding = 'utf8'
		self.font_family = Font
		self.fill = FontColor
		self.pointsize = FontSize
	}

	ann2 = Draw.new
	#ann2.annotate(photo,photo.columns,TEXTSPACE,photo.columns - (PLACE.size * 36),photo.rows - (TEXTSPACE/6), "@" + PLACE) {
	ann2.annotate(photo,photo.columns,photo.rows,10,10, PLACE) {
		self.stroke = 'transparent'
		self.encoding = 'utf8'
		self.gravity = SouthEastGravity
		self.font_family = Font2
		self.fill = FontColor2
		self.pointsize = FontSize2
	}
	photo.write("stamped/stamp-#{p}")
end

Comments - (Leave a comment)

Trackbacks

Use the following link to trackback from your own site:
/articles/trackback/2189

Leave a comment