Monday, December 21, 2009

[Work] Ruby, check directory folder existence


if File.exists? path_out
puts path_out+" exists"
else
# puts path_out+" doesn't exist"
Dir.mkdir(path_out)
end

Wednesday, December 16, 2009

[work] Free System Utilities for Window

Redmond lab

Free System Utilities for Window

http://redmondlab.googlepages.com/home

[work] gnuplot

C:\gnuplot\bin\wgnuplot.exe plot_gather.pl

file: plot_gather.pl
==============

set output "stoke_C_gather7.png"
#set terminal postscript eps color 22
set terminal png size 1200, 900

set xlabel "Roughness"
set ylabel "C"
plot [][] \
'curve.txt' using 1:2 with lines title "Roughtness-C"

[Work] Ruby, organize text file



files=["stokesCR025.txt",\
"stokesCR05.txt",\
"stokesCR10.txt",\
"stokesCR15.txt",\
"stokesCR20.txt",\
"stokesCR25.txt",\
"stokesCR30.txt",\
"stokesCR35.txt",\
"stokesCR40.txt",\
"stokesCR45.txt",\
"stokesCR50.txt"]
rough=[0.025, 0.050, 0.100, 0.150, 0.200, 0.250, 0.300,0.350,0.400,0.450,0.500]

num=files.size

fout=File.new("curve.txt", "w")
for j in 0..num-1
fname=files[j]
puts "fname "+fname
roughness=rough[j]

file = File.new(fname, "r")
i=0
while i<2 do
line = file.gets
i=i+1
end
value1=line.match(/^[0-9]* /)
puts value1.to_s
value2=line.match(/ [-,.,0-9]*$/)
puts value2.to_s
file.close

fout.puts roughness.to_s+" "+value2.to_s
end
fout.close

Wednesday, December 2, 2009

[Ruby] make thumbnails


require "FileUtils"
require "ftools"

filetype = ".jpg"
path_in = Dir.pwd

path_out=path_in+"/thumbnail"
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"

cmdline="convert -resize 200x -quality 100 "+fold+" "+fnew_ppm
puts cmdline
system("#{cmdline}")

cmdline="convert -quality 100 "+fnew_ppm+" "+fnew_jpg
puts cmdline
system("#{cmdline}")

puts "---------------------"
File.delete(fnew_ppm)
end

[Ruby] resize image (portrait or landscape)


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")

[Ruby] file rename


filetype = ".jpg"

path = "."
prefix="slide_"

dir=Dir.open(path)
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

count=0
for i in 0..num-1
fnew=prefix+"%05d"%count+a[i][a[i].length-4..a[i].length]
count=count+1
old_file = path + "/" + a[i]
new_file = path + "/" + fnew
puts "old "+old_file
puts "new "+new_file
puts "--------------"
File.rename(old_file, new_file)
end

[Ruby] is a number


def is_a_number(s)
s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end