Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

1
Hi, how can I crop image to svg/png mask when its not a rectangle?
Post Body

Hi there

Say I have a Z shaped white mask and I want to crop an image to this shape. How do I do this in java?

Both images are 2560x1440, but the Z shaped mask has transparent bits.

My attempt in scala but using Java's awt library:

val imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val imageB = imageBuff.createGraphics()
imageB.drawImage(image, 0, 0, null)

val maskBuffered = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val maskB = mask01Buffered.createGraphics()
maskB.drawImage(scaledmask, 0, 0, null)


def applyMask(imageBuffered: BufferedImage, maskBuffered: BufferedImage): BufferedImage = {
  val imagePixels = heroBuffered.getRGB(0, 0, width, height, null, 0, width)
  val maskPixels = maskBuffered.getRGB(0, 0, width, height, null, 0, width)

 for (i <- 0 to maskPixels.length - 1)
  {
    val color = imagePixels(i)
    val mask = maskPixels(i) 
    imagePixels(i) = mask | color
  }

  imageBuffered.setRGB(0, 0, width, height, imagePixels, 0, width)
  imageBuffered
}

val maskApplied = applyMask(imageBuff, maskBuffered)

val scaledForeground: Image = maskApplied.getScaledInstance((width * 1.08).toInt, (height * 1.08).toInt, Image.SCALE_DEFAULT)

val finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val f = finalImage.createGraphics
val newX: Int = (0 - width * 0.08).toInt
val newY: Int = (0 - height * 0.08).toInt
f.drawImage(scaledForeground, newX, newY, null)

This creates an image where I get the original image showing through the transparent bits of the mask and then the white of the mask overs the rest of the image, but what I really want is the other way around. I have tried having imagePixels(i) = color | mask instead but I get the same result.

Does anyone have any other ideas?

Many thanks, J

Author
Account Strength
100%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
2,792
Link Karma
563
Comment Karma
2,219
Profile updated: 3 days ago
Posts updated: 5 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago