ラベル 11.Ruby-Program の投稿を表示しています。 すべての投稿を表示
ラベル 11.Ruby-Program の投稿を表示しています。 すべての投稿を表示

2010-03-21

日本語を含む文字列の先頭5文字を取り出す-ruby1.8.7

日本語がなければ簡単"nihonngo"[0..4]

日本語が入った途端に、ややっこしくなる。
なんと、3時間もかかってしまった。
"nih入った途端".split(//u)[0..4].join

出来上がってみれば、なんと簡単なのに
webで検索しまくって split(//u)を見つけ
さらに 検索しまくって joinを見つけ、やっとできた。
もっと簡単なメソッドがないかな~

2009-04-05

写真.jpgを一括サイズ変更-800×600px

お客様からデジカメ写真を一括して小さいサイズにしたいという問い合わせがあった。
Windowsなので、Googleで探せばいっぱいある。
Ubuntuでも結構あるようだ。
でも、やっぱり自分で作ったものが一番だね..

convertコマンドを使うためにはImageMagickのインストールが必要です。
もちろん、Rubyも必要です。

1.写真変換用フォルダに変換用の写真を置く。(~/Photos/temp/)
2.プログラムをこのフォルダに置く。
3.端末で実行
 $ cd ~/Photos/temp/
 $ ruby conv800px.rb

以下、プログラム conv800px.rb -------------------------------------
(インデントは全角スペースなので半角スペースに変換してね)---------

#! ruby -Ku
Dir.open("."){|dir|
 dir.each{|file|
  if File.extname(file) =~ /^\.jpg$/i
   system("convert -geometry 800x600" + " " + file + " " + file)
   puts file
  end
 }
}
 

2008-03-04

写真の解像度を50%にする

カレントディレクトリの写真(.jpg)の解像度を50%にするプログラム。
データ量は約25%になるでしょう。
以下、プログラム(インデントは全角スペースなので、copyしてもエラーが出ます) -----

#! ruby -Ku
Dir.open("."){|dir|
 dir.each{|file|
  if File.extname(file) =~ /^\.jpg$/i
   system("convert " + file + " -resize 50% " + file)
   puts file
  end
 }
}

2008-02-11

c3.解像度変換-完成

これで、ほぼ完成のようです。試験でも実働でも問題なかった。
プログラムもスッキリ。そして、分かりやすくなった。

30枚以上の写真を撮って、r101を名変し、数枚を上位に名変して
HTML をちょっと書換えてから、サーバにアップロード。
1時間ほどで出来てしまった。これからは、余分なことしなくていいから楽できそう。

できあがったページは、 http://www.masou.net/room/room.html
プログラムは、メインが photoconv.rb、メソッド定義が conv1.rb、convx.rb 。
実行は、$ ruby photoconv.rb

ファイル名: photoconv.rb ------------------------------------------------
以下、プログラム(インデントは全角スペースなので、copyしてもエラーが出ます)

#! ruby -Ku

# 手作業-1. copy  camera -> original/, temp/
# 手作業-2. rename temp/500x375用 -> r101.jpg

require "conv1"
require "convx"

ipath = "temp"
opath = "converted"

conv1("r101", "500x375", ipath, opath)
convx("r1", "248x186", ipath, opath, 2, 12)
convx("r2", "122x92", ipath, opath, 1, 18)

ファイル名: convx.rb ----------------------------------------------------
#! ruby -Ku

def convx(name_h, pixel, ipath, opath, first, last)
 Dir.open(ipath){|dir|
  count = first
  dir.sort.each{|file|
   if File.extname(file) =~ /\.(JPG|jpg)/
    ifile = ipath + "/" + file
    ofile = opath + "/" + name_h + format("%02d", count) + ".jpg"
    system("convert -geometry " + pixel + " " + ifile + " " + ofile)
    File.delete(ifile)
    count += 1
   end
   break if count > last
  }
 }
end

ファイル名: conv1.rb ------------------------------------------------
#! ruby -Ku

def conv1(name_h, pixel, ipath, opath)
 upper = ipath + "/" + name_h + ".JPG"
 ifile = ipath + "/" + name_h + ".jpg"
 ofile = opath + "/" + name_h + ".jpg"

 File.rename(upper, ifile) if File.exist?(upper)
 if File.exist?(ifile)
  system("convert -geometry " + pixel + " " + ifile + " " + ofile)
  File.delete(ifile)
 else
  puts "r101.jpg が有りません。中止します。"
  exit
 end
end

2008-02-10

c2.とりあえず完成

とりあえず完成。
convert 248x186 と convert 122x92 の部分は、ほとんど同じなのでメソッドとして別ファイルに書き出す予定。

# 以下、プログラム(インデントは全角スペースなので、copyしてもエラーが出ます)
#! ruby -Ku

# manual-1. copy camera -> original/, temp/
# manual-2. rename temp/500x375用 -> r101.jpg

ipath = "temp"
opath = "converted"

# convert 500x375 -------------------------------------------------
name_h = "r101"
upper = ipath + "/" + name_h + ".JPG"
ifile = ipath + "/" + name_h + ".jpg"
ofile = opath + "/" + name_h + ".jpg"

File.rename(upper, ifile) if File.exist?(upper)
if File.exist?(ifile)
 system("convert -geometry 500x375 " + ifile + " " + ofile)
 File.delete(ifile)
else
 puts "r101.jpg が有りません。中止します。"
 exit
end

# convert 248x186 -------------------------------------------------
name_h = "r1"
count = 2

Dir.open(ipath){|dir|
 dir.sort.each{|file|
  if File.extname(file) =~ /\.(JPG|jpg)/
   ifile = ipath + "/" + file
   ofile = opath + "/" + name_h + format("%02d", count) + ".jpg"
   system("convert -geometry 248x186 " + ifile + " " + ofile)
   File.delete(ifile)
   count += 1
  end
  break if count > 12
 }
}

# convert 122x92 --------------------------------------------------
name_h = "r2"
count = 1

Dir.open(ipath){|dir|
 dir.sort.each{|file|
  if File.extname(file) =~ /\.(JPG|jpg)/
   ifile = ipath + "/" + file
   ofile = opath + "/" + name_h + format("%02d", count) + ".jpg"
   system("convert -geometry 122x92 " + ifile + " " + ofile)
   File.delete(ifile)
   count += 1
  end
  break if count > 18
 }
}

2008-02-09

c1.1枚だけ 500x375 に変換

# 以下、プログラム(インデントは全角スペースなので、copyしてもエラーが出ます)

#! ruby -Ku
ipath = "temp"

# convert 500x375 ----------------------------------------
opath = "px500"
fname = "r101"
upper = ipath + "/" + fname + ".JPG"
ifile = ipath + "/" + fname + ".jpg"
ofile = opath + "/" + fname + ".jpg"

File.rename(upper, ifile) if File.exist?(upper)
if File.exist?(ifile)
 system("convert -geometry 500x375 " + ifile + " " + ofile)
 File.delete(ifile)
else
 puts "r101.jpg が有りません。中止します。"
 exit
end

2008-02-08

c0.解像度変換プログラムの流れ

目的: Webサイト用にデジカメで撮った写真の解像度を変更し、名前を変更する。
解像度:枚数:名前は、500x375:1枚:r101、248x186:11枚:r102-r112、112x92:18枚:r201-r218

以下、簡単な手順
cd ~/Photos

# m(anual), p(rogram)
1m. cp camera -> orgn/, temp/
2m. rename 500x375用 -> temp/r101.jpg
3p. conv500 temp/r101.jpg -> px500/r101.jpg
4p. conv248 temp/* -> px248/ (11枚)
5p. conv122 temp/* -> px122/ (18枚)

2008-01-28

危険.名前を変更して連続番号

Rubyで、はじめて仕事用のプログラムを作りました。
よく使うし、これはとても役に立ちます。
こうしたらいいとか、気の付いたことがあれば教えてください。

# 以下、プログラム(インデントは全角スペースなので、copyしてもエラーが出ます)

ipath = "px1600" #input path
name_h = "r1" #name header
count = 0

Dir.open(ipath).each{|file|
 if file =~ /(.*)\.(JPG|jpg)$/
  count += 1
  name_c = ("00" + count.to_s).slice(-2,2)
  File.rename(ipath + "/" + file, ipath + "/" + name_h + name_c + ".jpg")
 end
}

2008/1/31
 とても大きな問題がありました。ファイルが消失する場合があります。
 変更前と変更後に同じ名前があるとき、ファイルが消滅する可能性があります。
 実際、このプログラムを2回続けて実行すると、消失することがあります。
2008/1/31
 Dir.open(...).each{...} # 推測ですが、closeされないのではないか?
 Dir.open(...){|d| d.each{|f| ...}} # やっぱりこうかな?
 とにかく、ある程度、試用してから公表すべきだよね。
 とにかくうれしくて、ついつい、やってしまった。(後悔)
2008/2/10
 変換元と変換後のフォルダを別にすることにして解決