Skip to content

Instantly share code, notes, and snippets.

@tuesd4y
Last active October 10, 2019 07:43
Show Gist options
  • Save tuesd4y/bed45a9d7522143cfda9de571ff46a1c to your computer and use it in GitHub Desktop.
Save tuesd4y/bed45a9d7522143cfda9de571ff46a1c to your computer and use it in GitHub Desktop.
imagemagick scripts
package at.triply.backend.eventbackend
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import java.util.concurrent.TimeUnit
object Main {
@JvmStatic
fun main1(args: Array<String>) {
// copy pairs of two files in one file with imagemagick
val f = File("fotodir")
fun String.runCommand(workingDir: File) {
ProcessBuilder(*split(" ").toTypedArray())
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()
.waitFor(60, TimeUnit.MINUTES)
}
val (first, second) = f.listFiles()
.mapIndexed { index, file -> index to file }
.groupBy { it.first.rem(2) }
.values
.toList()
for (i in first.indices) {
val f1 = first[i]
if (i < second.size) {
val f2 = second[i]
"""convert -append ${f1.second.absolutePath} ${f2.second.absolutePath} ${f.absolutePath}/image_$i.jpg"""
.runCommand(f)
} else {
Files.copy(Paths.get(f1.second.toURI()), Paths.get("${f.absolutePath}/image_$i.jpg"), StandardCopyOption.COPY_ATTRIBUTES)
}
}
}
@JvmStatic
fun main(args: Array<String>) {
val f = File("/FOLDER")
//group four pictures into one picture. Do the rotation scipt in the folder first, this only works with landscape rotated images.
fun String.runCommand(workingDir: File) {
ProcessBuilder(*split(" ").toTypedArray())
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()
.waitFor(60, TimeUnit.MINUTES)
}
val (first, second, third, fourth) = f.listFiles()
.mapIndexed { index, file -> index to file }
.groupBy { it.first.rem(4) }
.values
.toList()
for (i in first.indices) {
val f1 = first[i]
val f2 = second.getOrElse(i) { f1 }
val f3 = third.getOrElse(i) { f1 }
val f4 = fourth.getOrElse(i) { f1 }
val temp1 = "temp1"
val temp2 = "temp2"
"""convert -append ${f1.second.absolutePath} ${f2.second.absolutePath} ${f.absolutePath}/${temp1}_$i.jpg"""
.runCommand(f)
"""convert -append ${f3.second.absolutePath} ${f4.second.absolutePath} ${f.absolutePath}/${temp2}_$i.jpg"""
.runCommand(f)
"""convert +append ${f.absolutePath}/${temp1}_$i.jpg ${f.absolutePath}/${temp2}_$i.jpg ${f.absolutePath}/out_$i.jpg"""
.runCommand(f)
}
"rm ${f.absolutePath}/temp*.jpg".runCommand(f)
}
}
# print all photos in portrait orientation
for i in *.jpg; do identify $i | awk '{split($3,a,"x"); if (a[2]>a[1]) print $1;}'; done
# rotate all portrait photos to landscape:
for i in *.jpg; do identify $i | awk '{split($3,a,"x"); if (a[2]>a[1]) system("convert -rotate 90 "$1" "$1);}'; done
# src (partially: https://www.commandlinefu.com/commands/view/13432/determine-if-photos-have-been-rotated-to-portrait-orientation-instead-of-normal-landscape-orientation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment