| Message: 95769 |
 |
BY: Mike Leonard (mikeleonard) DATE: 2011-01-18 22:29 SUBJECT: RE: Reproducing an ImageMagick command in RMagick Two quick things:
1. I realized that my earlier code was flawed. It only worked because the upper case J in Helvetica extends as high above the baseline as the capital letters. This will get you the right number (defined here as total_height) to use for the height, if you haven't figured it out yet:
characters = "ABCDEFGHIjKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
family = "Helvetica"
size = 50
i = Magick::Image.new(2000,1000)
glyph = Draw.new
glyph.fill('cmyka(0,0,0,99%)')
glyph.stroke('cmyk(0,0,0,99%)')
glyph.opacity(1)
glyph.font_family(family)
glyph.text_antialias(false)
glyph.gravity=Magick::CenterGravity
glyph.pointsize(size)
glyph.text(0,0,characters)
glyph.draw(i)
i.trim!
total_height = i.rows
2. I took a stab at writing a trimx method. It isn't pretty, but it seems to work:
class Image
def trimx
collision = false
x1 = 0
result = self.copy
until collision == true
pixels = self.dispatch(x1, 0, 1, self.rows, "RGBA")
a_column = Image.constitute(1, self.rows, "RGBA", pixels)
#check whether the column contains any nonwhite pixels
white_pixel = Pixel.from_color("white")
h = a_column.color_histogram
h.delete(white_pixel)
if h.length > 0 then collision = true end
x1 += 1
end
result.crop!(x1,0,self.columns-x1, self.rows)
x2 = self.columns
collision = false
until collision == true
pixels = self.dispatch(x2, 0, 1, self.rows, "RGBA")
a_column = Image.constitute(1, self.rows, "RGBA", pixels)
#check whether the column contains any nonwhite pixels
white_pixel = Pixel.from_color("white")
h = a_column.color_histogram
h.delete(white_pixel)
if h.length > 0 then collision = true end
x2 -= 1
end
result.crop!(0,0,x2,self.rows)
return result
end
end | |