Class: EasyImgUtils
- Inherits:
-
Object
- Object
- EasyImgUtils
- Extended by:
- CommandHelper
- Includes:
- Magick, RXFHelperModule
- Defined in:
- lib/easyimg_utils.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_rectangle(a = [], quality: nil, color: 'green', stroke_width: 5, x1: 0, y1: 0, x2: 0, y2: 0) ⇒ Object
- #add_svg(svg_file) ⇒ Object
- #add_text(s = 'your text goes here', quality: nil) ⇒ Object
- #animate ⇒ Object
-
#best_viewport ⇒ Object
Used where images are perhaps cropped using CSS to a letterbox size image.
- #blur(x: 0, y: 0, w: 80, h: 80, strength: 8, quality: nil) ⇒ Object
- #brightness(quality: nil) ⇒ Object
-
#calc_resize(geometry) ⇒ Object
calculates the new geometry after a resize.
- #capture_screen(quality: nil) ⇒ Object
- #center_crop(w = 0, h = 0, width: w, height: h, quality: nil) ⇒ Object
- #composite(filex = nil, x: 0, y: 0, quality: nil) ⇒ Object (also: #overlay, #add_img)
-
#contrast(level = 5) ⇒ Object
contrast level 1 low -> 10 high.
- #convert(quality: nil) ⇒ Object
- #crop(x: 0, y: 0, w: nil, h: nil, quality: nil) ⇒ Object
- #faces ⇒ Object
- #fax_effect(threshold: 0.55, quality: nil) ⇒ Object
- #greyscale(quality: nil) ⇒ Object (also: #grayscale)
- #info ⇒ Object
-
#initialize(img_in = nil, img_out = nil, out: img_out, working_dir: '/tmp', debug: false) ⇒ EasyImgUtils
constructor
A new instance of EasyImgUtils.
- #make_thumbnail(width = 125, height = 125) ⇒ Object (also: #thumbnail)
-
#resize(raw_geometry = '320x240', quality: nil) ⇒ Object
defines the maximum size of an image while maintaining aspect ratio.
- #rotate(degrees) ⇒ Object
- #rotate_180 ⇒ Object
- #rotate_left ⇒ Object
- #rotate_right ⇒ Object
-
#scale(factor = 0.75, quality: nil) ⇒ Object
Scales an image by the given factor e.g.
-
#screencast(duration: 6, scale: 1, window: true) ⇒ Object
Takes a screenshot every second to create an animated gif.
- #sketch(quality: nil) ⇒ Object (also: #drawing)
- #view(show: true) ⇒ Object
- #vignette(quality: nil) ⇒ Object (also: #feathered_around)
Methods included from CommandHelper
Constructor Details
#initialize(img_in = nil, img_out = nil, out: img_out, working_dir: '/tmp', debug: false) ⇒ EasyImgUtils
Returns a new instance of EasyImgUtils.
87 88 89 90 91 92 93 |
# File 'lib/easyimg_utils.rb', line 87 def initialize(img_in=nil, img_out=nil, out: img_out, working_dir: '/tmp', debug: false) @file_in, @file_out, @working_dir = img_in, out, working_dir @debug = debug end |
Class Method Details
.calc_resize(geometry, new_geometry, force: false) ⇒ Object
e.g. calc_resize ‘1449x1932’, ‘640x480’ #=> 480x640 e.g. calc_resize ‘518x1024’, ‘*518x500’ #=> “518x1024” the asterisk denotes a guaranteed the image will be resized using x or y
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/easyimg_utils.rb', line 99 def self.calc_resize(geometry, new_geometry, force: false) xy = geometry.split('x',2) xy2 = new_geometry.split('x',2) # find any locked geometry which guarantees the resize on either x or y lock = xy2.find {|x| x =~ /^\*/} a = xy.map {|x| x[/\d+/].to_i} a2 = xy2.map {|x| x[/\d+/].to_i} i = lock ? a2.index(lock[1..-1].to_i) : a.index(a.max) factor = a2[i] / a[i].to_f s3 = a.map {|x| (x * factor).round}.join('x') end |
Instance Method Details
#add_rectangle(a = [], quality: nil, color: 'green', stroke_width: 5, x1: 0, y1: 0, x2: 0, y2: 0) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/easyimg_utils.rb', line 118 def add_rectangle(a=[], quality: nil, color: 'green', stroke_width: 5, x1: 0, y1: 0, x2: 0, y2: 0) x1, y1, x2, y2 = *a if a read() do |img| gc = Magick::Draw.new gc.stroke('green') gc.stroke_width(5) gc.fill('transparent') gc.rectangle(x1, y1, x2, y2) gc.draw(img) write img, quality end end |
#add_svg(svg_file) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/easyimg_utils.rb', line 134 def add_svg(svg_file) img = Magick::ImageList.new img.read(@file_in) img.read(svg_file) do self.format = 'SVG' self.background_color = 'transparent' end img.flatten_images.write @file_out end |
#add_text(s = 'your text goes here', quality: nil) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/easyimg_utils.rb', line 148 def add_text(s='your text goes here', quality: nil) read() do |img| d = Draw.new img.annotate(d, 0,0,0,0, s) do d.gravity = Magick::SouthGravity d.pointsize = 26 d.stroke = '#000000' d.fill = '#ffffff' d.font_weight = Magick::BoldWeight end write img, quality end end |
#animate ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/easyimg_utils.rb', line 167 def animate() anim = Magick::ImageList.new read() {|img| anim << img } anim.ticks_per_second = 100 anim.delay = 20 @file_out ? anim.write(@file_out) : anim.animate end |
#best_viewport ⇒ Object
Used where images are perhaps cropped using CSS to a letterbox size image. Works best with portrait mode photos of selfies or natural
landscapes with a lot of sky
Returns the starting y pos as a percentage of the image using face detection and high contrast detection on the y-axis
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/easyimg_utils.rb', line 186 def () percentage = 0 read() do |img| found = faces() index = if found.any? then # find the top y box = found.max_by {|x, y, width, height| y} box[1] else y_maxcontrast(img) end percentage = (100 / (img.rows / index.to_f)).round end return percentage end |
#blur(x: 0, y: 0, w: 80, h: 80, strength: 8, quality: nil) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/easyimg_utils.rb', line 214 def blur(x: 0, y: 0, w: 80, h: 80, strength: 8, quality: nil) width, height = w, h read() do |img| region = img.dispatch(x, y, width, height, 'RGB') face_img = Magick::Image.constitute(width, height, "RGB", region) img.composite!(face_img.gaussian_blur(0, strength), x, y, Magick::OverCompositeOp) write img, quality end end |
#brightness(quality: nil) ⇒ Object
230 231 232 233 234 235 |
# File 'lib/easyimg_utils.rb', line 230 def brightness(quality: nil) read() do |img| img2 = imglevel(-Magick::QuantumRange * 0.25, Magick::QuantumRange * 1.25, 1.0) write img2, quality end end |
#calc_resize(geometry) ⇒ Object
calculates the new geometry after a resize
239 240 241 |
# File 'lib/easyimg_utils.rb', line 239 def calc_resize(geometry) EasyImgUtils.calc_resize(info()[:geometry], geometry) end |
#capture_screen(quality: nil) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/easyimg_utils.rb', line 243 def capture_screen(quality: nil) # defaults (silent=false, frame=false, descend=false, # screen=false, borders=false) img = Magick::Image.capture(true, false, false, true, true) { self.filename = "root" } write img, quality end |
#center_crop(w = 0, h = 0, width: w, height: h, quality: nil) ⇒ Object
255 256 257 258 259 260 261 262 263 264 |
# File 'lib/easyimg_utils.rb', line 255 def center_crop(w=0, h=0, width: w, height: h, quality: nil) return unless w read() do |img| img.crop!(CenterGravity, width, height) write img, quality end end |
#composite(filex = nil, x: 0, y: 0, quality: nil) ⇒ Object Also known as: overlay, add_img
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/easyimg_utils.rb', line 266 def composite(filex=nil, x: 0, y: 0, quality: nil) return unless filex read() do |img| imgx = Magick::ImageList.new(filex) # Change the white pixels in the sign to transparent. imgx = imgx.matte_replace(0,0) img2 = Magick::Draw.new img2.composite(x, y, 0, 0, imgx) img2.draw(img) write img, quality end end |
#contrast(level = 5) ⇒ Object
contrast level 1 low -> 10 high
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/easyimg_utils.rb', line 292 def contrast(level=5) neutral = 5 return if level == neutral read() do |img| n = neutral - level sharpen = n > 0 n.abs.times { img = img.contrast(sharpen) } write img, quality end end |
#convert(quality: nil) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/easyimg_utils.rb', line 309 def convert(quality: nil) if File.extname(@file_in) == '.webp' then # output_format options: pam, ppm, pgm, bmp, tiff or yuv ext = File.extname(@file_out)[1..-1].to_sym puts 'ext: ' + ext.inspect if @debug if ext == :jpg then file_out = @file_out.sub(/\.jpg$/,'.png') WebP.decode(@file_in, file_out, output_format: :png) img = read(file_out) write img, quality else WebP.decode(@file_in, @file_out, output_format: ext) end else img = read() write img, quality end end |
#crop(x: 0, y: 0, w: nil, h: nil, quality: nil) ⇒ Object
336 337 338 339 340 341 342 343 344 345 |
# File 'lib/easyimg_utils.rb', line 336 def crop(x: 0, y: 0, w: nil, h: nil, quality: nil) return unless w read() do |img| img.crop!(x,y, width=w, height=h) write img, quality end end |
#faces ⇒ Object
347 348 349 |
# File 'lib/easyimg_utils.rb', line 347 def faces() DetectFaces.new(@file_in).faces end |
#fax_effect(threshold: 0.55, quality: nil) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/easyimg_utils.rb', line 351 def fax_effect(threshold: 0.55, quality: nil) read() do |img| # Use a threshold of 55% of MaxRGB. img = img.threshold(Magick::MaxRGB*threshold) write img, quality end end |
#greyscale(quality: nil) ⇒ Object Also known as: grayscale
363 364 365 366 367 368 369 370 |
# File 'lib/easyimg_utils.rb', line 363 def greyscale(quality: nil) read() do |img| img2 = img.quantize(256, GRAYColorspace) write img2, quality end end |
#info ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/easyimg_utils.rb', line 374 def info() img = Magick::Image.ping( @file_in ).first { geometry: "%sx%s" % [img.columns, img.rows], mime_type: img.mime_type, format: img.format, quality: img.quality, filesize: img.filesize, filename: img.filename, created: img.properties } end |
#make_thumbnail(width = 125, height = 125) ⇒ Object Also known as: thumbnail
386 387 388 389 390 391 392 393 |
# File 'lib/easyimg_utils.rb', line 386 def make_thumbnail(width=125, height=125) read() do |img| img2 = img.thumbnail(width, height) write img2, quality end end |
#resize(raw_geometry = '320x240', quality: nil) ⇒ Object
defines the maximum size of an image while maintaining aspect ratio
399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/easyimg_utils.rb', line 399 def resize(raw_geometry='320x240', quality: nil) geometry = calc_resize(raw_geometry ) read() do |preview| preview.change_geometry!(geometry) do |cols, rows, img| img.resize!(cols, rows) end write preview, quality end end |
#rotate(degrees) ⇒ Object
414 415 416 417 418 419 420 421 |
# File 'lib/easyimg_utils.rb', line 414 def rotate(degrees) read() do |img| img2 = img.rotate(degrees.to_i) write img2, quality end end |
#rotate_180 ⇒ Object
423 424 425 426 427 428 429 430 |
# File 'lib/easyimg_utils.rb', line 423 def rotate_180() read() do |img| img2 = img.rotate(180) write img2, quality end end |
#rotate_left ⇒ Object
432 433 434 435 436 437 438 439 |
# File 'lib/easyimg_utils.rb', line 432 def rotate_left() read() do |img| img2 = img.rotate(-45) write img2, quality end end |
#rotate_right ⇒ Object
441 442 443 444 445 446 447 448 |
# File 'lib/easyimg_utils.rb', line 441 def rotate_right() read() do |img| img2 = img.rotate(45) write img2, quality end end |
#scale(factor = 0.75, quality: nil) ⇒ Object
Scales an image by the given factor e.g. 0.75 is 75%
452 453 454 455 456 457 458 459 460 461 |
# File 'lib/easyimg_utils.rb', line 452 def scale(factor=0.75, quality: nil) read() do |img| img2 = img.scale(factor) write img2, quality end end |
#screencast(duration: 6, scale: 1, window: true) ⇒ Object
Takes a screenshot every second to create an animated gif
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/easyimg_utils.rb', line 465 def screencast(duration: 6, scale: 1, window: true) fileout = @file_out.sub(/\.\w+$/,'%d.png') puts 'fileout: ' + fileout if @debug x4ss = X4ss.new fileout, mouse: true, window: true mode = window ? :window : :screen sleep 2; x4ss.record duration: duration, mode: mode x4ss.save fileout2 = fileout.sub(/(?=%)/,'b') EasyImgUtils.new(fileout, fileout2).scale(scale) unless scale == 1 EasyImgUtils.new(fileout2, @file_out).animate end |
#sketch(quality: nil) ⇒ Object Also known as: drawing
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/easyimg_utils.rb', line 482 def sketch(quality: nil) read() do |img| # Convert to grayscale sketch = img.quantize(256, Magick::GRAYColorspace) # Apply histogram equalization sketch = sketch.equalize sketch = sketch.sketch(0, 10, 135) img = img.dissolve(sketch, 0.75, 0.25) write img, quality end end |
#view(show: true) ⇒ Object
503 504 505 506 507 508 509 |
# File 'lib/easyimg_utils.rb', line 503 def view(show: true) return unless @file_out command = `feh #{@file_out}` run command, show end |
#vignette(quality: nil) ⇒ Object Also known as: feathered_around
511 512 513 514 515 516 517 518 519 520 |
# File 'lib/easyimg_utils.rb', line 511 def vignette(quality: nil) read() do |img| img2 = img.vignette write img2, quality end end |