Class: MaskedApeClub::Load

Inherits:
Object
  • Object
show all
Defined in:
lib/masked_ape_club.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateway: 'https://ipfs.io/ipfs/') ⇒ Load

Returns a new instance of Load.



22
23
24
25
26
27
28
# File 'lib/masked_ape_club.rb', line 22

def initialize( gateway: 'https://ipfs.io/ipfs/' )
    @gateway = gateway
    # puts ">> #{@gateway}"
    @collection = Collection.load( @gateway )
    @image = nil
    return true
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



19
20
21
# File 'lib/masked_ape_club.rb', line 19

def collection
  @collection
end

#gatewayObject (readonly)

Returns the value of attribute gateway.



19
20
21
# File 'lib/masked_ape_club.rb', line 19

def gateway
  @gateway
end

#imageObject (readonly)

Returns the value of attribute image.



19
20
21
# File 'lib/masked_ape_club.rb', line 19

def image
  @image
end

Instance Method Details

#background(blob:, gravity: Magick::CenterGravity, offset_x: 0, offset_y: 0, width: 631, height: 631) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/masked_ape_club.rb', line 123

def background( blob:, gravity: Magick::CenterGravity, offset_x: 0, offset_y: 0, width: 631, height: 631)
    background = Magick::Image.from_blob( blob ).first
    background.format = 'PNG'
    background.crop_resized!( 631, 631, gravity )

    @image.resize_to_fit!( width, height )
    @image = background.composite( @image, offset_x, offset_y, Magick::OverCompositeOp )

    return true
end

#censored(upper_left_x: 240, upper_left_y: 210, lower_right_x: 460, lower_right_y: 270, color: 'black') ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/masked_ape_club.rb', line 113

def censored( upper_left_x: 240, upper_left_y: 210, lower_right_x: 460, lower_right_y: 270, color: 'black' )
    censored = Magick::Draw.new
    censored.fill( color )
    censored.rectangle( upper_left_x, upper_left_y, lower_right_x, lower_right_y )
    censored.draw( @image )
    
    return true
end

#load(id: 1) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/masked_ape_club.rb', line 38

def load( id: 1 )
    token = @collection
        .find { | a | a['token_id'].to_i == id }

    url = token['metadata']['image']
    uri = URI( url )
    blob = Net::HTTP.get( uri )
    @image = Magick::Image.from_blob( blob ).first

    return true
end

#mask(fuzz: '1%', position: 1500, size: 2) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/masked_ape_club.rb', line 57

def mask( fuzz: '1%', position: 1500, size: 2 )
    image = @image
    image.format = 'PNG'
    image.fuzz = fuzz
    
    pixels = []
    image
        .each_pixel { | pixel, c, r | pixels.push( pixel ) }
    
    background_color = pixels[ position ]
        .to_color( Magick::AllCompliance, false, 8, true )
    
    mask = Magick::Image
        .new( image.columns, image.rows) { self.background_color = 'transparent'  }
    

    size = 2
    cols = 631
    rows = 631
    
    img = Magick::Image.new( 631, 631) { self.background_color = 'transparent' }
    
    [ 
        [ 0, 0, size, 0, 0, size, 0, 0 ],
        [ cols, 0, cols-size, 0, cols, size, cols, 0 ],
        [ 0, rows, 0, rows-size, size, rows, 0, rows ],
        [ cols, rows, cols, rows-size, cols-size, rows, 0, rows ]
    ].each { | a |
        triangle = Magick::Draw.new
        triangle.fill( background_color )
        triangle.stroke('transparent')
        triangle.polygon( a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ], a[ 4 ], a[ 5 ], a[ 6 ], a[ 7 ] )
        triangle.draw( img ) 
    }

    w = 2
    [ 
        [ 0, 0, w, rows ],
        [ 0, 0, cols, w ],
        [ cols-w, 0, cols, cols ],
        [ 0, rows, cols, rows-w ]
    ].each { | a |
        gc = Magick::Draw.new
        gc.fill( background_color )
        gc.rectangle( a[ 0 ], a[ 1 ], a[ 2 ], a[ 3 ] )
        gc.draw( img )
    }

    @image = image
        .composite( img, 0, 0, Magick::OverCompositeOp)
        .paint_transparent( background_color )
    
    return @image
end

#mask_ape(id: 1, fuzz: "1%") ⇒ Object



31
32
33
34
35
# File 'lib/masked_ape_club.rb', line 31

def mask_ape( id: 1, fuzz: "1%" )
    blob = self.load( id: id )
    image = self.mask( fuzz: fuzz )
    return image
end

#write(path:) ⇒ Object



51
52
53
54
# File 'lib/masked_ape_club.rb', line 51

def write( path: )
    @image.write( path )
    return true
end