Crop image

Discussions on NConvert - the command line tool for image conversion and manipulation

Moderators: XnTriq, xnview

Post Reply
Zuldek
Posts: 12
Joined: Fri Jul 05, 2019 1:24 pm

Crop image

Post by Zuldek » Fri Jul 05, 2019 4:29 pm

How to set the correct command in order to crop the image to the specified size. Do not squeeze

?

cday
XnThusiast
Posts: 2091
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Crop image

Post by cday » Fri Jul 05, 2019 5:09 pm

Zuldek wrote:
Fri Jul 05, 2019 4:29 pm
How to set the correct command in order to crop the image to the specified size. Do not squeeze

Code: Select all

-crop x y w h     : Crop
Also the following option may be considered a 'crop' function:

Code: Select all

-canvas w h pos   : Resize canvas
              w h can be percent (ex: -canvas 100% 200%)
              or #x #y for offset
              pos top-left, top-center, top-right
                  center-left, center, center-right
                  bottom-left, bottom-center, bottom-right
Note that the second post in this thread shows how to create a copy of the help file for the version you are using. :D

Zuldek
Posts: 12
Joined: Fri Jul 05, 2019 1:24 pm

Re: Crop image

Post by Zuldek » Sat Jul 06, 2019 3:49 pm

Code: Select all

    nconvert -ratio -resize shortest 180 -overwrite %title_alt%.jpg
	nconvert -ratio -canvas 50 100 -overwrite %title_alt%.jpg
Does not go, the image size remains the same

cday
XnThusiast
Posts: 2091
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Crop image

Post by cday » Sat Jul 06, 2019 4:06 pm

Zuldek wrote:
Sat Jul 06, 2019 3:49 pm

Code: Select all

    nconvert -ratio -resize shortest 180 -overwrite %title_alt%.jpg
	nconvert -ratio -canvas 50 100 -overwrite %title_alt%.jpg
Does not go, the image size remains the same

Could you please explain in more detail what you wish to do, you wish to reduce the pixel dimensions of the input image %title_alt%.jpg ?

By cropping, or resizing the pixel dimensions without cropping?

Are you able to upload an example input image?

Zuldek
Posts: 12
Joined: Fri Jul 05, 2019 1:24 pm

Re: Crop image

Post by Zuldek » Sat Jul 06, 2019 4:46 pm

Reduce the canvas, that is, size the image. Parts of the image that do not fit the size, they are cut off.
Attachments
dff.jpg
dff.jpg (9.81 KiB) Viewed 284 times

cday
XnThusiast
Posts: 2091
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Crop image

Post by cday » Sat Jul 06, 2019 4:52 pm

Zuldek wrote:
Sat Jul 06, 2019 4:46 pm
Reduce the canvas, that is, size the image. Parts of the image that do not fit the size, they are cut off.
Reduce the width of the image, equally about the center??

Otherwise, could you draw on the image to indicate which part to wish to keep, or explain in more detail what you need?

Zuldek
Posts: 12
Joined: Fri Jul 05, 2019 1:24 pm

Re: Crop image

Post by Zuldek » Sat Jul 06, 2019 5:12 pm

A width of 200 is required, a height of 100. If possible, yes, in the center.

cday
XnThusiast
Posts: 2091
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Crop image

Post by cday » Sat Jul 06, 2019 5:17 pm

I think that's clear, I'll try to test the code required later, please be patient...

cday
XnThusiast
Posts: 2091
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Crop image

Post by cday » Sat Jul 06, 2019 6:48 pm

Zuldek wrote:
Sat Jul 06, 2019 5:12 pm
A width of 200 is required, a height of 100. If possible, yes, in the center.

This is the basic code to produce the image you need as I understand it, you may wish to edit the exact values used:

Code: Select all

nconvert -canvas 200 100 center dff.jpg

The following code can be used to change the name of the output file, if desired, and also add compression to the output JPEG, 70% here:

Code: Select all

nconvert -canvas 200 100 center -o Output.jpg -out jpeg -q 70 Input.jpg

For reference, the following is the help file listing for the -canvas option:

Code: Select all

-canvas w h pos   : Resize canvas
              w h can be percent (ex: -canvas 100% 200%)
              or #x #y for offset
              pos top-left, top-center, top-right
                  center-left, center, center-right
                  bottom-left, bottom-center, bottom-right

Edit:

If your need is actually to display the whole image vertically, rescaled to a height of 100 pixels, and to crop the width to 200 pixels without distorting the image, perhaps you could test the following options which I have never had a reason to use:

Code: Select all

-resize longest size    : Scale longest side
-resize shortest size   : Scale shortest side

Zuldek
Posts: 12
Joined: Fri Jul 05, 2019 1:24 pm

Re: Crop image

Post by Zuldek » Sun Jul 07, 2019 10:51 am

Code: Select all

	nconvert -ratio -resize shortest 180 -o titlee.jpg -q 70 scrinshot.jpg
    nconvert -canvas 320 180 center -o titlee.jpg -q 70 titlee.jpg
The first stage is image compression.
The second stage is cropping the image.

I don’t quite understand how to save the image with the same output name in the second stage. So that no titlee_1.jpg titlee_2.jpg

cday
XnThusiast
Posts: 2091
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Crop image

Post by cday » Sun Jul 07, 2019 11:55 am

Zuldek wrote:
Sun Jul 07, 2019 10:51 am

Code: Select all

	nconvert -ratio -resize shortest 180 -o titlee.jpg -q 70 scrinshot.jpg
    nconvert -canvas 320 180 center -o titlee.jpg -q 70 titlee.jpg
The first stage is image compression.
The second stage is cropping the image.
You seem to have made some progress, but it is sometimes difficult to understand what you wish to do... :wink:

Also NConvert has many options and I haven't used them all myself: I see the use of the -ratio option which was needed to avoid distorting the input image when resizing it, and I see that the -q option can be used without the -out option.

I don’t quite understand how to save the image with the same output name in the second stage. So that no titlee_1.jpg titlee_2.jpg
You wish the output from the second stage to have the name 'titlee.jpg' and it doesn't? I suspect the problem arises from the fact that when a file of the same name already exists in the same folder, the output file is given a suffix -1, -2 etc.

If that is the problem, I see two possible solutions: one would be to use the -D option to delete the input file, if that is acceptable, the second is to use the appropriate -o option:

Code: Select all

-o filename       : Output filename
              Use # to specify position of numeric enumerator
              Use % to specify source filename
              Use $ to specify full source pathname
              Use $$ to specify source folder name
              Use $EXIF:DateModified[date format] to specify EXIF date modified
              Use $EXIF:DateTaken[date format] to specify EXIF date taken
									Date format: Please check documentation of strftime
In this case -o %.jpg should work, I think without testing that, but note that in a batch file .bat the % symbol must be doubled to 'escape' the character, so -o %%.jpg.

Post Reply