Last active
February 3, 2026 20:24
-
-
Save dacr/6583b7802a54946a8e2091ca1eea9261 to your computer and use it in GitHub Desktop.
Draws several photos as mosaic / published by https://github.com/dacr/code-examples-manager #39882020-04be-4cd4-82e2-14efa7c5ec6b/72759b82ce8262d54e97a38133334daf982b95c7
This file contains hidden or 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
| // summary : Draws several photos as mosaic | |
| // keywords : scala, vector-graphics, doodle, photos | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 39882020-04be-4cd4-82e2-14efa7c5ec6b | |
| // created-on : 2023-08-13T13:53:06.700685431Z | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "org.creativescala::doodle:0.22.0" | |
| //> using dep "fr.janalyse::naturalsort:1.0.4" | |
| // --------------------- | |
| import doodle.core.* | |
| import doodle.core.format.* | |
| import doodle.syntax.* | |
| import doodle.syntax.all.* | |
| import doodle.syntax.transform.* | |
| import doodle.image.* | |
| import doodle.image.syntax.all.* | |
| import doodle.java2d.* | |
| import doodle.java2d.effect.* | |
| import doodle.java2d.algebra.* | |
| import cats.effect.unsafe.implicits.global | |
| import cats.implicits.* | |
| import cats.Semigroup | |
| val filesJavaStream = { | |
| import java.nio.file.{Files, Path} | |
| val fromPath = Path.of(scala.util.Properties.envOrElse("PHOTOS_INTERNAL_DATA", ".")) | |
| Files.find( | |
| fromPath, | |
| 4, | |
| (path, attrs) => path.toString.contains("32.jpg") | |
| ) | |
| } | |
| //val pictures = { | |
| // import java.util.stream.Collectors | |
| // import scala.jdk.CollectionConverters.* | |
| // filesJavaStream | |
| // .limit(10000) | |
| // .map(path => Picture.read(path.toFile)) | |
| // .collect(Collectors.toList[Picture[Unit]]) | |
| // .asScala | |
| // .toList | |
| //} | |
| val pictures = { | |
| import java.util.stream.Collectors | |
| import scala.jdk.CollectionConverters.* | |
| import java.nio.file.Path | |
| import fr.janalyse.tools.NaturalSort._ | |
| filesJavaStream | |
| .limit(10) | |
| .collect(Collectors.toList[Path]) | |
| .asScala | |
| .toList | |
| .sortBy(_.toFile.getName) | |
| .map(path => Picture.read(path.toFile)) | |
| } | |
| val frame = Frame( | |
| size = Size.fitToPicture(5), | |
| title = "Photos", | |
| center = Center.atOrigin, | |
| background = None, | |
| redraw = Redraw.clearToBackground | |
| ) | |
| //pictures | |
| // .grouped(140) | |
| // .map(_.reduce((a,b) => a.beside(b))) | |
| // .reduce((a,b) => b.below(a)) | |
| // .scale(0.40, 0.40) | |
| // .drawWithFrame(frame) | |
| def besizeNormalized(a: Picture[Unit], b: Picture[Unit]) = { | |
| for { | |
| asize <- a.size | |
| (_, aheight) = asize | |
| bsize <- b.size | |
| (_, bheight) = bsize | |
| ascale = 32d / aheight | |
| bscale = 32d / bheight | |
| an = if (ascale == 1d) a else a.scale(ascale, ascale) | |
| bn = if (bscale == 1d) b else b.scale(bscale, bscale) | |
| } yield an.beside(bn) | |
| } | |
| pictures | |
| .grouped(140) | |
| .map( | |
| _.reduce((a, b) => | |
| // a.beside(b) | |
| for { | |
| asize <- a.size | |
| bsize <- b.size | |
| (_, aheight) = asize | |
| (_, bheight) = bsize | |
| ascale = if (aheight == 0d) 1d else 32d / aheight | |
| bscale = if (bheight == 0d) 1d else 32d / bheight | |
| an = a.scale(ascale, ascale) | |
| bn = b.scale(bscale, bscale) | |
| } yield { | |
| println(s"$asize $bsize : $ascale - $bscale") | |
| // an.beside(bn) | |
| a.beside(b) | |
| } | |
| ) | |
| ) | |
| .reduce((a, b) => b.below(a)) | |
| .scale(0.40, 0.40) | |
| .drawWithFrame(frame) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment