Skip to content

Instantly share code, notes, and snippets.

@Daltonic
Last active September 29, 2022 18:47
Preprocessing IPFS NFT Images and Metadata
const fs = require('fs')
const path = require('path')
const sharp = require('sharp')
const { faker } = require('@faker-js/faker')
const input = './arts'
const output = './outputs'
let img_counter = 1
const imgSize = { width: 500, height: 500 }
const desired_ext = '.webp'
const base_url = 'https://ipfs.io/ipfs/REPLACE_WITH_IPFS_CID/'
const attributes = {
weapon: [
'Stick',
'Knife',
'Blade',
'Clube',
'Ax',
'Sword',
'Spear',
'Gun',
'Craft',
],
environment: [
'Space',
'Sky',
'Desert',
'Forest',
'Grassland',
'Moiuntains',
'Oceans',
'Rainforest',
],
rarity: Array.from(Array(10).keys()),
}
fs.readdirSync(input).forEach((file) => {
const orginal_ext = path.extname(file)
const orginal_file_name = path.basename(file).split('.')[0]
if (['.jpg', '.jpeg', '.png', '.gif', '.webp'].includes(orginal_ext)) {
const id = img_counter
const metadata = {
id,
name: `Adulam NFT #${id}`,
description:
'A.I Arts NFTs Collection, Mint and collect the hottest NFTs around.',
price: 1,
image: base_url + id + desired_ext,
demand: faker.random.numeric({ min: 10, max: 100 }),
attributes: [
{
trait_type: 'Environment',
value: attributes.environment.sort(() => 0.5 - Math.random())[0],
},
{
trait_type: 'Weapon',
value: attributes.weapon.sort(() => 0.5 - Math.random())[0],
},
{
trait_type: 'Rarity',
value: attributes.rarity.sort(() => 0.5 - Math.random())[0],
max_value: 10,
},
{
display_type: 'date',
trait_type: 'Created',
value: Date.now(),
},
{
display_type: 'number',
trait_type: 'generation',
value: 1,
},
],
}
if (fs.existsSync(`${input}/${orginal_file_name + orginal_ext}`)) {
sharp(`${input}/${orginal_file_name + orginal_ext}`)
.resize(imgSize.height, imgSize.width)
.toFile(`${output}/images/${id + desired_ext}`, (err, info) =>
console.log(err),
)
fs.writeFileSync(`${output}/metadata/${id}.json`, JSON.stringify(metadata), {
encoding: 'utf-8',
flag: 'w',
})
}
console.log(metadata)
img_counter++
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment