How to maintain image quality after cropping ?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
gorivalesumit
Posts: 3
Joined: 2014-08-01T01:43:53-07:00
Authentication code: 6789

How to maintain image quality after cropping ?

Post by gorivalesumit »

I am using

Code: Select all

imagemagick api
The problem i am facing is that 'file size reduces to kbs after cropping'. Here is what i am doing...

1. I have image test.jpg of 5177 x 3451 dimention,aspect ratio 3/2 and having size 51.2mb.
2. In my html page inside div I am setting image width to 1000px so becomes height 667px according to aspect ratio.
3.I am using cropzoom plugin to crop suppose i give crop of 615 x 410 px and x=90 y = 79
4 . So i am executing comman becomes...

Code: Select all

convert D:\CropZoom\pictures\test.jpg -crop 615.0x410.0+98.0+79.0 -strip -quality 100 D:\CropZoom\pictures\output.jpg
After cropping output image becomes 31.2kb only why...? It should be in MB?


Update : Here is all command and my logic.....

Code: Select all

// resize image to original size...
convert D:\CropZoom\pictures\test.jpg -resize 5177.0 x 3451.0 -quality 100 D:\CropZoom\pictures\output.jpg

rotate image if rotation slider value not == 0
convert D:\CropZoom\pictures\output.jpg -background transparent -rotate 0.0 -quality 100 D:\CropZoom\pictures\output.jpg

//This fix the page of the image so it crops fine!
convert D:\CropZoom\pictures\output.jpg -repage 0x0+0+0 -quality 100 D:\CropZoom\pictures\output.jpg

//crop the image with the viewed into the viewport .i.e crop container div(1000 x 600)
convert D:\CropZoom\pictures\output.jpg -crop 1000.0x600.0+0.0+0.0  -quality 100 D:\CropZoom\pictures\output.jpg

//create the viewport to put the cropped image
convert -size 1000.0x600.0 xc:black -quality 100 D:\CropZoom\pictures\viewport.jpg

//crop image according selector width,height,pageX,pageY 
convert D:\CropZoom\pictures\output.jpg -crop 615.0x410.0+98.0+79.0  -quality 100 D:\CropZoom\pictures\cropped.jpg 
Last edited by gorivalesumit on 2014-08-01T03:35:54-07:00, edited 1 time in total.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to maintain image quality after cropping ?

Post by Bonzo »

2. In my html page inside div I am setting image width to 1000px so becomes height 667px according to aspect ratio.
So the image is still 5177 x 3451 and you are just displaying it at 1000 x 667 which is irrelevant in this question

615 x410 is about ten times smaller than 5177x3451 so you expect a smaller file size, you are using -strip that removes all EXIF data and any profile, jpg compression has an effect and the density could be changing.
gorivalesumit
Posts: 3
Joined: 2014-08-01T01:43:53-07:00
Authentication code: 6789

Re: How to maintain image quality after cropping ?

Post by gorivalesumit »

Thanks for your reply. yes i am displaying it 1000 x 667 because user can see full image inside viewport (crop container div) and he can crop. And yes even i remove -strip output image becoming 142kbs
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to maintain image quality after cropping ?

Post by Bonzo »

You have a lot of potential compression every time you save it as a jpg.

What I do is make up a command variable containing all the code in one go; something like:

Code: Select all

// Resizing every one so
$cmd = '-resize 5177.0 x 3451.0';

if ( rotation slider value not !== 0 ){
$cmd .= ' -background transparent -rotate 0.0 ';
}

$cmd .= ' -repage 0x0+0+0 ';

// And so on; this means you will have one convert operation and $cmd will contain the operations to use

// You can then echo $cmd to see what it is going to do
echo $cmd.'<br>';

exec(" convert $input $cmd $output ");
gorivalesumit
Posts: 3
Joined: 2014-08-01T01:43:53-07:00
Authentication code: 6789

Re: How to maintain image quality after cropping ?

Post by gorivalesumit »

thanks i try out...can you plz tell me how to get image actual X Y for e.g in below image you can see my crop selection (29,27) and on original image it is 134,138 ?
http://i.imgur.com/1IPBpQF.jpg
Post Reply