require "FileUtils"
require "ftools"
filetype = ".jpg"
path_in = Dir.pwd
path_out=path_in+"/size2900"
if !FileTest::directory?(path_out)
Dir::mkdir(path_out)
end
dir=Dir.open(path_in)
a=Array.new(dir.collect.length)
num=0
dir.each do |file|
if(file.length>filetype.length)
if (file[file.length-4..file.length]==filetype)
a[num]=file
num=num+1
end
end
end
puts num
for i in 0..num-1
fold = path_in + "/" + a[i]
fnew_ppm = path_out + "/" + a[i][0..a[i].length-5]+".ppm"
fnew_jpg = path_out + "/" + a[i][0..a[i].length-5]+".jpg"
#check the image size, portrait or landscape
cmdline="identify "+fold+" >t.txt"
system("#{cmdline}")
file = File.new("t.txt", "r")
line = file.gets
m=line.match(/[0-9]*x[0-9]*/)
width=m.to_s.match(/^[0-9]*/)
height=m.to_s.match(/[0-9]*$/)
#resize to ppm
if(width.to_s.to_i>height.to_s.to_i)
puts "landscape"
cmdline="convert -resize 2900x -quality 100 "+fold+" "+fnew_ppm
puts cmdline
system("#{cmdline}")
else
puts "portrait"
cmdline="convert -resize x2900 -quality 100 "+fold+" "+fnew_ppm
puts cmdline
system("#{cmdline}")
end
file.close
#convert ppm to jpg
cmdline="convert -quality 100 "+fnew_ppm+" "+fnew_jpg
puts cmdline
system("#{cmdline}")
puts "---------------------"
#delete ppm
File.delete(fnew_ppm)
end
File.delete("t.txt")
Wednesday, December 2, 2009
[Ruby] resize image (portrait or landscape)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment