Last active
June 29, 2020 01:13
-
-
Save alannnna/4bfde468dcb42d88f3aa3efb060fad44 to your computer and use it in GitHub Desktop.
Show a scrimage image in Kotlin (Mac only)
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
plugins { | |
id 'java' | |
id 'org.jetbrains.kotlin.jvm' version '1.3.61' | |
} | |
group 'mygroup' | |
version '1.0-SNAPSHOT' | |
sourceCompatibility = 1.8 | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" | |
implementation "com.sksamuel.scrimage:scrimage-core:4.0.5" | |
} | |
compileKotlin { | |
kotlinOptions.jvmTarget = "1.8" | |
} | |
compileTestKotlin { | |
kotlinOptions.jvmTarget = "1.8" | |
} |
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
import com.sksamuel.scrimage.ImmutableImage | |
import com.sksamuel.scrimage.nio.PngWriter | |
import java.io.File | |
// Show image | |
// Implementation based on: | |
// https://github.com/python-pillow/Pillow/blob/77c098b01ffd599ec555d22ac76fd16563f4946f/src/PIL/ImageShow.py#L150-L162 | |
fun ImmutableImage.show() { | |
// Save image to a temp file | |
val f = File.createTempFile("tmp", ".png") | |
output(PngWriter(), f) | |
val commandStr = "open -a Preview.app $f; sleep 20; rm -f $f" | |
Runtime.getRuntime().exec(arrayOf("/bin/sh", "-c", commandStr)) | |
} | |
fun main() { | |
val im = ImmutableImage.loader().fromFile(File("images/dolphin.jpg")) | |
im.show() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment