Class | Magick::Image |
In: |
lib/RMagick.rb
|
Parent: | Object |
Ruby-level Magick::Image methods
Provide an alternate version of Draw#annotate, for folks who want to find it in this class.
# File lib/RMagick.rb, line 739 739: def annotate(draw, width, height, x, y, text, &block) 740: check_destroyed 741: draw.annotate(self, width, height, x, y, text, &block) 742: self 743: end
Set all pixels that are neighbors of x,y and are not the border color to the fill color
# File lib/RMagick.rb, line 761 761: def color_fill_to_border(x, y, fill) 762: color_flood_fill(border_color, fill, x, y, Magick::FillToBorderMethod) 763: end
Set all pixels that have the same color as the pixel at x,y and are neighbors to the fill color
# File lib/RMagick.rb, line 754 754: def color_floodfill(x, y, fill) 755: target = pixel_color(x, y) 756: color_flood_fill(target, fill, x, y, Magick::FloodfillMethod) 757: end
Set the color at x,y
# File lib/RMagick.rb, line 746 746: def color_point(x, y, fill) 747: f = copy 748: f.pixel_color(x, y, fill) 749: return f 750: end
Set all pixels to the fill color. Very similar to Image#erase! Accepts either String or Pixel arguments
# File lib/RMagick.rb, line 767 767: def color_reset!(fill) 768: save = background_color 769: # Change the background color _outside_ the begin block 770: # so that if this object is frozen the exeception will be 771: # raised before we have to handle it explicitly. 772: self.background_color = fill 773: begin 774: erase! 775: ensure 776: self.background_color = save 777: end 778: self 779: end
Used by ImageList methods - see ImageList#cur_image
# File lib/RMagick.rb, line 782 782: def cur_image 783: self 784: end
Iterate over IPTC record number:dataset tags, yield for each non-nil dataset
# File lib/RMagick.rb, line 844 844: def each_iptc_dataset 845: Magick::IPTC.constants.each do |record| 846: rec = Magick::IPTC.const_get(record) 847: rec.constants.each do |dataset| 848: data_field = get_iptc_dataset(rec.const_get(dataset)) 849: yield(dataset, data_field) unless data_field.nil? 850: end 851: end 852: nil 853: end
Thanks to Russell Norris!
# File lib/RMagick.rb, line 787 787: def each_pixel 788: get_pixels(0, 0, columns, rows).each_with_index do |p, n| 789: yield(p, n%columns, n/columns) 790: end 791: self 792: end
Retrieve EXIF data by entry or all. If one or more entry names specified, return the values associated with the entries. If no entries specified, return all entries and values. The return value is an array of [name,value] arrays.
# File lib/RMagick.rb, line 798 798: def get_exif_by_entry(*entry) 799: ary = Array.new 800: if entry.length == 0 801: exif_data = self['EXIF:*'] 802: if exif_data 803: exif_data.split("\n").each { |exif| ary.push(exif.split('=')) } 804: end 805: else 806: get_exif_by_entry() # ensure properties is populated with exif data 807: entry.each do |name| 808: rval = self["EXIF:#{name}"] 809: ary.push([name, rval]) 810: end 811: end 812: return ary 813: end
Retrieve EXIF data by tag number or all tag/value pairs. The return value is a hash.
# File lib/RMagick.rb, line 816 816: def get_exif_by_number(*tag) 817: hash = Hash.new 818: if tag.length == 0 819: exif_data = self['EXIF:!'] 820: if exif_data 821: exif_data.split("\n").each do |exif| 822: tag, value = exif.split('=') 823: tag = tag[1,4].hex 824: hash[tag] = value 825: end 826: end 827: else 828: get_exif_by_number() # ensure properties is populated with exif data 829: tag.each do |num| 830: rval = self['#%04X' % num.to_i] 831: hash[num] = rval == 'unknown' ? nil : rval 832: end 833: end 834: return hash 835: end
Retrieve IPTC information by record number:dataset tag constant defined in Magick::IPTC, above.
# File lib/RMagick.rb, line 839 839: def get_iptc_dataset(ds) 840: self['IPTC:'+ds] 841: end
(Thanks to Al Evans for the suggestion.)
# File lib/RMagick.rb, line 868 868: def level(black_point=0.0, white_point=nil, gamma=nil) 869: black_point = Float(black_point) 870: 871: white_point ||= Magick::QuantumRange - black_point 872: white_point = Float(white_point) 873: 874: gamma_arg = gamma 875: gamma ||= 1.0 876: gamma = Float(gamma) 877: 878: if gamma.abs > 10.0 || white_point.abs <= 10.0 || white_point.abs < gamma.abs 879: gamma, white_point = white_point, gamma 880: unless gamma_arg 881: white_point = Magick::QuantumRange - black_point 882: end 883: end 884: 885: return level2(black_point, white_point, gamma) 886: end
Make transparent any neighbor pixel that is not the border color.
# File lib/RMagick.rb, line 922 922: def matte_fill_to_border(x, y) 923: f = copy 924: f.opacity = Magick::OpaqueOpacity unless f.matte 925: f.matte_flood_fill(border_color, TransparentOpacity, 926: x, y, FillToBorderMethod) 927: end
Make transparent any pixel that matches the color of the pixel at (x,y) and is a neighbor.
# File lib/RMagick.rb, line 913 913: def matte_floodfill(x, y) 914: f = copy 915: f.opacity = OpaqueOpacity unless f.matte 916: target = f.pixel_color(x, y) 917: f.matte_flood_fill(target, TransparentOpacity, 918: x, y, FloodfillMethod) 919: end
Make the pixel at (x,y) transparent.
# File lib/RMagick.rb, line 893 893: def matte_point(x, y) 894: f = copy 895: f.opacity = OpaqueOpacity unless f.matte 896: pixel = f.pixel_color(x,y) 897: pixel.opacity = TransparentOpacity 898: f.pixel_color(x, y, pixel) 899: return f 900: end
Make transparent all pixels that are the same color as the pixel at (x, y).
# File lib/RMagick.rb, line 904 904: def matte_replace(x, y) 905: f = copy 906: f.opacity = OpaqueOpacity unless f.matte 907: target = f.pixel_color(x, y) 908: f.transparent(target) 909: end
Make all pixels transparent.
# File lib/RMagick.rb, line 930 930: def matte_reset! 931: self.opacity = Magick::TransparentOpacity 932: self 933: end
Corresponds to ImageMagick‘s -resample option
# File lib/RMagick.rb, line 936 936: def resample(x_res=72.0, y_res=nil) 937: y_res ||= x_res 938: width = x_res * columns / x_resolution + 0.5 939: height = y_res * rows / y_resolution + 0.5 940: self.x_resolution = x_res 941: self.y_resolution = y_res 942: resize(width, height) 943: end
Force an image to exact dimensions without changing the aspect ratio. Resize and crop if necessary. (Thanks to Jerett Taylor!)
# File lib/RMagick.rb, line 947 947: def resize_to_fill(ncols, nrows=nil, gravity=CenterGravity) 948: copy.resize_to_fill!(ncols, nrows, gravity) 949: end
# File lib/RMagick.rb, line 951 951: def resize_to_fill!(ncols, nrows=nil, gravity=CenterGravity) 952: nrows ||= ncols 953: if ncols != columns || nrows != rows 954: scale = [ncols/columns.to_f, nrows/rows.to_f].max 955: resize!(scale*columns+0.5, scale*rows+0.5) 956: end 957: crop!(gravity, ncols, nrows, true) if ncols != columns || nrows != rows 958: self 959: end
Convenience method to resize retaining the aspect ratio. (Thanks to Robert Manni!)
# File lib/RMagick.rb, line 967 967: def resize_to_fit(cols, rows=nil) 968: rows ||= cols 969: change_geometry(Geometry.new(cols, rows)) do |ncols, nrows| 970: resize(ncols, nrows) 971: end 972: end
# File lib/RMagick.rb, line 974 974: def resize_to_fit!(cols, rows=nil) 975: rows ||= cols 976: change_geometry(Geometry.new(cols, rows)) do |ncols, nrows| 977: resize!(ncols, nrows) 978: end 979: end
Replace neighboring pixels to border color with texture pixels
# File lib/RMagick.rb, line 988 988: def texture_fill_to_border(x, y, texture) 989: texture_flood_fill(border_color, texture, x, y, FillToBorderMethod) 990: end
Replace matching neighboring pixels with texture pixels
# File lib/RMagick.rb, line 982 982: def texture_floodfill(x, y, texture) 983: target = pixel_color(x, y) 984: texture_flood_fill(target, texture, x, y, FloodfillMethod) 985: end
Construct a view. If a block is present, yield and pass the view object, otherwise return the view object.
# File lib/RMagick.rb, line 994 994: def view(x, y, width, height) 995: view = View.new(self, x, y, width, height) 996: 997: if block_given? 998: begin 999: yield(view) 1000: ensure 1001: view.sync 1002: end 1003: return nil 1004: else 1005: return view 1006: end 1007: end