-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_pixels.rb.html
72 lines (63 loc) · 2.51 KB
/
get_pixels.rb.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE public PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta name="generator" content="ex2html.rb" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/popup.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script>
hljs.configure({ cssSelector: "pre" });
hljs.highlightAll();
</script>
<title>RMagick example: get_pixels.rb</title>
</head>
<body>
<h1>get_pixels.rb</h1>
<div class="bodybox">
<div class="bodyfloat">
<pre class="language-ruby">
# frozen_string_literal: true
require 'rmagick'
# Demonstrate partial transparency and the get_pixels and
# store_pixels methods by creating an image that goes from
# full-color on the left to monochrome on the right.
# Read the colorful picture of a rock formation. Scale
# it to 300 pixels high because we don't want a big picture.
rocks = Magick::Image.read('images/Red_Rocks.jpg').first
rocks.scale!(250.0 / rocks.rows)
# Make a monochrome copy. See Image#quantize for details
grayrocks = rocks.quantize(256, Magick::GRAYColorspace)
rows = grayrocks.rows
cols = grayrocks.columns
# Create an array of opacity values, proceeding from
# transparent to opaque. The array should have as many
# elements as there are columns in the image. The first
# element should be QuantumRange and each succeeding
# element slightly more opaque than its predecessor.
step = Magick::QuantumRange / cols.to_f
alpha_steps = Array.new(cols)
cols.times do |x|
alpha_steps[x] = Integer(x * step)
alpha_steps[x] = Magick::QuantumRange if alpha_steps[x] > Magick::QuantumRange
end
# Get each row of pixels from the mono image.
# Copy the pre-computed opacity values to the pixels.
# Store the pixels back.
grayrocks.alpha(Magick::ActivateAlphaChannel)
rows.times do |y|
pixels = grayrocks.get_pixels(0, y, cols, 1)
pixels.each_with_index { |p, x| p.alpha = alpha_steps[x] }
grayrocks.store_pixels(0, y, cols, 1, pixels)
end
# Composite the mono version of the image over the color version.
combine = rocks.composite(grayrocks, Magick::CenterGravity, Magick::OverCompositeOp)
# combine.display
combine.write 'get_pixels.jpg'
exit
</pre>
</div>
</div>
<div id="close"><a href="javascript:window.close();">Close window</a></div>
</body>
</html>