Class: TokenCollection
- Inherits:
-
Artbase::Base
- Object
- Artbase::Base
- TokenCollection
- Defined in:
- lib/artbase/collection/token.rb
Defined Under Namespace
Classes: Meta
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Instance Method Summary collapse
-
#_parse_dimension(str) ⇒ Object
e.g.
-
#_range(offset: 0) ⇒ Object
return “default” range - make “private” helper public - why? why not?.
-
#download_images(range = _range, force: false, direct: @image_base ? true : false) ⇒ Object
note: default to direct true if image_base present/availabe otherwise to false todo/check: change/rename force para to overwrite - why? why not?.
- #download_meta(range = _range, force: false) ⇒ Object
- #each_image(range = _range, exclude: true, &blk) ⇒ Object
- #each_meta(range = _range, exclude: true, &blk) ⇒ Object
- #image_url(id:, direct: @image_base ? true : false) ⇒ Object
-
#initialize(slug, count, token_base:, image_base: nil, image_base_id_format: nil, format:, source:, top_x: 0, top_y: 0, center_x: true, center_y: true, excludes: [], offset: 0) ⇒ TokenCollection
constructor
A new instance of TokenCollection.
- #meta_url(id:) ⇒ Object (also: #token_url)
- #pixelate(range = _range, exclude: true, force: false, debug: false, zoom: nil, faster: false) ⇒ Object
Methods inherited from Artbase::Base
#_normalize_trait_type, #_normalize_trait_value, #calc_attribute_counters, #convert_images, #dump_attributes, #export_attributes, #make_composite, #make_strip
Constructor Details
#initialize(slug, count, token_base:, image_base: nil, image_base_id_format: nil, format:, source:, top_x: 0, top_y: 0, center_x: true, center_y: true, excludes: [], offset: 0) ⇒ TokenCollection
Returns a new instance of TokenCollection.
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 111 112 113 114 115 116 117 118 |
# File 'lib/artbase/collection/token.rb', line 83 def initialize( slug, count, token_base:, image_base: nil, image_base_id_format: nil, format:, source:, top_x: 0, top_y: 0, center_x: true, center_y: true, excludes: [], offset: 0 ) # check: rename count to items or such - why? why not? @slug = slug @count = count @offset = offset ## starting by default at 0 (NOT 1 or such) @token_base = token_base @image_base = image_base @image_base_id_format = image_base_id_format @width, @height = _parse_dimension( format ) ## note: allow multiple source formats / dimensions ### e.g. convert 512x512 into [ [512,512] ] ## source = [source] unless source.is_a?( Array ) @sources = source.map { |dimension| _parse_dimension( dimension ) } @top_x = top_x ## more (down)sampling / pixelate options @top_y = top_y @center_x = center_x @center_y = center_y @excludes = excludes end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
81 82 83 |
# File 'lib/artbase/collection/token.rb', line 81 def count @count end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
81 82 83 |
# File 'lib/artbase/collection/token.rb', line 81 def slug @slug end |
Instance Method Details
#_parse_dimension(str) ⇒ Object
e.g. convert dimension (width x height) “24x24” or “24 x 24” to [24,24]
122 123 124 |
# File 'lib/artbase/collection/token.rb', line 122 def _parse_dimension( str ) str.split( /x/i ).map { |str| str.strip.to_i } end |
#_range(offset: 0) ⇒ Object
return “default” range - make “private” helper public - why? why not?
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/artbase/collection/token.rb', line 127 def _range( offset: 0 ) ## return "default" range - make "private" helper public - why? why not? ## note: range uses three dots (...) exclusive (NOT inclusive) range ## e.g. 0...100 => [0,..,99] ## 1...101 => [1,..,100] ## ## note: allow offset argument ## (to start with different offset - note: in addition to builtin 0/1 offset) (0+@offset+offset...@count+@offset) end |
#download_images(range = _range, force: false, direct: @image_base ? true : false) ⇒ Object
note: default to direct true if image_base present/availabe
otherwise to false
todo/check: change/rename force para to overwrite - why? why not?
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/artbase/collection/token.rb', line 359 def download_images( range=_range, force: false, direct: @image_base ? true : false ) start = Time.now delay_in_s = 0.3 range.each do |id| ## note: skip if (downloaded) file already exists skip = false if !force ['png', 'gif', 'jgp', 'svg'].each do |format| if File.exist?( "./#{@slug}/token-i/#{id}.#{format}" ) skip = true break end end end next if skip image_src = image_url( id: id, direct: direct ) ## note: will auto-add format file extension (e.g. .png, .jpg) ## depending on http content type!!!!! start_copy = Time.now copy_image( image_src, "./#{@slug}/token-i/#{id}" ) stop = Time.now diff = stop - start_copy puts " download image in #{diff} sec(s)" diff = stop - start mins = diff / 60 ## todo - use floor or such? secs = diff % 60 puts "up #{mins} mins #{secs} secs (total #{diff} secs)" puts "sleeping #{delay_in_s}s..." sleep( delay_in_s ) end end |
#download_meta(range = _range, force: false) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/artbase/collection/token.rb', line 324 def ( range=_range, force: false ) start = Time.now delay_in_s = 0.3 range.each do |id| outpath = "./#{@slug}/token/#{id}.json" if !force && File.exist?( outpath ) next ## note: skip if file already exists end dirname = File.dirname( outpath ) FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname ) puts "==> #{id} - #{@slug}..." token_src = ( id: id ) copy_json( token_src, outpath ) stop = Time.now diff = stop - start puts " download token metadata in #{diff} sec(s)" mins = diff / 60 ## todo - use floor or such? secs = diff % 60 puts "up #{mins} mins #{secs} secs (total #{diff} secs)" puts "sleeping #{delay_in_s}s..." sleep( delay_in_s ) end end |
#each_image(range = _range, exclude: true, &blk) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/artbase/collection/token.rb', line 140 def each_image( range=_range, exclude: true, &blk ) range.each do |id| #### # filter out/skip # if exclude && @excludes.include?( id ) # puts " skipping / exclude #{id}..." # next # end puts "==> #{id}" img = Image.read( "./#{@slug}/#{@width}x#{@height}/#{id}.png" ) blk.call( img, id ) end end |
#each_meta(range = _range, exclude: true, &blk) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/artbase/collection/token.rb', line 157 def ( range=_range, exclude: true, &blk ) range.each do |id| ## check: change/rename id to index - why? why not? = Meta.read( "./#{@slug}/token/#{id}.json" ) #### # filter out/skip # if exclude && @excludes.include?( meta.name ) # puts " skipping / exclude #{id} >#{meta.name}<..." # next # end blk.call( , id ) end end |
#image_url(id:, direct: @image_base ? true : false) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/artbase/collection/token.rb', line 291 def image_url( id:, direct: @image_base ? true : false ) src = if direct && @image_base ### ## todo/fix: ## change image_base_id_format ## to image_base proc with para id and call proc!!!! if @image_base_id_format @image_base.gsub( '{id}', @image_base_id_format % id ) else @image_base.gsub( '{id}', id.to_s ) end else ## todo/check - change/rename data to meta - why? why not? data = Meta.read( "./#{@slug}/token/#{id}.json" ) = data.name = data.image puts "==> #{id} - #{@slug}..." puts " name: #{}" puts " image: #{}" end src ## quick ipfs (interplanetary file system) hack - make more reusabele!!! src = handle_ipfs( src ) src end |
#meta_url(id:) ⇒ Object Also known as: token_url
281 282 283 284 285 286 287 |
# File 'lib/artbase/collection/token.rb', line 281 def ( id: ) src = @token_base.gsub( '{id}', id.to_s ) ## quick ipfs (interplanetary file system) hack - make more reusabele!!! src = handle_ipfs( src ) src end |
#pixelate(range = _range, exclude: true, force: false, debug: false, zoom: nil, faster: false) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/artbase/collection/token.rb', line 177 def pixelate( range=_range, exclude: true, force: false, debug: false, zoom: nil, faster: false ) range.each do |id| if exclude && @excludes.include?( id ) puts " skipping #{id}; listed in excludes #{@excludes.inspect}" next end outpath = "./#{@slug}/#{@width}x#{@height}/#{id}.png" if !force && File.exist?( outpath ) next ## note: skip if file already exists end center_x = if @center_x.is_a?( Proc ) then @center_x.call( id ); else @center_x; end center_y = if @center_y.is_a?( Proc ) then @center_y.call( id ); else @center_y; end puts "==> #{id} - reading / decoding #{id} ..." if faster ## note: faster for now only supports ## single /one source format ## always will use first source format from array for now cmd = "./pixelator " cmd << "./#{@slug}/token-i/#{id}.png" cmd << " " + @sources[0][0].to_s cmd << " " + @sources[0][1].to_s cmd << " " + outpath cmd << " " + @width.to_s cmd << " " + @height.to_s puts "==> #{cmd}..." ret = system( cmd ) if ret puts "OK" else puts "!! FAIL" if ret.nil? puts " command not found" else puts " exit code: #{$?}" end end else start = Time.now img = Image.read( "./#{@slug}/token-i/#{id}.png" ) stop = Time.now diff = stop - start puts " in #{diff} sec(s)\n" source = nil @sources.each do |source_width, source_height| if img.width == source_width && img.height == source_height source = [source_width, source_height] break end end if source source_width = source[0] source_height = source[1] steps_x = Image.calc_sample_steps( source_width-@top_x, @width, center: center_x ) steps_y = Image.calc_sample_steps( source_height-@top_y, @height, center: center_y ) pix = if debug img.pixelate_debug( steps_x, steps_y, top_x: @top_x, top_y: @top_y ) else img.pixelate( steps_x, steps_y, top_x: @top_x, top_y: @top_y ) end ## todo/check: keep usingu slug e.g. 0001.png or "plain" 1.png - why? why not? ## slug = "%04d" % id pix.save( outpath ) if zoom outpath = "./#{@slug}/#{@width}x#{@height}/#{id}@#{zoom}x.png" pix.zoom( zoom ).save( outpath ) end else puts "!! ERROR - unknown/unsupported dimension - #{img.width}x#{img.height}; sorry - tried:" pp @sources exit 1 end end end end |