Skip to content

Instantly share code, notes, and snippets.

@obelisk68
Created May 5, 2016 12:32
Show Gist options
  • Save obelisk68/bfd73c8374c0b2525d3cec4a76e1ef9e to your computer and use it in GitHub Desktop.
Save obelisk68/bfd73c8374c0b2525d3cec4a76e1ef9e to your computer and use it in GitHub Desktop.
# encoding: Shift_JIS
# for Windows
require 'green_shoes'
Sf = %w(png PNG jpg jpeg JPG JPEG gif GIF bmp BMP)
class String
def imgfile?
return false unless m = /\.(.+)$/.match(self)
return m[1] if Sf.include?(m[1])
false
end
def imgsuffix
Sf.each do |sf|
a = "." + sf
return a if include?(a)
end
""
end
end
Shoes.app width: 500, height: 480 do
background lavender
title "画像変換".encode("utf-8")
fname1 = fname2 = ""
stack height: 120 do
background azure
b = button "変換されるファイルを選択".encode("utf-8") do
fname1 = ask_open_file
end
a = para ""
b.click {a.clear; a = para fname1}
end
para "変換先のファイル名".encode("utf-8")
edit_line(width: 300) {|e| fname2 = e.text}
button "変換".encode("utf-8") do
if fname1.imgfile? and fname2.imgfile?
Dir.chdir(File.dirname(fname1))
`convert ./#{File.basename(fname1)} ./#{fname2}`
puts "converted"
exit
end
end
fname3 = ""
sf1 = sf2 = ""
para "\n"
stack height: 120 do
background moccasin
para "一括変換するフォルダ".encode("utf-8")
b1 = button "選択".encode("utf-8") do
fname3 = ask_open_folder
end
a2 = para ""
b1.click {a2.clear; a2 = para fname3}
end
para "画像形式を選択して下さい(変換前/変換後)".encode("utf-8")
list_box items: ["jpg", "png", "bmp", "gif"] {|t| sf1 = t.text}
list_box items: ["jpg", "png", "bmp", "gif"] {|t| sf2 = t.text}
button "変換" do
if sf1 != sf2
Dir.chdir(fname3)
unless File.exist?("converted") and File.directory?("converted")
Dir.mkdir("converted")
end
Dir.chdir(fname3)
Dir.glob("*").each do |fn1|
a = fn1.imgsuffix.downcase
a = ".jpg" if a == ".jpeg"
if File.file?(fn1) and a == "." + sf1
fn2 = /(.+)\..+$/.match(fn1)[1] + "." + sf2
`convert #{fn1} ./converted/#{fn2}`
end
end
puts "converted"
exit
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment