Skip to content

Ruby Class:Day 8 Calendar for 2011

30/12/2010

This day start with RMagick is a complete interface between the Ruby programming language and the ImageMagick and GraphicsMagick image processing libraries.

Need for install
1.librmagick-ruby – ImageMagick API for Ruby
2.librmagick-ruby-doc – ImageMagick API for Ruby (documentation)
3.librmagick-ruby1.8 – ImageMagick API for Ruby 1.8

simple Rmagic program

require 'rubygems'
require 'RMagick'
include Magick
image =  ImageList.new("123.JPG")
text = Magick::Draw.new
text.font_family = 'helvetica'
text.pointsize = 42
# We write the text and set its color
text.annotate(image, 100,100,302,202, "Happy new Year"){self.fill = 'yellow'}
image.display

to know about Rmagic see here http://www.mustap.com/rubyzone_post_185_manipulating-images-with-rma
calendar program implement in Rmagic as:
calendar.rb

class Ithuthandavarusam
  def initialize
    @caption=''
    @week_heading = ''
    @first_line = ''
    @second_line = ''
    @third_line = ''
    @fourth_line = ''
    @fifth_line = ''
    @sixth_line = ''
  end

  def process(first_day)
    month = first_day.month
    year = first_day.year
    printing_calendar(first_day,compute_end_date_of_the_month(month,year))
  end

  def compute_end_date_of_the_month(month,year)
    if (((year % 4) == 0) and (month == 2))
      end_date_of_the_month = 29
    elsif (((year % 4) != 0) and (month == 2))
      end_date_of_the_month = 28
    end
    if [1,3,5,7,8,10,12].include?(month) then
      end_date_of_the_month = 31
    elsif [4,6,9,11].include?(month) then
      end_date_of_the_month = 30
    end
    return end_date_of_the_month
  end
#print end_date_of_the_month
def week_heading
 @week_heading = @week_heading +"Sun Mon Tue Wed Thu Fri Sat"
end

def printing_calendar(first_day,end_date_of_the_month)
  day_in_number =  {:sun => 1, :mon => 2,:tue =>3,:wed =>4 ,:thu => 5, :fri =>6 , :sat =>7}
  @caption = @caption + "%28s"%first_day.strftime("%B")
  day_in_word = first_day.strftime("%a").downcase
  leading_spaces =''
  for spacer in  1...day_in_number[day_in_word.to_sym] do
    leading_spaces = leading_spaces + (" "*4)
    print leading_spaces
  end

  first_line = leading_spaces
  running_day = first_day.day
    for filler in day_in_number[day_in_word.to_sym]..7
      first_line = first_line + "%4d"%running_day
      running_day += 1
    end
    week_heading
    @first_line = @first_line + first_line
    first_line = ''
for iteration in 1..3
for day in 1..7 do
first_line = first_line + "%4d"%running_day
running_day += 1
end
@second_line = @second_line + first_line if iteration == 1
@third_line = @third_line + first_line if iteration == 2
@fourth_line = @fourth_line + first_line if iteration == 3
first_line = ''
end
if ((end_date_of_the_month - (running_day-1))>= 7)
for day in 1..7 do
first_line = first_line + "%4d"%running_day
running_day += 1
end
else
fifth_week_complete_day = running_day
for day in fifth_week_complete_day..end_date_of_the_month
first_line = first_line + "%4d"%running_day
running_day += 1
end
last_day = Time.local(first_day.year,first_day.month,end_date_of_the_month)
day_in_word = last_day.strftime("%a").downcase
for filler in day_in_number[day_in_word.to_sym]...7 do
first_line = first_line + " "*4
end
end
@fifth_line += first_line
first_line = ''
if ((end_date_of_the_month - (running_day-1))> 0)
sixth_week_complete_day = running_day
for day in sixth_week_complete_day..end_date_of_the_month
first_line = first_line + "%4d"%running_day
running_day += 1
end
last_day = Time.local(first_day.year,first_day.month,end_date_of_the_month)
day_in_word = last_day.strftime("%a").downcase
for filler in day_in_number[day_in_word.to_sym]+1..7 do
first_line = first_line + " "*4
end
else
first_line = first_line + " "*28
end
@sixth_line += first_line

end
def print_caption
$line1=@caption
$line2=@week_heading
$line3=@first_line
$line4=@second_line
$line5=@third_line
$line6=@fourth_line
$line7=@fifth_line
$line8=@sixth_line
@caption=''
@first_line = ''
@week_heading = ''
@second_line = ''
@third_line = ''
@fourth_line =''
@fifth_line = ''
@sixth_line = ''
end
end

Rmagic with calender
screen.rb

require 'calendar.rb'
require 'rubygems'
require 'RMagick'
include Magick
image =  ImageList.new("ubuntu.jpg")
text = Magick::Draw.new
text.font_family = 'helvetica'
text.pointsize = 20
x1,y1,x2,y2=50,30,50,30
text.annotate(image,x1,y1,x2,y2,"Calender"){}
y1+=60
y2+=60

# We write the text and set its color
print "Please enter the Year in YYYY format "
year = gets.chomp.to_i
this_year = Ithuthandavarusam.new
ranges = [1..2,3..4,5..6,7..8,9..10,11..12]
ranges.each do |range|
for month in range do
first_day = Time.local(year,month,1)
this_year.process(first_day)
end
this_year.print_caption
text.annotate(image, x1,y1,x2,y2, $line1){self.fill = 'black'}
y1+=40
y2+=40
text.annotate(image, x1,y1,x2,y2, $line2){self.fill = 'Green'}
y1+=40
y2+=40
text.annotate(image, x1,y1,x2,y2, $line3){self.fill = 'darkred'}
y1+=40
y2+=40
text.annotate(image, x1,y1,x2,y2, $line4){self.fill = 'darkred'}
y1+=40
y2+=40
text.annotate(image, x1,y1,x2,y2, $line5){self.fill = 'darkred'}
y1+=40
y2+=40
text.annotate(image, x1,y1,x2,y2, $line6){self.fill = 'darkred'}
y1+=40
y2+=40
text.annotate(image, x1,y1,x2,y2, $line7){self.fill = 'darkred'}
y1+=40
y2+=40
text.annotate(image, x1,y1,x2,y2, $line8){self.fill = 'darkred'}
y1+=40
y2+=40
text.annotate(image, x1,y1,x2,y2, "\n\n\n\n"){self.fill = 'darkred'}
end
image.display

but i having problem with this.this format work while display in terminal but if i write same content with same formate in image alignment problem occurs still now i could not solve that.

Advertisements

From → Ruby

2 Comments
  1. please upload here with your output calendar image which has created by your ruby script man… !.

    😛

  2. I could not agree with you more..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: