Permalink
Browse files

Be helpful: write a descriptive message to stdout when invoked.

  • Loading branch information...
1 parent 59eb049 commit 25bd366f2f6feecdae43a069057392bb45ad9dc6 rmagick committed Sep 7, 2003
Showing with 108 additions and 36 deletions.
  1. +12 −0 examples/describe.rb
  2. +38 −23 examples/histogram.rb
  3. +11 −3 examples/image_opacity.rb
  4. +21 −4 examples/import_export.rb
  5. +16 −3 examples/pattern_fill.rb
  6. +10 −3 examples/rotating_text.rb
View
@@ -5,6 +5,18 @@
require 'RMagick'
+puts <<END_INFO
+
+This example shows how to extract attributes from an image.
+
+END_INFO
+
+if ARGV.length == 0
+ puts "Specify one or more image filenames as arguments."
+ exit
+end
+
+
ARGV.each { |file|
puts file
img = Magick::Image::read(file).first
View
@@ -1,5 +1,6 @@
# This is the prototype for a new HISTOGRAM coder to
-# be added to the next release of GraphicsMagick.
+# be added to the next release of GraphicsMagick. Specify
+# an image filename as an argument.
require 'RMagick'
@@ -110,30 +111,36 @@ def pixel_intensity(pixel)
# Make the color histogram. Quantize the image to 256 colors if necessary.
def color_freq(fg, bg)
img = number_colors > 256 ? quantize(256) : self
- hist = img.color_histogram
- pixels = hist.keys.sort_by {|pixel| hist[pixel] }
-
- scale = HISTOGRAM_ROWS / (hist.values.max*AIR_FACTOR)
histogram = Image.new(HISTOGRAM_COLS, HISTOGRAM_ROWS) {
self.background_color = bg
self.border_color = fg
}
- gc = Draw.new
- gc.affine(1, 0, 0, -scale, 0, HISTOGRAM_ROWS)
- width = 256.0/img.number_colors
- gc.stroke_width(width.to_i)
-
- start = 256 - img.number_colors
- pixels.each { |pixel|
- gc.stroke(pixel.to_color)
- gc.line(start, 0, start, hist[pixel])
- start += width
- }
-
- gc.draw(histogram)
+ begin
+ hist = img.color_histogram
+ pixels = hist.keys.sort_by {|pixel| hist[pixel] }
+ scale = HISTOGRAM_ROWS / (hist.values.max*AIR_FACTOR)
+
+ gc = Draw.new
+ gc.affine(1, 0, 0, -scale, 0, HISTOGRAM_ROWS)
+ width = 256.0/img.number_colors
+ gc.stroke_width(width.to_i)
+
+ start = 256 - img.number_colors
+ pixels.each { |pixel|
+ gc.stroke(pixel.to_color)
+ gc.line(start, 0, start, hist[pixel])
+ start += width
+ }
+
+ gc.draw(histogram)
+ rescue NotImplementedError
+ $stderr.puts "The color_histogram method is not supported by this version "+
+ "of ImageMagick/GraphicsMagick"
+ end
+
histogram['Label'] = 'Color Frequency'
return histogram
end
@@ -217,22 +224,30 @@ def histogram(fg='white', bg='black')
end
end
+puts <<END_INFO
+
+This example shows how to get pixel-level access to an image.
+Usage: histogram.rb <image-filename>
+
+END_INFO
# Get filename from command line.
-if !ARGV[0] || ARGV[0] == '-?' then
- puts "Usage: histogram.rb <image-file>"
- exit
+if !ARGV[0] then
+ puts "No filename argument. Defaulting to Hot_Air_Balloons.jpg"
+ filename = '../doc/ex/images/Hot_Air_Balloons.jpg'
+else
+ filename = ARGV[0]
end
# Only process first frame if multi-frame image
-image = Magick::Image.read(ARGV[0])
+image = Magick::Image.read(filename)
if image.length > 1
puts "Charting 1st image"
end
image = image.first
# Give the user something to look at while we're working.
-name = File.basename(ARGV[0]).sub(/\..*?$/,'')
+name = File.basename(filename).sub(/\..*?$/,'')
$defout.sync = true
printf "Creating #{name}_Histogram.miff"
@@ -5,7 +5,14 @@
require 'RMagick'
include Magick
-balloons = Image.read('images/Hot_Air_Balloons_H.jpg').first
+puts <<END_INFO
+
+This example uses the opacity attribute to create a semi-transparent title.
+View the resulting image by entering the command: display image_opacity.miff
+
+END_INFO
+
+balloons = Image.read('../doc/ex/images/Hot_Air_Balloons_H.jpg').first
legend = Image.new(160, 50) { self.background_color = 'white' }
gc = Draw.new
@@ -14,8 +21,9 @@
self.stroke = 'transparent'
}
-legend.opacity = 0.70*TransparentOpacity
+legend.opacity = 0.50*TransparentOpacity
result = balloons.composite(legend, NorthWestGravity, OverCompositeOp)
-result.write 'test.miff'
+puts "...Writing image_opacity.miff"
+result.write 'image_opacity.miff'
exit
@@ -1,14 +1,31 @@
+#
+# Demonstrate the export_pixels and import_pixels methods.
+#
require 'RMagick'
include Magick
+puts <<END_INFO
+
+This example demonstrates the export_pixels and import_pixels methods
+by copying an image one row at a time. The result is an copy that
+is identical to the original.
+
+END_INFO
+
img = Image.read('../doc/ex/images/Gold_Statue.jpg').first
copy = Image.new(img.columns, img.rows);
-img.rows.times { |r|
- scanline = img.export_pixels(0, r, img.columns, 1, "RGB");
- copy.import_pixels(0, r, img.columns, 1, "RGB", scanline);
- }
+begin
+ img.rows.times { |r|
+ scanline = img.export_pixels(0, r, img.columns, 1, "RGB");
+ copy.import_pixels(0, r, img.columns, 1, "RGB", scanline);
+ }
+rescue NotImplementedError
+ $stderr.puts "The export_pixels and import_pixels methods are not supported" +
+ " by this version of ImageMagick/GraphicsMagick"
+ exit
+end
copy.display
exit
@@ -8,18 +8,31 @@
require 'RMagick'
include Magick
+puts <<END_INFO
+
+This example demonstrates the PATTERN: image format, which is
+new in ImageMagick 5.5.7. Specify the name of any of the
+supported patterns as an argument. For example, try "checkerboard".
+
+END_INFO
+
class PatternFill < Magick::TextureFill
def initialize(name='bricks')
@pat_img = Magick::Image.read("pattern:#{name}").first
super(@pat_img)
end
end
-raise(ArgumentError, "No pattern name specified") unless ARGV[0]
+if ARGV[0]
+ pattern = ARGV[0]
+else
+ $stderr.puts "Defaulting to checkerboard pattern."
+ pattern = 'checkerboard'
+end
# Create a sample image that is 100x bigger than the pattern.
-attrs = Image.ping("pattern:#{ARGV[0]}").first
+attrs = Image.ping("pattern:#{pattern}").first
-tryit = Image.new(10*attrs.columns, 10*attrs.rows, PatternFill.new(ARGV[0]))
+tryit = Image.new(10*attrs.columns, 10*attrs.rows, PatternFill.new(pattern))
tryit.display
exit
@@ -1,9 +1,15 @@
+# Demonstrate the Draw#rotation= method by producing
+# an animated MIFF file showing a rotating text string.
+
require 'RMagick'
include Magick
-# Demonstrate the Draw#rotation= method by producing
-# an animated MIFF file showing a rotating text string.
+puts <<END_INFO
+Demonstrate the rotation= attribute in the Draw class
+by producing an animated image. View the output image
+by entering the command: animate rotating_text.miff
+END_INFO
text = Draw.new
text.pointsize = 28
@@ -37,5 +43,6 @@
# ignored if ImageMagick not configured with ZLIB
animation.compression = ZipCompression
#animation.animate
-animation.write("rotated_text.miff")
+puts "...Writing rotating_text.miff"
+animation.write("rotating_text.miff")
exit

0 comments on commit 25bd366

Please sign in to comment.