Skip to content

Instantly share code, notes, and snippets.

@kachick
Last active May 15, 2017 09:26
Show Gist options
  • Save kachick/8453488 to your computer and use it in GitHub Desktop.
Save kachick/8453488 to your computer and use it in GitHub Desktop.
TIFFなりなんなりの無圧縮or可逆圧縮データからImageMagick経由で可逆圧縮PNGを生成した後に、pngquantで減色する (文字中心データ対象なら--speed は10で十分。標準の3だとめっちゃ遅い)
# coding: us-ascii
require 'logger'
sources = ARGV.dup
abort "Pass target sources." if sources.empty?
class MultiLogger
def initialize(*destinations)
@destinations = destinations.map { |dst| Logger.new dst }
end
Logger.instance_methods.each do |meth|
define_method meth do |*args, &block|
@destinations.each { |dst| dst.__send__ meth, *args, &block }
end
end
end
color = 16
imagemagick = 'convert'
pngquant = 'pngquant'
logger = MultiLogger.new $stdout, 'PNG-oprations.log'
failures = []
sources.each do |source|
basepath, _, extname = source.rpartition('.')
source_type = extname.upcase
basename = File.basename basepath
lossless_png = "#{basepath}.png"
result = "#{basepath}_#{color}color.png"
begin
logger << "#{basename}: Converting #{source_type} to lossless PNG\n"
`#{imagemagick} #{source} -compress LossLess #{lossless_png}`
logger << "#{basename}: Decreasing the PNG color to #{color}\n"
`#{pngquant} --ext _#{color}color.png --speed 10 #{color} #{lossless_png}`
raise 'Ouch! ><' unless File.exist? result
rescue Exception => err
failures << basename
logger.fatal "#{basename}: Failed: #{err}\n"
else
File.delete source
File.delete lossless_png
logger << "#{basename}: Success\n"
end
end
logger.<<(failures.empty? ? "Yatta! ^^\n" : "Akankatta! >< #{failures.inspect}\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment