separate the nigbot generation and normal avatar generation
This commit is contained in:
		
							
								
								
									
										125
									
								
								build-avatars.js
									
									
									
									
									
								
							
							
						
						
									
										125
									
								
								build-avatars.js
									
									
									
									
									
								
							| @ -109,6 +109,73 @@ function generateLegacyAvatars () { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // // 2024 avatars generation | ||||||
|  | // async function generateAvatars (part) { | ||||||
|  | //   console.log('Starting generating ' + part) | ||||||
|  | //   const parts = avatarPartsDef[part] | ||||||
|  | //   if (!parts) { | ||||||
|  | //     throw new Error('Missing part\'s conf: ' + part) | ||||||
|  | //   } | ||||||
|  |  | ||||||
|  | //   const inputDir = path.join('./assets/images/avatars/', part) | ||||||
|  | //   const outputDir = path.join('./dist/server/avatars/', part) | ||||||
|  | //   fs.mkdirSync(outputDir, { recursive: true }) | ||||||
|  |  | ||||||
|  | //   function computeFilename (part, count) { | ||||||
|  | //     let a = (1 + (count % parts[part])).toString() | ||||||
|  | //     if (a.length < 2) { a = '0' + a} | ||||||
|  |  | ||||||
|  | //     return path.join( | ||||||
|  | //       inputDir, | ||||||
|  | //       part + '_' + a + '.png' | ||||||
|  | //     ) | ||||||
|  | //   } | ||||||
|  | //   // We can't generate all combinations! It would make 400 000 000 files! | ||||||
|  | //   // So we arbitrary pick some combinations, using some modulus | ||||||
|  | //   const nb = 200 // number of avatars to generate | ||||||
|  |  | ||||||
|  | //   for (let i = 0; i < nb; i++) { | ||||||
|  | //     const ouputFile = path.join( | ||||||
|  | //       outputDir, | ||||||
|  | //       i.toString() + '.png' | ||||||
|  | //     ) | ||||||
|  | //     if (await fs.existsSync(ouputFile)) { | ||||||
|  | //       console.log(`Skipping ${ouputFile}, file already exists`) | ||||||
|  | //       continue | ||||||
|  | //     } | ||||||
|  |  | ||||||
|  | //     const partsToCombine = Object.keys(parts) | ||||||
|  | //     const firstPart = partsToCombine.shift() | ||||||
|  | //     const firstFile = computeFilename(firstPart, i) | ||||||
|  |  | ||||||
|  | //     // We just have to combinate different parts into one file, then output at the wanted size. | ||||||
|  | //     const composites = [] | ||||||
|  | //     let j = 0 | ||||||
|  | //     for (const part of partsToCombine) { | ||||||
|  | //       j++ // introduce an offset so we don't get all empty parts at the same time | ||||||
|  | //       composites.push({ | ||||||
|  | //         input: computeFilename(part, i + (j * 7)) | ||||||
|  | //       }) | ||||||
|  | //     } | ||||||
|  |  | ||||||
|  | //     const buff = await sharp(firstFile) | ||||||
|  | //       .composite(composites) | ||||||
|  | //       .toBuffer() | ||||||
|  |  | ||||||
|  | //     await sharp(buff) | ||||||
|  | //       .resize(60, 60) | ||||||
|  | //       .png({ | ||||||
|  | //         compressionLevel: 9, | ||||||
|  | //         palette: true | ||||||
|  | //       }) | ||||||
|  | //       .toFile(ouputFile) | ||||||
|  | //   } | ||||||
|  | // } | ||||||
|  |  | ||||||
|  | const fs = require('fs') | ||||||
|  | const path = require('path') | ||||||
|  | const sharp = require('sharp') | ||||||
|  |  | ||||||
| // 2024 avatars generation | // 2024 avatars generation | ||||||
| async function generateAvatars (part) { | async function generateAvatars (part) { | ||||||
|   console.log('Starting generating ' + part) |   console.log('Starting generating ' + part) | ||||||
| @ -130,10 +197,37 @@ async function generateAvatars (part) { | |||||||
|       part + '_' + a + '.png' |       part + '_' + a + '.png' | ||||||
|     ) |     ) | ||||||
|   } |   } | ||||||
|   // We can't generate all combinations! It would make 400 000 000 files! |  | ||||||
|   // So we arbitrary pick some combinations, using some modulus |  | ||||||
|   const nb = 200 // number of avatars to generate |  | ||||||
|    |    | ||||||
|  |   // Function to generate "nigbot" files without composites | ||||||
|  |   async function generateNigbotFiles(nb) { | ||||||
|  |     for (let i = 0; i < nb; i++) { | ||||||
|  |       const ouputFile = path.join( | ||||||
|  |         outputDir, | ||||||
|  |         'nigbot_' + i.toString() + '.png' | ||||||
|  |       ) | ||||||
|  |       if (await fs.existsSync(ouputFile)) { | ||||||
|  |         console.log(`Skipping ${ouputFile}, file already exists`) | ||||||
|  |         continue | ||||||
|  |       } | ||||||
|  |  | ||||||
|  |       const firstPart = 'nigbot' | ||||||
|  |       const firstFile = computeFilename(firstPart, i) | ||||||
|  |  | ||||||
|  |       const buff = await sharp(firstFile) | ||||||
|  |         .resize(60, 60) | ||||||
|  |         .png({ | ||||||
|  |           compressionLevel: 9, | ||||||
|  |           palette: true | ||||||
|  |         }) | ||||||
|  |         .toBuffer() | ||||||
|  |  | ||||||
|  |       await sharp(buff) | ||||||
|  |         .toFile(ouputFile) | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   // Function to generate other files with composites | ||||||
|  |   async function generateOtherFiles(nb) { | ||||||
|     for (let i = 0; i < nb; i++) { |     for (let i = 0; i < nb; i++) { | ||||||
|       const ouputFile = path.join( |       const ouputFile = path.join( | ||||||
|         outputDir, |         outputDir, | ||||||
| @ -148,13 +242,24 @@ async function generateAvatars (part) { | |||||||
|       const firstPart = partsToCombine.shift() |       const firstPart = partsToCombine.shift() | ||||||
|       const firstFile = computeFilename(firstPart, i) |       const firstFile = computeFilename(firstPart, i) | ||||||
|  |  | ||||||
|     // We just have to combinate different parts into one file, then output at the wanted size. |       if (firstFile.includes('nigbot')) { | ||||||
|  |         console.log(`Skipping ${firstFile}, file name contains "nigbot"`) | ||||||
|  |         continue | ||||||
|  |       } | ||||||
|  |  | ||||||
|       const composites = [] |       const composites = [] | ||||||
|       let j = 0 |       let j = 0 | ||||||
|       for (const part of partsToCombine) { |       for (const part of partsToCombine) { | ||||||
|         j++ // introduce an offset so we don't get all empty parts at the same time |         j++ // introduce an offset so we don't get all empty parts at the same time | ||||||
|  |         const filename = computeFilename(part, i + (j * 7)) | ||||||
|  |  | ||||||
|  |         if (filename.includes('nigbot')) { | ||||||
|  |           console.log(`Skipping ${filename}, file name contains "nigbot"`) | ||||||
|  |           continue | ||||||
|  |         } | ||||||
|  |  | ||||||
|         composites.push({ |         composites.push({ | ||||||
|         input: computeFilename(part, i + (j * 7)) |           input: filename | ||||||
|         }) |         }) | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @ -170,8 +275,18 @@ async function generateAvatars (part) { | |||||||
|         }) |         }) | ||||||
|         .toFile(ouputFile) |         .toFile(ouputFile) | ||||||
|     } |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   const nb = 200 // number of avatars to generate | ||||||
|  |  | ||||||
|  |   // Generate "nigbot" files | ||||||
|  |   await generateNigbotFiles(nb) | ||||||
|  |  | ||||||
|  |   // Generate other files with composites | ||||||
|  |   await generateOtherFiles(nb) | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| async function generateBotsAvatars () { | async function generateBotsAvatars () { | ||||||
|   { |   { | ||||||
|     // Moderation bot avatar: choosing some parts, and turning it so it is facing left. |     // Moderation bot avatar: choosing some parts, and turning it so it is facing left. | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user