ImageMagick incorrect dimensions - php

I've been on this problem for several hours now. I can't crop/resize a certain image correctly.
The source image has a dimension of 900x398 px
The target dimension is 650x178 px
but the returned dimension is 647x178 px. I dont't get it. This is the command I use:
/usr/bin/convert jpg:"/location/20-prefab_woningen.jpg" -auto-orient -shave 0x78 -resize 650x174 -colorspace RGB "location/new.jpg" &&exit
Is this a common bug? I can't find anything on the web about it. ImageMagick version doesn't seem to matter, tried both local and on the server but I get the same results.

resize tries to fit the image into the specified dimensions. It doesn't force it to exactly that size. See the manual.
Use the !flag to tell IM to ignore the aspect ratio.
/usr/bin/convert jpg:"/location/20-prefab_woningen.jpg"
-auto-orient -shave 0x78
-resize 650x174\!
-colorspace RGB "location/new.jpg" &&exit

Related

Cropping an image using Jcrop and Imagemagick

I'm confused on how the -crop function works in Imagemagick.
I have the following values from Jcrop.
(x1,y1), (x2,y2), width and height.
And following command:
exec("convert $target_path -crop ".$w."x".$h."+$x+$y +repage $target_path");
Original image:
Result after crop:
My question is, How do I used the coordinates and dimensions from Jcrop, and use them with Imagemagick?
I have no idea what values you are passing into convert, but your command needs to look something like this to extract the light region -if that is your aim:
convert x.png -crop 240x240+120+100 out.png
The first 240 is the width of the cropped area, and the second 240 is its height. The 120 is the x-offset across from the top-left corner and the +100 is the y-offset down from the top.
Or, in general terms, you specify the crop like this
convert input.png -crop ${x}x${y}+${a}+${b} output.png
I feel like an idiot.
I had style='max-width:500px;' on my image during the crop. Removed the style and now it's working.

How to increase quality of image through imagemagick binary convert?

I'm working in an image conversion using imageMagick binary convert. When i resize a small image into a larger image and also increase the quality of image.
Here's my sample code:
$img = 'old_image.png';
$path1= 'new_img.png';
exec("convert $img -quality 100% -density 600 -resize 2480x3508 -depth 400 $path1");
When i used this command its working fine and it convert large image with loss of quality.
When i need to increase quality by using sharpen 50% code in exec command it doesn't create a proper image and no response in exec command.
$img = 'old_image.png';
$path1= 'new_img.png';
exec("convert $img -sharpen 99% -quality 100% -density 600 -resize 2480x3508 -depth 400 $path1");
Here I'm using image magick binary convert. How to achieve this image quality. Any help would be appreciated.
Think sharpen works like blur and takes an argument of {radius}x{sigma}
What version are you using? Maybe look at some of the docs that correspond to it...
http://www.imagemagick.org/Usage/blur

PHP Imagick (ImageMagick) RGB > CMYK with Flat Black

I'm using PHP Imagick to convert PNG images generated in PhantomJS to TIF CMYK,
for print purposes I need a flat Black (cmyk - 0,0,0,100) - the conversion generates blacks like (cmyk - 58,49,44,89).
I'm converting the images using color profile (section of my code below) -> the code is based on Convert image from RGB to CMYK with Imagick
is it possible to force a flat black with Imagick ? do you know any other tools that might help ?
thanks,
if ($has_icc_profile === false) {
$icc_rgb = file_get_contents( '/srgb_profiles' . '/sRGB.icc');
$image->profileImage('icc', $icc_rgb);
unset($icc_rgb);
}
// then we add an CMYK profile
$icc_cmyk = file_get_contents( '/cmyk_profiles'.'/JapanColor2002Newspaper.icc');
$image->profileImage('icc', $icc_cmyk);
UPDATE :
after checking online I think I'm looking for a UCR en.wikipedia.org/wiki/Under_color_removal method for ImageMagick - I found that convert old versions supported under color removal
-undercolor <undercolor factor>x<black-generation factor>
control undercolor removal and black generation on CMYK images.
This option enables you to perform undercolor removal and black generation on CMYK images-- images to be printed on a four-color printing system. You can con- trol how much cyan, magenta, and yellow to remove from your image and how much black to add to it. The standard undercolor removal is 1.0x1.0. You'll frequently get better results, though, if the percentage of black you add to your image is slightly higher than the percentage of C, M, and Y you remove from it. For example you might try 0.5x0.7. (http://www.chemie.fu-berlin.de/chemnet/use/suppl/imagemagick/www/convert.html) -
apparently the option is not supported anymore, I'm interested if anyone knows if UCR is the solution I'm looking for and if anyone knows if it's supported or if I'm supposed to use a different method to get the same result.
If you use ImageMagick's convert at the command line like this to generate a grayscale ramp, 1 pixel wide and 256 pixels tall, going from white to black and convert it to CMYK colorspace and then show it as text, you get what you want:
convert -size 1x256 'gradient:rgb(255,255,255)-rgb(0,0,0)' -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,256,65535,cmyk
0,0: (0%,0%,0%,0%) #0000000000000000 cmyk(0,0,0,0)
0,1: (0%,0%,0%,0.392157%) #0000000000000101 cmyk(0,0,0,1)
0,2: (0%,0%,0%,0.784314%) #0000000000000202 cmyk(0,0,0,2)
0,3: (0%,0%,0%,1.17647%) #0000000000000303 cmyk(0,0,0,3)
0,4: (0%,0%,0%,1.56863%) #0000000000000404 cmyk(0,0,0,4)
0,5: (0%,0%,0%,1.96078%) #0000000000000505 cmyk(0,0,0,5)
0,6: (0%,0%,0%,2.35294%) #0000000000000606 cmyk(0,0,0,6)
0,7: (0%,0%,0%,2.7451%) #0000000000000707 cmyk(0,0,0,7)
0,8: (0%,0%,0%,3.13725%) #0000000000000808 cmyk(0,0,0,8)
0,9: (0%,0%,0%,3.52941%) #0000000000000909 cmyk(0,0,0,9)
0,10: (0%,0%,0%,3.92157%) #0000000000000A0A cmyk(0,0,0,10)
...
...
0,249: (0%,0%,0%,97.6471%) #000000000000F9F9 cmyk(0,0,0,249)
0,250: (0%,0%,0%,98.0392%) #000000000000FAFA cmyk(0,0,0,250)
0,251: (0%,0%,0%,98.4314%) #000000000000FBFB cmyk(0,0,0,251)
0,252: (0%,0%,0%,98.8235%) #000000000000FCFC cmyk(0,0,0,252)
0,253: (0%,0%,0%,99.2157%) #000000000000FDFD cmyk(0,0,0,253)
0,254: (0%,0%,0%,99.6078%) #000000000000FEFE cmyk(0,0,0,254)
0,255: (0%,0%,0%,100%) #000000000000FFFF cmyk(0,0,0,255)
You must be doing something different - maybe this will help you work it out. I am guessing it is your ICC profiles but you can experiment with the above command.
If you just want to experiment with spot values, you can just have IM translate a single pixel like this:
convert -size 1x1 xc:#000000 -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,1,65535,cmyk
0,0: (0%,0%,0%,100%) #000000000000FFFF cmyk(0,0,0,255)
or maybe more simply like this:
convert -size 1x1 xc:#000000 -depth 8 -colorspace cmyk txt:
# ImageMagick pixel enumeration: 1,1,255,cmyk
0,0: (0,0,0,255) #000000FF cmyk(0,0,0,255)
Note the following though:
You must put profiles between input image and output image names on the command line.
If your image has no embedded profile, the first profile you give is applied to the input image and the second to the output image. If your input image does have a profile, the first profile you give is applied to the output image.

Generate a image thumbnail without stretching it with ImageMagick 6.2.8

Fill Area Flag ('^' flag) is support IM v6.3.8-3But my client's production server has version ImageMagick 6.2.8
Right now in my local server i use this command to generate thumbnail and it works fine:
convert image.jpg -resize "280x210^" -gravity Center -crop "280x210+0+0" thumbnail.jpg
Since my client's production server doesn't support '^' flag how can i generate a thumbnail without using it? (or maybe calculating it manually in PHP or BASH)
Should i use -extent, does it stretch the image?
I also read this and im not sure if ^ flag is for not letting the image stretch because thats what i want, generate a thumbnail without stretching it.
Note: i dont have root access on the server. Im using PHP and BASH to run the commands.
EDIT:
I also don't want any other background colors while resizing and croping.
try
convert image.jpg -background black -resize 280
-gravity center -crop 280x210+0+0 -extent 280x210 image.c.jpg
I found a solution
This is the PHP function i used:
function imgconvert($in,$out,$size){
$size_arr=explode('x',$size);
$resize=( ($size_arr[0]/$size_arr[1]) > 1.775 ? $size_arr[0].'x':'x'.$size_arr[1]);
system("convert \"$in\" -resize $resize -gravity Center -crop \"$size+0+0\" \"$out\"");
}
It seems that if width/height is larger than 1.775 i should use widthX as resize value and if else than i should use Xheight .

Imagemagick Crop command not giving perfect result

I am using Imagemagick for resizing and cropping image.
Test Image :
I need to re-size it for 300 x 320 frame for this first I am resizing the image and then cropping it and i am using the following commands:
exec("convert /uploadImagePath -thumbnail 300 /newImagePath");
exec("convert /newImagePath -gravity Center -crop 290x310+0+0 /newImagePath");
But it gives me following image
As you can see image is not complete. Where am I mistaken?
(Answer is updated, providing an illustrated example for -liquid-rescale further below now)
Your original image's dimensions are:
489 x 640 pixels
Your desired dimensions seem to be:
290 x 310 pixels
This cannot scale to these dimensions without either:
cropping (do not keep all areas of the intial image)
keeping desired width (give up desired height)
keeping desired height (give up desired width)
distortion (do not keep the aspect ratio when scaling)
padding (add more pixels to one or more edges)
removing pixels where it's not obvious ("liquid rescale" or "seam carving" -- see Wikipedia)
Your result shows '1.' (cropping), which you don't like. So you have options '2.' (keeping width), '3.' (keeping height), '4.' (distortion), '5.' (padding) and '6.' (seam carving) left to test.
'2.': Keeping desired Height
convert WPTgp.jpg -resize x310 keep-height.jpg
Resulting Image has dimensions of 237 x 310 pixels.
Keep Height....
(determine width automatically)
'3.': Keeping desired Width
convert WPTgp.jpg -resize 290x keep-width.jpg
Resulting Image has dimensions of 290 x 380 pixels.
Keep Width.....
(determine height automatically)
'4.': Distortion
convert WPTgp.jpg -resize 290x310\! distorted.jpg
Resulting Image has dimensions of 290 x 310 pixels.
Distorted......
(ignore aspect ratio -- distort image if required to fit dimensions)
'5.': Padding
convert WPTgp.jpg \
-resize 290x310 \
-gravity center \
-background orange \
-extent 290x310 \
padded.jpg
Resulting Image has dimensions of 290 x 310 pixels. (Orange background was added only to demonstrate that the 'extention' of the image did work.)
Padded.........
(keep aspect ratio -- extend image for desired dimensions)
'6.': Seam Carving
convert WPTgp.jpg -liquid-rescale 290x310\! liquid.jpg
The above would be the command you'd spontaneously derive from quick-reading the ImageMagick command options reference. However, it doesn't work well, and instead I used:
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 290x310 liquid.jpg
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 48.4% liquid.jpg
Further below is an explanation why I needed to modify it....
Liquid-rescaled
Sorry -- I cannot provide example picture right now; this requires the additional ImageMagick delegate liblqr (liquid rescaling library) to be installed, which I don't have at this moment) I've now had the opportunity to create a 'liquidly rescaled' version of the original image.
Caveats about Seam Carving / '-liquid-rescale':
As stated above, the last image is not the result of my originally proposed command, but of one of these two modified versions:
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 290x310 liquid.jpg
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 48.4% liquid.jpg
Remember, we have an original image of 489x610 pixels, which we are expected to scale to 290x310 pixels. But -liquid-rescale isn't good at rescaling in two dimensions at once -- it's designed to scale into one direction only (horizontal or vertical). If you try to do both at once, results may not be what you'd expect. Here is the result for the originally proposed command:
convert WPTgp.jpg -liquid-rescale 290x310\! liquid.jpg
LQR gone wrong
That's why I came up with the two modified commands which work in two steps:
First, apply liquid rescaling to the horizontal dimension only, expanding the original's width from 489 pixels to 599 pixels.
Second, apply 'normal' aspect-ratio-keeping scaling to the intermediate result to produce the final image.
Try:
$inputFile = "WPTgp.jpg";
exec("convert {$inputFile} -resize 290x310^ -gravity Center -crop 290x310+0+0 picCropped.png");

Categories