class Object

Constants

Opensea

add convenience alias

PIXEL_OFFSETS

add common

pixel(ate) offsets (for sampling)

Public Instance Methods

convert_images( collection, from: 'jpg', to: 'png' ) click to toggle source
# File lib/artbase/helper.rb, line 14
def convert_images( collection, from: 'jpg',
                                to: 'png' )
    files = Dir.glob( "./#{collection}/i/*.#{from}" )
    puts "==> converting #{files.size} image(s) from #{from} to #{to}"

    files.each_with_index do |file,i|
      dirname   = File.dirname( file )
      extname   = File.extname( file )
      basename  = File.basename( file, extname )

      cmd = "magick convert #{dirname}/#{basename}.#{from} #{dirname}/#{basename}.#{to}"

      puts "   [#{i+1}/#{files.size}] - #{cmd}"
      system( cmd )

      if from == 'gif'
        ## assume multi-images for gif
        ##   save  image-0.png  to  image.png
        path0 = "#{dirname}/#{basename}-0.#{to}"
        path  = "#{dirname}/#{basename}.#{to}"

        puts "   saving #{path0} to #{path}..."

        blob = File.open( path0, 'rb' ) { |f| f.read }
        File.open( path, 'wb' ) { |f| f.write( blob ) }
      end
    end
end
copy_image( src, dest, dump_headers: false ) click to toggle source
# File lib/artbase/helper.rb, line 88
def copy_image( src, dest,
                 dump_headers: false )
  uri = URI.parse( src )

  http = Net::HTTP.new( uri.host, uri.port )

  puts "[debug] GET #{uri.request_uri} uri=#{uri}"

  headers = { 'User-Agent' => "ruby v#{RUBY_VERSION}" }

  request = Net::HTTP::Get.new( uri.request_uri, headers )
  if uri.instance_of? URI::HTTPS
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  response   = http.request( request )

  if response.code == '200'
    puts "#{response.code} #{response.message}"

    content_type   = response.content_type
    content_length = response.content_length
    puts "  content_type: #{content_type}, content_length: #{content_length}"

    if dump_headers   ## for debugging dump headers
      headers = response.each_header.to_h
      puts "htttp respone headers:"
      pp headers
    end


    format = if content_type =~ %r{image/jpeg}i
                'jpg'
             elsif content_type =~ %r{image/png}i
                'png'
              elsif content_type =~ %r{image/gif}i
                'gif'
             else
              puts "!! error:"
              puts " unknown image format content type: >#{content_type}<"
              exit 1
            end

    ##   make sure path exits - autocreate dirs
    ## make sure path exists
    dirname = File.dirname( "#{dest}.#{format}" )
    FileUtils.mkdir_p( dirname )  unless Dir.exist?( dirname )

    File.open( "#{dest}.#{format}", 'wb' ) do |f|
      f.write( response.body )
    end
  else
    puts "!! error:"
    puts "#{response.code} #{response.message}"
    exit 1
  end
end
copy_json( src, dest ) click to toggle source
# File lib/artbase/helper.rb, line 50
def copy_json( src, dest )
  uri = URI.parse( src )

  http = Net::HTTP.new( uri.host, uri.port )

  puts "[debug] GET #{uri.request_uri} uri=#{uri}"

  headers = { 'User-Agent' => "ruby v#{RUBY_VERSION}" }


  request = Net::HTTP::Get.new( uri.request_uri, headers )
  if uri.instance_of? URI::HTTPS
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  response   = http.request( request )

  if response.code == '200'
    puts "#{response.code} #{response.message}"
    puts "  content_type: #{response.content_type}, content_length: #{response.content_length}"

    text = response.body.to_s
    text = text.force_encoding( Encoding::UTF_8 )

    data = JSON.parse( text )

    File.open( dest, "w:utf-8" ) do |f|
      f.write( JSON.pretty_generate( data ) )
    end
  else
    puts "!! error:"
    puts "#{response.code} #{response.message}"
    exit 1
  end
end
counter_to_csv( counter ) click to toggle source
# File lib/artbase/attributes.rb, line 71
def counter_to_csv( counter )

  puts "type, name, count"
  counter.each do |trait_type, h|
    puts "#{trait_type}, ∑ Total, #{h[:count]}"
    h[:by_type].each do |trait_value, count|
       puts "#{trait_type}, #{trait_value}, #{count}"
    end
  end
end
counter_to_text( counter ) click to toggle source
# File lib/artbase/attributes.rb, line 3
def counter_to_text( counter )

  counter = counter.to_a

  attribute_counter = counter[0]
  more_counter      = counter[1..-1]


  puts "Attribute Counts\n"
  trait_type, h = attribute_counter

  total = h[:by_type].values.reduce(0) { |sum,count| sum+count }


  types = h[:by_type]
  types = types.sort { |l,r| l[0]<=>r[0] }  ## sort by name

  puts "\n"
  puts "|Name|Total (%)|"
  puts "|--------|----------:|"

  types.each do |rec|
      name     = rec[0]
      count    = rec[1]
      percent =  Float(count*100)/Float(total)

      puts "| **#{name} Attributes** | #{count} (#{'%.2f' % percent}) |"
  end
  puts "\n"

  more_counter.each_with_index do |(trait_type, h),i|
    print " · "  if i > 0   ## add separator
    print "#{trait_type } (#{h[:by_type].size})"
  end
  puts "\n\n"



  more_counter.each do |trait_type, h|
    print "### #{trait_type } (#{h[:by_type].size}) - "
    print "∑Total #{h[:count]}/#{total}\n"

    puts "\n"
    puts "|Name|Total (%)|"
    puts "|--------|----------:|"

      types = h[:by_type]
      types = types.sort do |l,r|
                            # sort by 1) by count
                            #         2) by name a-z
                                  res = r[1] <=> l[1]
                                  res = l[0] <=> r[0]  if res == 0
                                  res
                         end  ## sort by count
      types.each do |rec|
         name     = rec[0]
         count    = rec[1]
         percent =  Float(count*100)/Float(total)

         puts "| **#{name}** | #{count} (#{'%.2f' % percent}) |"
      end
      puts "\n\n"
    end
end
slugify( name ) click to toggle source
# File lib/artbase/helper.rb, line 4
def slugify( name )
  name.downcase.gsub( /[^a-z0-9 ()$_-]/ ) do |_|
     puts " !! WARN: asciify - found (and removing) non-ascii char >#{Regexp.last_match}<"
     ''  ## remove - use empty string
  end.gsub( ' ', '_')
end