class MaskedApeClub::Load

Attributes

collection[R]
gateway[R]
image[R]

Public Class Methods

new( gateway: 'https://ipfs.io/ipfs/' ) click to toggle source
# 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

Public Instance Methods

background( blob:, gravity: Magick::CenterGravity, offset_x: 0, offset_y: 0, width: 631, height: 631) click to toggle source
# 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' ) click to toggle source
# 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 ) click to toggle source
# 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 ) click to toggle source
# 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%" ) click to toggle source
# 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: ) click to toggle source
# File lib/masked_ape_club.rb, line 51
def write( path: )
    @image.write( path )
    return true
end