Scala image processing library
I'm working with scrimage right now, and I need to get the image format from the file, as right now, if a GIF is uploaded, it will not become an animated JPEG. I need to be able to tell if the image file is a GIF so I know to not use the Format.JPEG writer. Is there any way to do this?
Source: (StackOverflow)
I have a for comprehension like this using an image library that supports concurrent operations https://github.com/sksamuel/scrimage :
for (
file <- myDirectory.listFiles;
image <- AsyncImage(file);
scaled <- image.scale(0.5)
// possibly more ops here?
)
{
scaled.writer(Format.PNG).write(new File(dirOutput + file.getName))
}
here is the definition for write()
:
def write(out: OutputStream)(implicit executionContext: ExecutionContext): Future[Unit] = Future {
writer.write(out)
}
What I want to do is wait until all my images in my directory finish resizing before my program shutdowns. From what I gleaned from other posts on SO is to basically stuff all these Futures into a list of Futures and and use Await
for that to finish... Can anyone help me out here?
Source: (StackOverflow)
I'm trying to combine jcrop and scrimage but I'm having trouble in understanding
the documentation of scrimage.
The user uploads an image. When the upload is done the user is able choose a fixed
area to crop with Jcrop:
upload.js
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
progress: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$("#progress").find(".progress-bar").css(
"width",
progress + "%"
);
},
done: function (e, data) {
$("#cropArea").empty();
var cropWidth = 210;
var cropHeight = 144;
if(data.result.status == 200) {
var myImage = $("<img></img>", {
src: data.result.link
}).appendTo('#cropArea');
var c = myImage.Jcrop({
allowResize: false,
allowSelect: false,
setSelect:[0,0,cropWidth,cropHeight],
onSelect: showCoords
});
}
}
});
});
Example:
When the user is satisfied the coordinates will be posted to the server and there is where the magic should happen.
Controller:
def uploadFile = Action(multipartFormDataAsBytes) { request =>
val result = request.body.files.map {
case FilePart(key, filename, contentType, bytes) => {
val coords = request.body.dataParts.get("coords")
val bais = new ByteArrayInputStream(bytes)
Image(bais).resize(magic stuff with coords)
Ok("works")
}
}
result(0)
}
If i read the docs for scrimage and resize:
Resizes the canvas to the given dimensions. This does not scale the
image but simply changes the dimensions of the canvas on which the
image is sitting. Specifying a larger size will pad the image with a
background color and specifying a smaller size will crop the image.
This is the operation most people want when they think of crop.
But when trying to implement resize with an inputstream Image(is).resize()
I'm not sure I how should do this. Resize takes a scaleFactor, position and color... I guess I should
populate the position with the coords I get from jcrop??, and what do I do with the scaleFactor? Anybody got a good example of how to do this this?
Thank you for two great libs!
Source: (StackOverflow)
I'm trying to wrap my head around futures, and have a little sample of scaling a directory of images going...
Can some explain to me how this should work?
import com.sksamuel.scrimage._
import scala.concurrent._
import ExecutionContext.Implicits.global
import java.io._
object Test {
def main(args: Array[String]) = {
val dir = new File("/Users/bthibault/Downloads/ff_images/")
dir.listFiles().foreach(file => AsyncImage(file).map( x => x.scale(0.5) ).onSuccess {
case image => image.writer(Format.PNG).withMaxCompression.write(file)
})
}
}
I'm using the Scrimage package https://github.com/sksamuel/scrimage/ where he gives an example of the Async operation... here is the actual Async file:
https://github.com/sksamuel/scrimage/blob/master/core/src/main/scala/com/sksamuel/scrimage/AsyncImage.scala
Can somebody help me understand what I need to do to get this working? I was playing with the map() function ... my thought was I need to return a real 'image' onSuccess rather than an AsyncImage ....
Source: (StackOverflow)