cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
thickey
Level III

Crop a JPG Image

Trying to crop the light grey border from around the outside of the below wafermap image using the following script. I guess I'm not understanding how the crop message works as there is no difference in the before and after images.

 

Any idea of what I'm doing wrong? 

 

Cheers, Troy

 

 

img = open("D:\wafer.jpg", jpg);
win1 = newWindow("Original", img);

img << crop(left(5),right(5),top(5),bottom(5));
win2 = newWindow("Cropped", img);

img << savePicture("D:\waferCropped.jpg", jpg);

 

thickey_0-1638495807241.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
thickey
Level III

Re: Crop a JPG Image

OK, so it looks like I just didn't understand how the crop function works. Fundamentally you need to know the height and width of the image you are cropping and work with those values when you crop, relative to the LHS and TOP Edges

Left = Number of pixels to remove from left side

Right = 'Width' (in pixels) of remaining image to keep

Top = Number of pixels to remove from top of image

Bottom = 'Height' (in pixels) of remaining image to keep

 

img = open("D:\WaferMaps\8PTPCVEB\wafer.jpg", jpg);
imgX = (img << GetSize)[1];
imgY = (img << GetSize)[2];
win1 = newWindow("Original", img);

myTrimBy = 5;
img << crop(left(myTrimBy),right(imgX - myTrimBy), top(myTrimBy), bottom(imgY - myTrimBy));
win2 = newWindow("Cropped", img);

img << savePicture("D:\WaferMaps\8PTPCVEB\waferCropped.jpg", jpg);

thickey_2-1638520391500.png

 

View solution in original post

9 REPLIES 9
Thierry_S
Super User

Re: Crop a JPG Image

Hi,

I think that you forgot to assign the cropped image to a variable:

img2 = img << crop(left(5),right(5),top(5),bottom(5));
win2 = newWindow("Cropped", img2);

Best,

TS

Thierry R. Sornasse
thickey
Level III

Re: Crop a JPG Image

Thanks for the reply Thierry.

I had tried exactly that as well but with no luck. 

 

 

 

Georg
Level VII

Re: Crop a JPG Image

Dear @Thierry_S ,

I would have thought the same way, this behaviour is strange, as the documentation say: "creates new image ...".

But in fact crop is changing the current image, so into my eyes the documentation is not correct, it is misleading.

@tonya_mauldin , do you think the documentation has to be changed according to this?

 

Names Default To Here( 1 );
img = Open( "$SAMPLE_IMAGES/tile.jpg", jpg );
Print( "Size before crop:", img << get size );

img << Crop(
	Left( 50 ),
	Right( 400 ),
	Top( 50 ),
	Bottom( 400 )
);

Print( "Size before crop:", img << get size );
print("So crop changes the image and does not create a new one as written in the documentation");

Images (jmp.com)

 

Georg
thickey
Level III

Re: Crop a JPG Image

OK, so it looks like I just didn't understand how the crop function works. Fundamentally you need to know the height and width of the image you are cropping and work with those values when you crop, relative to the LHS and TOP Edges

Left = Number of pixels to remove from left side

Right = 'Width' (in pixels) of remaining image to keep

Top = Number of pixels to remove from top of image

Bottom = 'Height' (in pixels) of remaining image to keep

 

img = open("D:\WaferMaps\8PTPCVEB\wafer.jpg", jpg);
imgX = (img << GetSize)[1];
imgY = (img << GetSize)[2];
win1 = newWindow("Original", img);

myTrimBy = 5;
img << crop(left(myTrimBy),right(imgX - myTrimBy), top(myTrimBy), bottom(imgY - myTrimBy));
win2 = newWindow("Cropped", img);

img << savePicture("D:\WaferMaps\8PTPCVEB\waferCropped.jpg", jpg);

thickey_2-1638520391500.png

 

gzmorgan0
Super User (Alumni)

Re: Crop a JPG Image

Nice job. The Right and Bottom definitions you provided are not exactly as you specified. Your script is spot on!

See the script below.

Names Default To Here( 1 );
img = Open( "$SAMPLE_IMAGES/tile.jpg", jpg );

s = {w,h} = img << get size();
show( s ); //{451, 451}
obj = New Window( "tile(40,40)", img );
Wait( 1 );
img << Crop( Left( 50 ), Right( 100 ), Top( 60 ), Bottom( 150 ) );
s = {w,h} = img << get size();
show( s ); //{50, 90}

//img is a matrix of pixels the initail size is 1 to 451 columns aka "w" width
//and "h" is height 1 to 451 rows

//Crop Left(x) is the starting column and Right(y) is the ending column 
//Top() and Bottom() works the same, so after the crop the width is 100-50=50
//and the height is  150-60=90
Craige_Hales
Super User

Re: Crop a JPG Image

Yes, it is a non-obvious interface and wants better doc. @Anonymous 

 

pixels = [
-11 -11 -11 -11 -11 -11 -11 -11 -11,
-11 -11 -91 -99 -99 -99 -99 -92 -11,
-11 -11 -99 -99 -99 -99 -99 -99 -11,
-11 -11 -99 -99 -99 -99 -99 -99 -11,
-11 -11 -99 -99 -99 -99 -99 -99 -11,
-11 -11 -93 -99 -99 -99 -99 -94 -11];

img = New Image( pixels );
{hSize, ySize} = img << size;
Show( img << getsize, img << getpixels );

// crop the -11 out of the image
img << crop( Left( 2 ), Right( 8 ), top( 1 ), bottom( 6 ) );
Show( img << getsize, img << getpixels );

////    img << getsize = {6, 5};
////    img << getpixels = 
////    [	-91 -99 -99 -99 -99 -92, 
////        -99 -99 -99 -99 -99 -99, 
////        -99 -99 -99 -99 -99 -99, 
////        -99 -99 -99 -99 -99 -99, 
////        -93 -99 -99 -99 -99 -94];

The zero-based pixel indexes are on the hairlines between the pixels.The zero-based pixel indexes are on the hairlines between the pixels.

Craige

Re: Crop a JPG Image

Thank you for bringing this to my attention. What document is being referenced in previous comments? 

 

Thanks,

Qiana

Craige_Hales
Super User

Re: Crop a JPG Image

@Georg  pointed to https://www.jmp.com/support/help/en/16.1/?os=win&source=application&utm_source=helpmenu&utm_medium=a...

Which appears identical to the scripting guide. syntax reference.

Capture1.PNG

 

I'd point to the scripting index:

Capture.PNG

Both say "creates a new image" but only the existing image is modified, which was part of the confusion, and neither does a good job explaining the parameters. There is another crop, for a segment, that is distinct from this one.

 

Thanks for looking at it!

Craige
hogi
Level XII

Re: Crop a JPG Image

Scripting Index Jmp18:

hogi_0-1714396298414.png