Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a Watermark #65

Closed
igalbenardete opened this issue Dec 1, 2014 · 14 comments
Closed

Creating a Watermark #65

igalbenardete opened this issue Dec 1, 2014 · 14 comments

Comments

@igalbenardete
Copy link

I am trying to put watermarks on the images. I have tried half of the composite types but could not get the result I wanted. Composite combines the images but both images just appear faded or one just becomes completely dominated by the other and only a single image is visible. Is there an alternative for watermarks ? I found Watermarks.scala in the repository but could not make any sense out of it or use it.

@gwenzek
Copy link
Collaborator

gwenzek commented Dec 1, 2014

Here is a sample of code for watermarking. I don't know why there is no 'Add' composite in scrimage, but you can add it easily:

import com.sksamuel.scrimage.Image
import com.sksamuel.scrimage.composite._
import thirdparty.romainguy.BlendComposite.BlendingMode
import thirdparty.romainguy.BlendComposite

object Main extends App {
  class AddComposite(alpha: Double) extends BlenderComposite(BlendComposite.BlendingMode.ADD, alpha.toFloat)

  val bird = Image(new java.io.File("bird_small.png"))
  val watermark = Image(new java.io.File("watermark.png"))

  val marked = bird.composite(new AddComposite(0.5), watermark)

  marked.write("bird_marked.png")
}

bird_small.png
bird_small

watermark.png
watermark

bird_marked.png
bird_marked

@igalbenardete
Copy link
Author

So here is my outup
Main image:
color

Watermark:
special_5

RESULT:
car

@igalbenardete
Copy link
Author

By the way main image is jpeg and the watermark is png. Just letting you know so that that is not the problem, hopefully.

@gwenzek
Copy link
Collaborator

gwenzek commented Dec 1, 2014

With the code I put above with an alpha value of 1 (new AddComposite(1.0)), I go this:
car_sold

Which seems better (the car isn't altered). But if you are looking for a watermarking that can be use for a lot of images I wouldn't choose something with red in it.

@igalbenardete
Copy link
Author

This one is good enough and these are just example images. However, my results are completely different:
x

@gwenzek
Copy link
Collaborator

gwenzek commented Dec 2, 2014

I wasn't able to reproduce your result, but here is how I got mine:

import com.sksamuel.scrimage.Image
import com.sksamuel.scrimage.composite._
import thirdparty.romainguy.BlendComposite.BlendingMode
import thirdparty.romainguy.BlendComposite

object Main extends App {
  class AddComposite(alpha: Double) extends BlenderComposite(BlendComposite.BlendingMode.ADD, alpha.toFloat)

  val car = Image(new java.io.File("car.jpg"))
  val sold = Image(new java.io.File("sold.png"))

  val car_sold = car.composite(new AddComposite(1), sold)
  car_sold.write("car_sold.png")
}

Which composite and alpha parameter did you choose ?

@igalbenardete
Copy link
Author

currentImage = currentImage.composite(new AddComposite(1.0), watermarkImage)

This is how I use it and I defined AddComposite same as here.

@gwenzek
Copy link
Collaborator

gwenzek commented Dec 2, 2014

Really weird ...

Is their any other operation that you apply to your currentImage before or after that ?
because the result is bigger (800x600) than the car image (628x417) but it should be of the same size.

Indeed img.composite(..., other) is always of the size of img

@igalbenardete
Copy link
Author

A resizing operation is applied before the composite function is called but that should not matter

@igalbenardete
Copy link
Author

I fixed it. The watermark image is resized as well. When the resize function returns an image, it covers the background with white background. However the watermark png has a transparent background. Is there a way to avoid having a white background while using resize function ? Additionally, does the watermark image has to be transparent ? What if I want to make it completely opaque ? (This means that I just want to put one image on top of the other)

@gwenzek
Copy link
Collaborator

gwenzek commented Dec 2, 2014

ok this is interesting.
I tested it and scale doesn't remove transparency. But there are operations that does remove it, like saving as JPEG and load back (JPEG doesn't handle transparency)

In my first example, I didn't use a transparent image, but an image with a black background, because it is 'transparent' respectively to the add composite.

If you just want to draw over, just use the overlay method: car.overlay(sold).write("car_sold_2.png")
car_sold_2

@igalbenardete
Copy link
Author

Ok perfect this looks like it is working. So the only difference between scaleTo and resize is basically the background handling ?
With the composite and using resize with a color with 0 alpha still does not make it work but I guess scaleTo will be just fine for now. Overlay was what I am looking for thank you very much.

@igalbenardete
Copy link
Author

In resizeTo i was able to indicate a Position for the image, how can I do the same thing by using scaleTo ?

@gwenzek
Copy link
Collaborator

gwenzek commented Dec 4, 2014

The readme provides a bit of information about this. resize doesn't scale your image so if you resize to a bigger image it will have to fill the background with the provided color (white by default).

overlay(overlayImage: Image, x: Int = 0, y: Int = 0): Image let you specify where you want to draw the overlayImage. I think that would be the way to go.

@gwenzek gwenzek closed this as completed Dec 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants