Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Howto resize multiple pictures, graphics, images

for k in $(ls *.jpg); do convert -resize 800 -quality 80 $k r800-$k; done

Batch re-size a collection of images from the command line

for img in $(ls *.png); do convert $img -resize 75% smaller-$img; done;

Generate a graph using Gruff

This Ruby code produced a graph using gruff. The output shows a line graph [twitxr.com] for the different fruits. Source code origin: Gruff Update With Bar Graphs | Ruby on Rails for Newbies [rubyonrails.com]

require 'gruff'

g = Gruff::Line.new
g.title = "My Graph" 

g.data("Apples", [1, 2, 3, 4, 4, 3])
g.data("Oranges", [4, 8, 7, 9, 8, 9])
g.data("Watermelon", [2, 3, 1, 5, 6, 8])
g.data("Peaches", [9, 9, 10, 8, 7, 9])

g.labels = {0 => '2003', 2 => '2004', 4 => '2005'}

g.write('my_fruity_graph.png')


Note: I executed the code within an irb session on my Gentoo box. With Gentoo, Gruff was installed [gentoo-portage.com] using the command emerge -va gruff. I tried installing it on Ubuntu but ran into some difficulty, even with help from the article install rmagick ubuntu [dzone.com].

*udpate 21:48 24-Feb*

The following code does exactly as the same code above, however it uses XML to separate the data from the process, making it easier and more efficient to build graphs.
#!/usr/bin/ruby
# file: xml2gruff.rb

require 'rexml/document'
require 'gruff'
include REXML

class Xml2Gruff
  
  def initialize(filename)
    file = File.new(filename, 'r')
    doc = Document.new(file)
    # get the title
    @title = doc.root.elements['summary/title'].text
    
    @record = Hash.new
    # get each record
    doc.root.elements.each('records/item') {|item|
      avalues = Array.new
      item.elements.each('values/value') { |value| avalues << value.text.to_i }
      @record[item.elements['label'].text] = avalues
    }
    
    # get the summary labels
    @labels = Hash.new  
    doc.root.elements.each('summary/scale/label') {|l| @labels[l.elements['value'].text.to_i] = l.elements['title'].text} 
    
  end
  
  def save_line_graph(filename)

    g = Gruff::Line.new
    g.title = @title 
    @record.each {|label, data| g.data(label, data) }
    g.labels = @labels
    g.write(filename)

  end
end

if __FILE__ == $0
 x2g = Xml2Gruff.new('my_fruit.xml')
 x2g.save_line_graph('my_fruit2.png')
end


file: my_fruit.xml
<graph>
  <summary>
    <title>My Graph</title>
    <scale>
      <label><title>2003</title><value>0</value></label>
      <label><title>2004</title><value>2</value></label>
      <label><title>2005</title><value>4</value></label>
    </scale>
  </summary>
  <records>
    <item>
      <label>Apples</label>
      <values><value>1</value><value>2</value><value>3</value><value>4</value><value>4</value><value>3</value></values>
    </item>
    <item>
      <label>Oranges</label>
      <values><value>4</value><value>8</value><value>7</value><value>9</value><value>8</value><value>9</value></values>
    </item>
    <item>
      <label>Watermelon</label>
      <values><value>2</value><value>3</value><value>1</value><value>5</value><value>6</value><value>8</value></values>
    </item>
    <item>
      <label>Peaches</label>
      <values><value>9</value><value>9</value><value>10</value><value>8</value><value>7</value><value>9</value></values>
    </item>
  </records>
</graph>

Reference: gruff's gruff-0.2.9 Documentation [rubyforge.org]
to ruby rmagick image ubuntu gentoo magick graph chart gruff by jrobertson on Feb 24, 2008

Avatar Resizer

I go on a lot of Bulletin Board, every has its own limits of size for the avatars, instead of the resize manually I created a script which does it for me with RMagick

#!/usr/bin/ruby
require "RMagick"
$SIZES = [80 , 100 , 110 , 128]

if !ARGV[0]
  puts "Usage: mk_avatars.rb SourceAvatarPath"
  exit
end

image = Magick::Image.read(ARGV[0]).first
$SIZES.each do |sz|
  puts "Generating Avatar : #{sz}"
  out = image.thumbnail(sz,sz)
  file = "out_#{sz}.#{image.format}"
  out.write(file)
end

Resizing images using RMagick

require 'rubygems'
require 'RMagick'

include Magick

def append_filename(filename, suffix)
  extrac = filename.split('.')
  extrac[-2] += suffix
  extrac.join('.')
end                                                                                  
ARGV.each do |f|
  ImageList.new(f).resize(800, 600).write(append_filename(f, '_l'))
  ImageList.new(f).resize(400, 300).write(append_filename(f, '_m'))
  ImageList.new(f).resize(40, 30).write(append_filename(f, '_s'))
end
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS