Jump to content
Sign in to follow this  
Phil Mood

Crop Image Background

Recommended Posts

Hi, I use the CropImage function to insert an image from a file into a container field,

Base64Decode ( Base64Encode ( CropImage( $importpath ; myTable::img_X ; myTable::img_Y ; myTable::img_pic_width ; myTable::img_pic_height ) ) ; myTable::img_destination_fieldname )

It works perfectly but, if the XY coordinates are negatives, the background that fills the empty space is set automatically to black.

Ex:

Base64Decode ( Base64Encode ( CropImage( $importpath ; -200 ; -75 ; 500 ; 250 ) ) ; myTable::img_destination_fieldname )

https://punto-rosso.d.pr/GHn8Vv

I use negative coordinates to position the picture inside the container.

How I can avoid the black background? Or at least set it to white/transparent.

Thanks

Small update: the problem exist even with positive values. Ex.

Base64Decode ( Base64Encode ( CropImage( $importpath ; 25 ; 100; 500 ; 250 ) ) ;myTable::img_destination_fieldname )

https://punto-rosso.d.pr/u505b6

It seems that the Crop function simply add automatically a black background to fill any empty space.

Share this post


Link to post
Share on other sites
Posted (edited)

For JPG

Add this at the top

import java.awt.Color

 Then add

//add these
d.setBackground(Color.WHITE)
//or choose any other Color you prefer
//see https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
d.clearRect(0, 0, widthInt, heightInt)
//before this
d.drawImage(img, xInt*-1, yInt*-1, null)

This line/param

BufferedImage.TYPE_INT_RGB

says it is an RGB image

If your source has transparency then you want to be using BufferedImage.TYPE_INT_ARGB, but the code to get/create transparency where you have a negtive x or y is a little more difficult...

Edited by john renfrew

Share this post


Link to post
Share on other sites

Hi John,

thats perfect!

The ARGB argument did the trick. A genius strike! ;)

Thanks a lot for your help.

Best

Share this post


Link to post
Share on other sites

Hi John,

I am trying to obtain the same result with the function "Rotate Image" but somehow your trick doesn't work here.

Any help is really appreciated.

Thanks

Share this post


Link to post
Share on other sites

Are you trying to rotate and resize at the same time?? Surely when you rotate an image there is no 'spare' to be cast as transparent? 

If you rotate a 400*300px image by 90degrees then you get a 300*400px image, or are you trying to rotate by other angles??

Share this post


Link to post
Share on other sites

Here is a new function for rotation of PNG files, which should fix your issue, if you pass it a file with no transparency or alphait returns an error to you

If you do SMGetVariable("cm") after running it you will see the color model Java thinks the file has

 

// RotatePNG ( imgLocation ; degrees )
// v1.0
// 19_03_12 JR


import javax.imageio.ImageIO
import java.awt.image.BufferedImage
import java.awt.Graphics2D
import java.awt.geom.AffineTransform
import java.awt.RenderingHints


isTransparent = {image, x, y ->
	pixel = image.getRGB(x,y)
	return (pixel>>24) == 0x00
}

containsTransparency  = { image ->
	for (i in 0..<image.getHeight()) {
		for (j in 0..<image.getWidth()) {
			if (isTransparent(image, j, i)){
				return true
			} //end if
		} //end for
	} //end for
} //end containsTransparency

containsAlphaChannel ={ image ->
	return image.getColorModel().hasAlpha()
}//end containsAlphaChannel

degrees = Double.parseDouble(degrees)
radians = Math.toRadians(degrees)
isRightAngle = degrees % 90 == 0

BufferedImage img
if (imgLocation.indexOf("://") != -1) {
	img = ImageIO.read(new URL(imgLocation))
} else {
	img = ImageIO.read(new File(imgLocation))
}

cm = img.getColorModel()
//check for transparency
try{
isAlpha = containsAlphaChannel(img)
if ( !isAlpha) {
	isTransparent = containsTransparency(img)
	if ( !isTransparent){
		return 'ERROR - no transparency'
	}//end if
	return 'ERROR - no transparency'
} //end if
} catch (e) {
	return 'ERROR - unspecified'
}

int newWidth = Math.abs(Math.cos(radians) * img.getWidth()) + Math.abs(Math.sin(radians) * img.getHeight())
int newHeight = Math.abs(Math.cos(radians) * img.getHeight()) + Math.abs(Math.sin(radians) * img.getWidth())
at = AffineTransform.getRotateInstance(radians, img.getWidth()/2, img.getHeight()/2)

type = BufferedImage.TYPE_INT_ARGB

dest = new BufferedImage(newWidth, newHeight, type)

g2d = dest.createGraphics()
if (!isRightAngle) {
    // enable antialiasing for odd angles
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}

g2d.setTransform(AffineTransform.getTranslateInstance((newWidth - img.getWidth()) / 2, (newHeight - img.getHeight()) / 2))
g2d.drawImage(img, at, null)

return dest

So only two parameters as you don't care about background colour

  • Like 1

Share this post


Link to post
Share on other sites

Hi John,

you're the best!

A big, big thanks, I really appreciate your help.

Have a nice day.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.