Created
February 13, 2024 12:07
-
-
Save alessioC42/12d2f49953497ef665338d42b185d634 to your computer and use it in GitHub Desktop.
School Portal Hesse: compare all school images with the inverted image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Jimp = require('jimp'); | |
async function getSchools() { | |
let response = await fetch("https://startcache.schulportal.hessen.de/exporteur.php?a=schoollist"); | |
let data = await response.json(); | |
let result = []; | |
for (let i = 0; i < data.length; i++) { | |
for (let j = 0; j < data[i].Schulen.length; j++) { | |
result.push(data[i].Schulen[j].Id); | |
} | |
if (i === data.length - 1) { | |
return result; | |
} | |
} | |
} | |
async function main() { | |
let schoolIds = await getSchools(); | |
const totalSchools = schoolIds.length; | |
let completedSchools = 0; | |
for (let i = 0; i < schoolIds.length; i++) { | |
const schoolID = schoolIds[i]; | |
// Load the original image using jimp | |
const originalImage = await Jimp.read(`https://startcache.schulportal.hessen.de/exporteur.php?a=schoollogo&i=${schoolID}`); | |
if (originalImage.getHeight() === 1) { | |
completedSchools++; | |
continue; | |
} | |
// Invert the colors | |
const invertedImage = originalImage.clone().invert(); | |
// Create a new canvas to display both images side by side | |
const canvasWidth = originalImage.getWidth() + invertedImage.getWidth() + 40; // Add padding | |
const canvasHeight = Math.max(originalImage.getHeight(), invertedImage.getHeight()) + 20; | |
const canvas = new Jimp(canvasWidth, canvasHeight, 0xfffbffff); // White background | |
// Jimp has no function to fill a rectangle with a color. | |
for (let x = 0; x < canvasWidth / 2; x++) { | |
for (let y = 0; y < canvasHeight; y++) { | |
canvas.setPixelColor(0x1d1b1eff, x + canvasWidth/2, y); | |
} | |
} | |
// Composite the images onto the canvas | |
canvas.composite(originalImage, 10, (canvasHeight - originalImage.getHeight()) / 2); | |
canvas.composite(invertedImage, originalImage.getWidth() + 30, (canvasHeight - invertedImage.getHeight()) / 2); | |
// Save the combined image to a file | |
const filename = `outputs/${schoolID}.png`; | |
await canvas.writeAsync(filename); | |
completedSchools++; | |
// Update progress bar | |
const progress = (completedSchools / totalSchools) * 100; | |
console.log(`Progress: ${progress.toFixed(2)}%`); | |
} | |
console.log('All school logos processed and saved.'); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment