Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| yiler-huang-generating-photos-with-code [2023/06/05 23:13] – yiler.huang | yiler-huang-generating-photos-with-code [2023/06/05 23:33] (current) – yiler.huang | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| ===== First round ===== | ===== First round ===== | ||
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| - | {{: | + | {{: |
| ===== Second round ===== | ===== Second round ===== | ||
| {{: | {{: | ||
| + | |||
| + | ===== Third round ===== | ||
| + | |||
| + | ==== First program ==== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ==== Second program ==== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | <code javascript> | ||
| + | // import the JIMP library | ||
| + | |||
| + | const Jimp = require(" | ||
| + | |||
| + | |||
| + | const fs = require(' | ||
| + | |||
| + | |||
| + | // knuth shuffle from https:// | ||
| + | //shuffle | ||
| + | function shuffle(array) { | ||
| + | var currentIndex = array.length, | ||
| + | randomIndex; | ||
| + | // While there remain elements to shuffle... | ||
| + | while (currentIndex != 0) { | ||
| + | // Pick a remaining element... | ||
| + | randomIndex = Math.floor(Math.random() * currentIndex); | ||
| + | currentIndex--; | ||
| + | // And swap it with the current element. | ||
| + | [array[currentIndex], | ||
| + | array[randomIndex], | ||
| + | ]; | ||
| + | } | ||
| + | return array; | ||
| + | } | ||
| + | |||
| + | // | ||
| + | function randomRange(min, | ||
| + | return min + ((max - min) * Math.random()) | ||
| + | } | ||
| + | |||
| + | |||
| + | // | ||
| + | function pickFromArray(array) { | ||
| + | return array[Math.floor(Math.random() * array.length)]; | ||
| + | } | ||
| + | |||
| + | //from https:// | ||
| + | // set the path data in an array | ||
| + | |||
| + | let pathArray = [ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ]; | ||
| + | |||
| + | |||
| + | function trueOrFalse() { | ||
| + | let output = Math.random() < 0.5 ? true : false; | ||
| + | return output | ||
| + | } | ||
| + | |||
| + | // use map to read the path data and produce JIMP Promises | ||
| + | |||
| + | let newPA = shuffle(pathArray).slice(0, | ||
| + | let jimpData = newPA.map(path => Jimp.read(path)); | ||
| + | |||
| + | |||
| + | |||
| + | // through Promise.all, | ||
| + | // the data processing will proceed in parallel, just as it does in Hydra | ||
| + | |||
| + | for (let i = 0; i < 50; i++) { | ||
| + | Promise.all(jimpData).then((pictures) => { | ||
| + | return pictures[0] | ||
| + | .grayscale() | ||
| + | //composite argument1: shiftX, argument2: shiftY | ||
| + | .composite(pictures[1].color([{ | ||
| + | apply: " | ||
| + | params: [randomRange(0, | ||
| + | }]).mirror(trueOrFalse(), | ||
| + | mode: Jimp.BLEND_LIGHTEN, | ||
| + | opacitySource: | ||
| + | }) | ||
| + | .composite(pictures[2].grayscale().dither565().dither565(), | ||
| + | mode: | ||
| + | opacitySource: | ||
| + | }) | ||
| + | .color([{ | ||
| + | apply: " | ||
| + | params: [randomRange(-360, | ||
| + | }]) | ||
| + | .resize(2000, | ||
| + | .write(`./ | ||
| + | }).catch((err) => { | ||
| + | console.error(err) | ||
| + | }) | ||
| + | newPA = shuffle(pathArray).slice(0, | ||
| + | jimpData = newPA.map(path => Jimp.read(path)); | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Program 3 ==== | ||
| + | |||
| + | |||
| + | {{: | ||
| + | |||
| + | |||
| + | < | ||
| + | // import the JIMP library | ||
| + | |||
| + | const Jimp = require(" | ||
| + | |||
| + | |||
| + | const fs = require(' | ||
| + | |||
| + | |||
| + | // knuth shuffle from https:// | ||
| + | //shuffle | ||
| + | function shuffle(array) { | ||
| + | var currentIndex = array.length, | ||
| + | randomIndex; | ||
| + | // While there remain elements to shuffle... | ||
| + | while (currentIndex != 0) { | ||
| + | // Pick a remaining element... | ||
| + | randomIndex = Math.floor(Math.random() * currentIndex); | ||
| + | currentIndex--; | ||
| + | // And swap it with the current element. | ||
| + | [array[currentIndex], | ||
| + | array[randomIndex], | ||
| + | ]; | ||
| + | } | ||
| + | return array; | ||
| + | } | ||
| + | |||
| + | // | ||
| + | function randomRange(min, | ||
| + | return min + ((max - min) * Math.random()) | ||
| + | } | ||
| + | |||
| + | |||
| + | // | ||
| + | function pickFromArray(array) { | ||
| + | return array[Math.floor(Math.random() * array.length)]; | ||
| + | } | ||
| + | |||
| + | //from https:// | ||
| + | // set the path data in an array | ||
| + | |||
| + | let pathArray = [ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ]; | ||
| + | |||
| + | |||
| + | function trueOrFalse() { | ||
| + | let output = Math.random() < 0.5 ? true : false; | ||
| + | return output | ||
| + | } | ||
| + | |||
| + | // use map to read the path data and produce JIMP Promises | ||
| + | |||
| + | let newPA = shuffle(pathArray).slice(0, | ||
| + | |||
| + | |||
| + | let jimpData = newPA.map(path => Jimp.read(path)); | ||
| + | |||
| + | |||
| + | |||
| + | // through Promise.all, | ||
| + | // the data processing will proceed in parallel, just as it does in Hydra | ||
| + | |||
| + | for (let i = 0; i < 100; i++) { | ||
| + | Promise.all(jimpData).then((pictures) => { | ||
| + | return pictures[0] | ||
| + | .grayscale() | ||
| + | //composite argument1: shiftX, argument2: shiftY | ||
| + | .composite(pictures[1].grayscale().invert(), | ||
| + | mode: | ||
| + | opacitySource: | ||
| + | }) | ||
| + | .composite(pictures[2].color([{ | ||
| + | apply: " | ||
| + | params: [randomRange(0, | ||
| + | }]).mirror(trueOrFalse(), | ||
| + | mode: Jimp.BLEND_LIGHTEN, | ||
| + | opacitySource: | ||
| + | }) | ||
| + | .color([{ | ||
| + | apply: " | ||
| + | params: [randomRange(-360, | ||
| + | }]) | ||
| + | .resize(2000, | ||
| + | .write(`./ | ||
| + | }).catch((err) => { | ||
| + | console.error(err) | ||
| + | }) | ||
| + | newPA = shuffle(pathArray).slice(0, | ||
| + | jimpData = newPA.map(path => Jimp.read(path)); | ||
| + | |||
| + | } | ||
| + | </ | ||