easyimg_utils 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5a7070bfe90782130922f1707f7143dbd2cbb8465197ff48055650f4fa137a2
4
- data.tar.gz: 1b909e5e43b88e300b1542b44dfdb080c3c45da1f9b10e7da5ad6b3676f72118
3
+ metadata.gz: c09e7c063bbd8a5c1060c74338346317b3432f46e53a0e3cdc6e0d4a64553520
4
+ data.tar.gz: 8d73c424b106e51dea74394557e601b8ea78bfca66fd43bf80a10088527ba726
5
5
  SHA512:
6
- metadata.gz: 3270ace1416ed3cd00b4d2e7ab942fdae8c1509d20d2f60cea12c488060e42b65520c08e5476a13aa603354e4300c7c9202de9b5cf8fc455efc7274f5b59b2f3
7
- data.tar.gz: de164b42b68ca9708a8d712ee9b28e1bbdddab87d73357d254e41c883be698274bbb2cfbf8154f36c05e0cf9c88414f39ea825b1725754c43444073d6bb54aea
6
+ metadata.gz: 602af329751d96be2d89a4b6487d35d9c1920a4e56fb8d5330014e8e924b0edb44db1f74bd926224bcf3ee62435cba1b3b209ccea1f2301b0618a299534702b9
7
+ data.tar.gz: abd50555f42ad809647180e58325578951b653f545fc7411c8b58e3132625ff90e82666509d1933ecf75d48409437b27dad9718fa60a4183c4d0174bc85e0fa5
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/easyimg_utils.rb CHANGED
@@ -57,6 +57,7 @@ class EasyImgUtils
57
57
  * add_svg # adds an SVG transparency overlay. usage: add_svg('/tmp/image1.svg')
58
58
  * add_text # e.g. add_text('some text')
59
59
  * animate Creates an animated gif e.g. animate('/tmp/a%d.png', '/tmp/b.gif')
60
+ * best_viewport # returns the best viewing region on the y-axis
60
61
  * blur # e.g. blur(x: 231, y: 123, w: 85, h: 85)
61
62
  * capture_screen # takes a screenshot of the desktop
62
63
  * calc_resize # e.g. calc_resize '640x480' #=> 640x491
@@ -175,6 +176,41 @@ class EasyImgUtils
175
176
 
176
177
  end
177
178
 
179
+ # Used where images are perhaps cropped using CSS to a letterbox size image.
180
+ # Works best with portrait mode photos of selfies or natural
181
+ # landscapes with a lot of sky
182
+ #
183
+ # Returns the starting y pos as a percentage of the image using face
184
+ # detection and high contrast detection on the y-axis
185
+ #
186
+ def best_viewport()
187
+
188
+ percentage = 0
189
+
190
+ read() do |img|
191
+
192
+ found = faces()
193
+
194
+ index = if found.any? then
195
+
196
+ # find the top y
197
+ box = found.max_by {|x, y, width, height| y}
198
+ box[1]
199
+
200
+ else
201
+
202
+ y_maxcontrast(img)
203
+
204
+ end
205
+
206
+ percentage = (100 / (img.rows / index.to_f)).round
207
+
208
+ end
209
+
210
+ return percentage
211
+
212
+ end
213
+
178
214
  def blur(x: 0, y: 0, w: 80, h: 80, strength: 8, quality: nil)
179
215
 
180
216
  width, height = w, h
@@ -483,7 +519,7 @@ class EasyImgUtils
483
519
 
484
520
  end
485
521
 
486
- alias feathered_around vignette
522
+ alias feathered_around vignette
487
523
 
488
524
  private
489
525
 
@@ -553,5 +589,31 @@ class EasyImgUtils
553
589
 
554
590
  end
555
591
  end
592
+
593
+ # returns the y index of the pixel containing the most contrast using the
594
+ # y-axis center of the image
595
+ #
596
+ def y_maxcontrast(img)
597
+
598
+ rows, cols = img.rows, img.columns
599
+ center = (cols / 2).round
600
+ pixels = img.get_pixels(center,0,1,rows)
601
+
602
+ rgb = []
603
+ px = pixels[0]
604
+ rgb = [px.red, px.green, px.blue].map { |v| 255*(v/65535.0) }
556
605
 
606
+ a = pixels[1..-1].map do |pixel,i|
607
+
608
+ c = [pixel.red, pixel.green, pixel.blue].map { |v| 255*(v/65535.0) }
609
+ rgb[0] - c[0]
610
+ rgb.map.with_index {|x,i| (x - c[i]).abs.to_i}
611
+
612
+ end
613
+
614
+ a2 = a.map(&:sum)
615
+ a2.index(a2.max) + 1
616
+
617
+ end
618
+
557
619
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easyimg_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file