ERLEDIGT
NEIN
NEIN
ANTWORTEN
3
3
ZUGRIFFE
1419
1419
EMPFEHLEN
-
Hallo,
hier mal meine Lösung in Ruby:
Code ruby:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#!/usr/bin/env ruby require 'rubygems' require 'rmagick' require 'trollop' include Magick # Optionen parsen options = Trollop::options do banner 'Usage: dice.rb [options]' text 'Options:' opt :input, 'Input image (required)', :required => true, :type => String opt :output, 'Output image (required)', :required => true, :type => String opt :dice, 'Dice image (required)', :required => true, :type => String opt :resize, 'Resize image before processing', :type => String end # Würfelbild einlesen und zerschneiden dices = Image::read(options[:dice]).first DICE_SIZE = dices.columns dice = [] 6.times do |d| dice[d] = dices.crop(0, d*DICE_SIZE, DICE_SIZE, DICE_SIZE) end # Eingabebild einlesen img = Image::read(options[:input]).first # Eingabebild verkleinern, falls gewünscht if options[:resize] begin img.change_geometry(options[:resize]) {|cols, rows| img.resize!(cols, rows) } rescue ArgumentError => e puts e exit(1) end end # Bild mit den Farben für die Farbreduzierung erzeugen colors = Image.new(6, 1, GradientFill.new(0, 0, 0, 0, '#000', '#fff')) # Umwandlung in Graustufen img = img. quantize(256, GRAYColorspace, NoDitherMethod). # in Graustufen umwandeln normalize. # Kontrastspreizung remap(colors, FloydSteinbergDitherMethod) # Farbreduzierung mit Dithering # Ausgabebild anlegen result = Image.new(img.columns*DICE_SIZE, img.rows*DICE_SIZE) # Würfel setzen img.each_pixel do |pixel, x, y| i = 5 - (pixel.intensity.to_f/QuantumRange*5).to_i result.composite!(dice[i], x*DICE_SIZE, y*DICE_SIZE, CopyCompositeOp) end # Ausgabebild schreiben result.write(options[:output])
Wie ihr seht verwende ich RMagick, die Ruby-Schnittstelle zu ImageMagick. Damit reduzieren sich die Umwandlung in Graustufen, die Kontrastspreizung und die Quantisierung mit Dithering auf ein paar einfache Methodenaufrufe
Zum Parsen der Kommandozeilenparameter kommt das schicke Trollop zum Einsatz, was ich vorher noch nicht kannte. Der Grund, warum ich mich nach einer Alternative zum Standardbibliothek-Bestandteil optparse umgeschaut habe, ist simpel: die Parameterdefinition mit optparse war länger als der Rest des Skripts und hätte somit nur vom Wesentlichen abgelenkt.
Ein Beispielaufruf:
Code :1
ruby dice.rb -i Me_by_miss_mosh.jpg -o me.png -d wuerfel9-2.png -r 150
Mit dem Quellbild http://miss-mosh.deviantart.com/art/Me-107120469 ergibt das folgende Ausgabe:
![[QUIZ#14] Matthias Reitinger (Ruby)-me.png](http://www.tutorials.de/attachments/archiv/51619d1322823100t-me.png)
Grüße,
Matthias„Gib einem Menschen einen Fisch, und er wird für einen Tag satt. Lehre ihn Fischen, und er wird ein Leben lang satt.“
“For every complex problem, there is an answer that is short, simple and wrong.”
“Pessimism is safe, but optimism is a lot faster!”
Aktuelles Coding Quiz: #17 - Wörter kreuz und quer
-
27.03.10 23:16 #2
Dein Outputbild ist mehr als doppelt so groß wie input
-
„Gib einem Menschen einen Fisch, und er wird für einen Tag satt. Lehre ihn Fischen, und er wird ein Leben lang satt.“
“For every complex problem, there is an answer that is short, simple and wrong.”
“Pessimism is safe, but optimism is a lot faster!”
Aktuelles Coding Quiz: #17 - Wörter kreuz und quer
-
28.03.10 14:19 #4
Ich war nur verwundert
Ähnliche Themen
-
[QUIZ#13] Matthias Reitinger (Ruby)
Von Matthias Reitinger im Forum ArchivAntworten: 0Letzter Beitrag: 17.01.10, 17:02 -
[QUIZ#12] Matthias Reitinger (Ruby)
Von Matthias Reitinger im Forum ArchivAntworten: 0Letzter Beitrag: 14.11.09, 17:15 -
[QUIZ#10] Matthias Reitinger (Ruby)
Von Matthias Reitinger im Forum ArchivAntworten: 1Letzter Beitrag: 10.10.09, 20:56 -
[QUIZ#1] Matthias Reitinger (C++)
Von Matthias Reitinger im Forum ArchivAntworten: 4Letzter Beitrag: 22.09.08, 22:02 -
[QUIZ#1] Matthias Reitinger (Ruby)
Von Matthias Reitinger im Forum ArchivAntworten: 0Letzter Beitrag: 21.09.08, 02:44







Login





