Skip to content

Instantly share code, notes, and snippets.

@kristm
Last active August 29, 2015 14:04
Show Gist options
  • Save kristm/f15314dc97e958486475 to your computer and use it in GitHub Desktop.
Save kristm/f15314dc97e958486475 to your computer and use it in GitHub Desktop.
amfufu
#!/usr/bin/env ruby
require 'RMagick'
require 'fileutils'
#color parser from https://gist.github.com/edouard/1787879
TOP_N = 10 # Number of swatches
def sort_by_decreasing_frequency(img)
hist = img.color_histogram
sorted = hist.keys.sort_by {|p| -hist[p]}
new_img = Magick::Image.new(hist.size, 1)
new_img.store_pixels(0, 0, hist.size, 1, sorted)
end
def get_pix(img)
palette = Magick::ImageList.new
pixels = img.get_pixels(0, 0, img.columns, 1)
end
def get_dominant_color(path)
original = Magick::Image.read(path).first
quantized = original.quantize(TOP_N, Magick::RGBColorspace)
normal = sort_by_decreasing_frequency(quantized)
pixels = get_pix(normal)
pixels[0].to_hsla.first.round if pixels
end
#end color parser
def main
target_year, start_index, total_images = ARGV.empty? ? [2005,0,50] : ARGV.map! { |o| o.to_i }
start_index = 0 if start_index.nil?
total_images = 50 if total_images.nil?
offset = "#{target_year}000000".to_i + start_index
(offset..(offset + total_images)).to_a.each do |index|
begin
image_source = "#{ENV['AMFUFU_BASE']}/#{target_year}/#{index}p.JPG"
image_output = "#{index}.jpg"
system "curl #{image_source} -o #{image_output}"
color = get_dominant_color image_output
FileUtils.mv File.absolute_path(image_output), "#{color}-#{image_output}"
rescue Magick::ImageMagickError
puts "Invalid image"
FileUtils.rm image_output
end
end
end
main if __FILE__ == $0 and !ENV['AMFUFU_BASE'].nil?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment