Last active
September 1, 2024 12:10
-
-
Save retheviper/fc90347d6718a22d81001523f4c17d66 to your computer and use it in GitHub Desktop.
Create LGTM image with Scrimage
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"collapsed": true, | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.401069Z", | |
"start_time": "2024-09-01T12:06:39.379797Z" | |
} | |
}, | |
"source": [ | |
"import com.sksamuel.scrimage.ImmutableImage\n", | |
"import com.sksamuel.scrimage.graphics.RichGraphics2D\n", | |
"import com.sksamuel.scrimage.nio.PngWriter\n", | |
"import java.awt.Color\n", | |
"import java.awt.Font\n", | |
"import java.awt.RenderingHints\n", | |
"import java.awt.geom.Rectangle2D\n", | |
"import java.io.File\n", | |
"import java.nio.file.Files\n", | |
"import java.nio.file.Path\n", | |
"import java.nio.file.StandardOpenOption" | |
], | |
"outputs": [], | |
"execution_count": 33 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.441935Z", | |
"start_time": "2024-09-01T12:06:39.405859Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Read image\n", | |
"val imageFile = File(\"test_image.png\")\n", | |
"val image = ImmutableImage.loader().fromFile(imageFile)" | |
], | |
"outputs": [], | |
"execution_count": 34 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.467261Z", | |
"start_time": "2024-09-01T12:06:39.444271Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Prepare text\n", | |
"val text = \"LGTM\"\n", | |
"val fontSize = image.width / 3\n", | |
"val font = Font(\"Arial\", Font.BOLD, fontSize)" | |
], | |
"outputs": [], | |
"execution_count": 35 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.488604Z", | |
"start_time": "2024-09-01T12:06:39.469594Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Prepare render\n", | |
"val g2 = RichGraphics2D(image.awt().createGraphics())\n", | |
"g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)" | |
], | |
"outputs": [], | |
"execution_count": 36 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.510602Z", | |
"start_time": "2024-09-01T12:06:39.490937Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Set font and calculate text size\n", | |
"g2.font = font\n", | |
"val textBounds: Rectangle2D = g2.fontMetrics.getStringBounds(text, g2)" | |
], | |
"outputs": [], | |
"execution_count": 37 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.536310Z", | |
"start_time": "2024-09-01T12:06:39.512516Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Calculate position of text (Center)\n", | |
"val x = (image.width - textBounds.width) / 2\n", | |
"val y = (image.height - textBounds.height) / 2 + g2.fontMetrics.ascent" | |
], | |
"outputs": [], | |
"execution_count": 38 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.601222Z", | |
"start_time": "2024-09-01T12:06:39.538422Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Draw outline of text\n", | |
"val outlineOffset = 3\n", | |
"g2.color = Color.BLACK\n", | |
"for (dx in -outlineOffset..outlineOffset) {\n", | |
" for (dy in -outlineOffset..outlineOffset) {\n", | |
" if (dx != 0 || dy != 0) {\n", | |
" g2.drawString(text, (x + dx).toFloat(), (y + dy).toFloat())\n", | |
" }\n", | |
" }\n", | |
"}" | |
], | |
"outputs": [], | |
"execution_count": 39 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.626177Z", | |
"start_time": "2024-09-01T12:06:39.603674Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Draw text\n", | |
"g2.color = Color.WHITE\n", | |
"g2.drawString(text, x.toFloat(), y.toFloat())" | |
], | |
"outputs": [], | |
"execution_count": 40 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.645210Z", | |
"start_time": "2024-09-01T12:06:39.628877Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Dispose resource\n", | |
"g2.dispose()" | |
], | |
"outputs": [], | |
"execution_count": 41 | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"end_time": "2024-09-01T12:06:39.769610Z", | |
"start_time": "2024-09-01T12:06:39.647599Z" | |
} | |
}, | |
"cell_type": "code", | |
"source": [ | |
"// Save image\n", | |
"val outputStream = Files.newOutputStream(Path.of(\"test_image_lgtm.png\"), StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)\n", | |
"outputStream.use {\n", | |
" PngWriter.NoCompression.write(image, image.metadata, it)\n", | |
"}" | |
], | |
"outputs": [], | |
"execution_count": 42 | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Kotlin", | |
"language": "kotlin", | |
"name": "kotlin" | |
}, | |
"language_info": { | |
"name": "kotlin", | |
"version": "1.9.23", | |
"mimetype": "text/x-kotlin", | |
"file_extension": ".kt", | |
"pygments_lexer": "kotlin", | |
"codemirror_mode": "text/x-kotlin", | |
"nbconvert_exporter": "" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment