Class | Magick::Image |
In: |
lib/RMagick.rb
|
Parent: | Object |
Ruby-level Magick::Image methods
remap | -> | affinity |
Provide an alternate version of Draw#annotate, for folks who want to find it in this class.
# File lib/RMagick.rb, line 765 765: def annotate(draw, width, height, x, y, text, &block) 766: check_destroyed 767: draw.annotate(self, width, height, x, y, text, &block) 768: self 769: 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 787 787: def color_fill_to_border(x, y, fill) 788: color_flood_fill(border_color, fill, x, y, Magick::FillToBorderMethod) 789: 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 780 780: def color_floodfill(x, y, fill) 781: target = pixel_color(x, y) 782: color_flood_fill(target, fill, x, y, Magick::FloodfillMethod) 783: end
Set the color at x,y
# File lib/RMagick.rb, line 772 772: def color_point(x, y, fill) 773: f = copy 774: f.pixel_color(x, y, fill) 775: return f 776: end
Set all pixels to the fill color. Very similar to Image#erase! Accepts either String or Pixel arguments
# File lib/RMagick.rb, line 793 793: def color_reset!(fill) 794: save = background_color 795: # Change the background color _outside_ the begin block 796: # so that if this object is frozen the exeception will be 797: # raised before we have to handle it explicitly. 798: self.background_color = fill 799: begin 800: erase! 801: ensure 802: self.background_color = save 803: end 804: self 805: end
Used by ImageList methods - see ImageList#cur_image
# File lib/RMagick.rb, line 808 808: def cur_image 809: self 810: end
Iterate over IPTC record number:dataset tags, yield for each non-nil dataset
# File lib/RMagick.rb, line 870 870: def each_iptc_dataset 871: Magick::IPTC.constants.each do |record| 872: rec = Magick::IPTC.const_get(record) 873: rec.constants.each do |dataset| 874: data_field = get_iptc_dataset(rec.const_get(dataset)) 875: yield(dataset, data_field) unless data_field.nil? 876: end 877: end 878: nil 879: end
Thanks to Russell Norris!
# File lib/RMagick.rb, line 813 813: def each_pixel 814: get_pixels(0, 0, columns, rows).each_with_index do |p, n| 815: yield(p, n%columns, n/columns) 816: end 817: self 818: 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 824 824: def get_exif_by_entry(*entry) 825: ary = Array.new 826: if entry.length == 0 827: exif_data = self['EXIF:*'] 828: if exif_data 829: exif_data.split("\n").each { |exif| ary.push(exif.split('=')) } 830: end 831: else 832: get_exif_by_entry() # ensure properties is populated with exif data 833: entry.each do |name| 834: rval = self["EXIF:#{name}"] 835: ary.push([name, rval]) 836: end 837: end 838: return ary 839: end
Retrieve EXIF data by tag number or all tag/value pairs. The return value is a hash.
# File lib/RMagick.rb, line 842 842: def get_exif_by_number(*tag) 843: hash = Hash.new 844: if tag.length == 0 845: exif_data = self['EXIF:!'] 846: if exif_data 847: exif_data.split("\n").each do |exif| 848: tag, value = exif.split('=') 849: tag = tag[1,4].hex 850: hash[tag] = value 851: end 852: end 853: else 854: get_exif_by_number() # ensure properties is populated with exif data 855: tag.each do |num| 856: rval = self['#%04X' % num.to_i] 857: hash[num] = rval == 'unknown' ? nil : rval 858: end 859: end 860: return hash 861: end
Retrieve IPTC information by record number:dataset tag constant defined in Magick::IPTC, above.
# File lib/RMagick.rb, line 865 865: def get_iptc_dataset(ds) 866: self['IPTC:'+ds] 867: end
(Thanks to Al Evans for the suggestion.)
# File lib/RMagick.rb, line 894 894: def level(black_point=0.0, white_point=nil, gamma=nil) 895: black_point = Float(black_point) 896: 897: white_point ||= Magick::QuantumRange - black_point 898: white_point = Float(white_point) 899: 900: gamma_arg = gamma 901: gamma ||= 1.0 902: gamma = Float(gamma) 903: 904: if gamma.abs > 10.0 || white_point.abs <= 10.0 || white_point.abs < gamma.abs 905: gamma, white_point = white_point, gamma 906: unless gamma_arg 907: white_point = Magick::QuantumRange - black_point 908: end 909: end 910: 911: return level2(black_point, white_point, gamma) 912: end
Make transparent any neighbor pixel that is not the border color.
# File lib/RMagick.rb, line 948 948: def matte_fill_to_border(x, y) 949: f = copy 950: f.opacity = Magick::OpaqueOpacity unless f.matte 951: f.matte_flood_fill(border_color, TransparentOpacity, 952: x, y, FillToBorderMethod) 953: end
Make transparent any pixel that matches the color of the pixel at (x,y) and is a neighbor.
# File lib/RMagick.rb, line 939 939: def matte_floodfill(x, y) 940: f = copy 941: f.opacity = OpaqueOpacity unless f.matte 942: target = f.pixel_color(x, y) 943: f.matte_flood_fill(target, TransparentOpacity, 944: x, y, FloodfillMethod) 945: end
Make the pixel at (x,y) transparent.
# File lib/RMagick.rb, line 919 919: def matte_point(x, y) 920: f = copy 921: f.opacity = OpaqueOpacity unless f.matte 922: pixel = f.pixel_color(x,y) 923: pixel.opacity = TransparentOpacity 924: f.pixel_color(x, y, pixel) 925: return f 926: end
Make transparent all pixels that are the same color as the pixel at (x, y).
# File lib/RMagick.rb, line 930 930: def matte_replace(x, y) 931: f = copy 932: f.opacity = OpaqueOpacity unless f.matte 933: target = f.pixel_color(x, y) 934: f.transparent(target) 935: end
Make all pixels transparent.
# File lib/RMagick.rb, line 956 956: def matte_reset! 957: self.opacity = Magick::TransparentOpacity 958: self 959: end
Corresponds to ImageMagick‘s -resample option
# File lib/RMagick.rb, line 962 962: def resample(x_res=72.0, y_res=nil) 963: y_res ||= x_res 964: width = x_res * columns / x_resolution + 0.5 965: height = y_res * rows / y_resolution + 0.5 966: self.x_resolution = x_res 967: self.y_resolution = y_res 968: resize(width, height) 969: 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 973 973: def resize_to_fill(ncols, nrows=nil, gravity=CenterGravity) 974: copy.resize_to_fill!(ncols, nrows, gravity) 975: end
# File lib/RMagick.rb, line 977 977: def resize_to_fill!(ncols, nrows=nil, gravity=CenterGravity) 978: nrows ||= ncols 979: if ncols != columns || nrows != rows 980: scale = [ncols/columns.to_f, nrows/rows.to_f].max 981: resize!(scale*columns+0.5, scale*rows+0.5) 982: end 983: crop!(gravity, ncols, nrows, true) if ncols != columns || nrows != rows 984: self 985: end
Convenience method to resize retaining the aspect ratio. (Thanks to Robert Manni!)
# File lib/RMagick.rb, line 993 993: def resize_to_fit(cols, rows=nil) 994: rows ||= cols 995: change_geometry(Geometry.new(cols, rows)) do |ncols, nrows| 996: resize(ncols, nrows) 997: end 998: end
# File lib/RMagick.rb, line 1000 1000: def resize_to_fit!(cols, rows=nil) 1001: rows ||= cols 1002: change_geometry(Geometry.new(cols, rows)) do |ncols, nrows| 1003: resize!(ncols, nrows) 1004: end 1005: end
Replace neighboring pixels to border color with texture pixels
# File lib/RMagick.rb, line 1014 1014: def texture_fill_to_border(x, y, texture) 1015: texture_flood_fill(border_color, texture, x, y, FillToBorderMethod) 1016: end
Replace matching neighboring pixels with texture pixels
# File lib/RMagick.rb, line 1008 1008: def texture_floodfill(x, y, texture) 1009: target = pixel_color(x, y) 1010: texture_flood_fill(target, texture, x, y, FloodfillMethod) 1011: 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 1020 1020: def view(x, y, width, height) 1021: view = View.new(self, x, y, width, height) 1022: 1023: if block_given? 1024: begin 1025: yield(view) 1026: ensure 1027: view.sync 1028: end 1029: return nil 1030: else 1031: return view 1032: end 1033: end