Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Last active January 1, 2016 11:49
Show Gist options
  • Select an option

  • Save joshdholtz/8140463 to your computer and use it in GitHub Desktop.

Select an option

Save joshdholtz/8140463 to your computer and use it in GitHub Desktop.
Quick script to scale retina images down 50% into a new file in current directory I also added a bash script in my /usr/local/bin so that I can run this from anywhere
#!/bin/bash
ruby ~/scripts/unretina_it.rb
require 'RMagick'
Dir.new('.').each do |file|
# Ignore hidden files
if !file.start_with?(".")
# Finds the file extension and file name without the extension (there is probably be an easier way but whatevs)
start_extension = file.rindex(".")
filename = file.slice(0, start_extension)
extension = file.slice(start_extension, file.length - start_extension)
# Finds only the filenames that are retina (ends with @2x)
if filename.end_with?("@2x")
# Removes the "@2x" from the filename for the unretina filename
scaled_filename = filename.sub("@2x", "")
# A puts you putz
puts "Going to scale #{filename}#{extension} to #{scaled_filename}#{extension}"
# Scales by 50% into new image (this probably overwrites if unretina already exists, I'm not sure though)
img = Magick::Image::read( "#{filename}#{extension}" ).first
thumb = img.scale(0.5)
thumb.write "#{scaled_filename}#{extension}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment