I'm working in php, and going through each image pixel-by-pixel to get an average brightness for each image is going to be way to cpu intensive...
I've looked through both GD and imagemagick docs, but haven't found a way to return the average brightness of an image... Can this be done quickly either in these libraries, or in another package easily accessible by php?
Here is an interesting post using ImageMagick for computing the average graylevel of an image. This post also discusses Mark Ransom's technique of size reduction to 1x1 using ImageMagick.
In Imagemagick command line, you can convert to HSI or LAB and get the brightness (Intensity or Luminosity) from the average of the I or L channel. Any of these methods should work. Note that -scale 1x1 does a simple average of the whole image/channel and saves that value in 1 pixel result. -scale is very fast. It is not like -resize, which uses a specific filter function. Alternately, you can just compute the mean of the image without writing to 1 pixel.
convert image -colorspace HSI -channel b -separate +channel -scale 1x1 -format "%[fx:100*u]\n" info:
convert image -colorspace LAB -channel r -separate +channel -scale 1x1 -format "%[fx:100*u]\n" info:
convert image -colorspace HSI -channel b -separate +channel -format "%[fx:100*u.mean]\n" info:
convert image -colorspace LAB -channel r -separate +channel -format "%[fx:100*u.mean]\n" info:
convert image -colorspace HSI -channel b -separate +channel -format "%[mean]\n" info:
convert image -colorspace LAB -channel r -separate +channel -format "%[mean]\n" info:
The result will be between 0 and 100% with 0 being black and 100 white for all but the last two, where fx range is between 0 and 1. Thus the 100 factor to get percent. For the last two commands, the values will be between 0-255 for Q8 install and 0-65535 for Q16 install.
Note that channels are labeled in order as if they were r,g,b. But for modern versions of Imagemagick, you can use 0,1,2.
Alternately, you can get the pixel color for the channel which will be some gray value:
convert image -colorspace HSI -channel b -separate +channel -scale 1x1 -format "%[pixel:u.p{0,0}]\n" info:
convert image -colorspace LAB -channel r -separate +channel -scale 1x1 -format "%[pixel:u.p{0,0}]\n" info:
Sorry I do not know Imagick, but see
http://us3.php.net/manual/en/imagick.scaleimage.php
http://us3.php.net/manual/en/imagick.getimagepixelcolor.php
http://us3.php.net/manual/en/imagick.transformimagecolorspace.php
http://us3.php.net/manual/en/imagick.getimagechannelstatistics.php
or possibly
http://us3.php.net/manual/en/imagick.getimageproperty.php
Perhaps an Imagick expert would be kind enough to convert one of these commands from command line to Imagick code.
Sample? Just pick 10% of random pixels instead of 100%... Error rate will rise obviously but 10% of the pixels seems fine to me, in most cases it should yield great results!
Cache the values if using them more then once, because this is not a fast solution. I tried first to resize the image to 1x1 pixel with imagick, but the results were not good. Best results I got without imagick resize, but its very slow with big images. The example resizes to 1000x1000 pixels. Keep in mind this example does not cover images with alpha channel.
function getImageBrightness( $path )
{
$width = 1000;
$height = 1000;
try
{
$imagick = new imagick( $path );
$imagick->resizeImage( $width, $height );
$_brightness = 0;
for( $i=0; $i < $width; $i++ )
for( $j=0; $j < $height; $j++ )
if( $pixel = $imagick->getImagePixelColor($i, $j) )
if( $colors = $pixel->getColor() AND isset($colors['r']) )
{
$brightness = ($colors['r'] + $colors['g'] + $colors['b']) / (3* 255);
$_brightness = $brightness + $_brightness;
}
$_brightness = $_brightness / ( $height * $width );
return $_brightness; // from 0 (black) to 1 (white)
} catch( ImagickException $e )
{}
return 0.5; // default
}
Imagick has a histogram feature to return colors by count. You can more quickly average the same information without processing pixel-by-pixel.
Performance will be dependent on the number of colors in the image/histogram (e.g. a single color image will be extremely fast).
<?php
$image = new Imagick('image.jpg');
$pixels = $image->getImageHistogram(); // Generate histogram of colors
$sumbright = 0; // sum of brightness values
$cntbright = 0; // count of pixels
foreach($pixels as $p){
$color = $p->getColor(); // Get rbg pixel color
$cnt = $p->getColorCount(); // Get number of pixels with above color
$sumbright += (($color['r'] + $color['g'] + $color['b']) / (3*255)) * $cnt; // Calculate 0.0 - 1.0 value of brightness (0=rgb[0] 1=rgb[255])
$cntbright += $cnt;
}
$avgbright = $sumbright / $cntbright; // Average 0-1 brightness of all pixels in the histogram
?>
Related
I want to make a part of an image transparent, I tried the code below, even tried many constants as COMPOSITE_DSTOUT, but all didn't work, does anyone know how to?
$fooImage->newImage(256, 256, new ImagickPixel('transparent'));
$Image->compositeImage($fooImage, Imagick::COMPOSITE_DSTOUT, $offsetX, offsetY);
I tested the code below, just got yellow with black, not transparent:
$width = 256;
$height = 256;
$image = new Imagick();
$image->newImage($width, $height, new ImagickPixel('yellow'));
$x = 50;
$y = 100;
$fooWidth = 100;
$fooHeight = 60;
//Create a new transparent image of the same size
$mask = new Imagick();
$mask->newImage($width, $height, new ImagickPixel('none'));
$mask->setImageFormat('png');
//Draw onto the new image the areas you want to be transparent in the original
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->rectangle($x, $y, $x + $fooWidth, $y + $fooHeight);
$mask->drawImage($draw);
//Composite the images
$image->compositeImage($mask, Imagick::COMPOSITE_DSTOUT, 0, 0,
Imagick::CHANNEL_ALPHA);
$image->setImageFormat('png');
$image->writeImage($path);
Got black inside yellow, not transparent in yellow
You need to make a black and white mask image the size of your input (white where you want it to be opaque and black where you want it to be transparent). Then use the equivalent of -compose copyopacity -composite to put the mask into the alpha channel of the image. Sorry, I do not code Imagick.
Here is an example using ImageMagick command line syntax:
Input:
convert logo.jpg \( -size 640x480 xc:white -size 200x200 xc:black -geometry +200+100 -compose over -composite \) +geometry -alpha off -compose copy_opacity -composite result.png
You can see it is transparent by compositing it over another image (in this case a checkerboard).
convert ( -size 640x480 pattern:checkerboard ) result.png -compose over -composite result2.jpg
Do you try \Imagick::COMPOSITE_COPYOPACITY ?
Because that's probably the right one.
I need to create square images with no image loss. I found a tool that does the job as a bash script using ImageMagick but can never seem to be able to do it with php Imagick.
The script I found is called squareup from http://www.fmwconcepts.com/imagemagick/squareup/index.php
My code looks like this currently:
$image = new Imagick($srcimage);
$image->setCompressionQuality(100);
if ($image->getImageHeight() <= $image->getImageWidth())
$image->resizeImage($maxsize, 0, Imagick::FILTER_MITCHELL, 1);
else
$image->resizeImage(0, $maxsize, Imagick::FILTER_MITCHELL, 1);
$h=$image->getImageHeight();
$w=$image->getimagewidth();
$hlarge=0;
$wlarge=0;
if ($w>$h) {
$diff=intval(($w-$h)/2);
$wlarge=1;
$h=$w;
} else {
$diff=intval(($h-$w)/2);
$w=$h;
$hlarge=1;
}
$newimage = new Imagick();
if ($image->getImageColorspace() == Imagick::COLORSPACE_CMYK) {
$fg="cmyk(0,0,0,0)";
$fg_pixel=new ImagickPixel($fg);
$newimage->newImage($w, $h, $fg_pixel);
$newimage->setImageColorspace(Imagick::COLORSPACE_CMYK);
} else {
$newimage->newImage($w, $h, new ImagickPixel('#ffffff'));
}
$newimage->compositeImage($image,\Imagick::COMPOSITE_OVER,0,0);
$newimage->setImageCompression(Imagick::COMPRESSION_JPEG);
$newimage->setImageCompressionQuality(100);
$newimage->stripImage();
$newimage->writeImage($contactimage);
$newimage->destroy();
$image->destroy();
The simplest way to do pad to square or crop to square in ImageMagick 6 is as follows:
Input:
size=`convert hatching_orig.jpg -format "%[fx:max(w,h)]" info:`
convert hatching_orig.jpg -background red -gravity center -extent ${size}x${size} hatching_pad.jpg
size=`convert hatching_orig.jpg -format "%[fx:min(w,h)]" info:`
convert hatching_orig.jpg -background red -gravity center -extent ${size}x${size} hatching_crop.jpg
Same command, but different size variable.
In IM 7, you can do each in one command line.
These commands should be easy to convert to Imagick, I would expect. But should be done in sRGB colorspace. See http://us3.php.net/manual/en/imagick.extentimage.php
Im struggling to resize an image regardless of orientation using imagemagick, is the program that terrible that a basic detection method is not even included? Do I have to now go and check the shortest side using getimagesize and comparing them or what?
-resize 125^
only works one way, the minute i flip the image 90 degrees the shorter side becomes smaller than 125.. ie its not resizing the shorter side on both orientations. Im trying to use less code if that makes a difference.
here is a function ive just completed to force an exact pixel size - I cant guarantee it 100% but ive tested it with many options and got perfect results so far, it gives the closest result imo. First it resizes the SMALLEST difference between the source image and specified sizes by calculating ratios. Then trims off excess pixels. I have compensated for odd numbers, negative values, etc. I have had good results so far. Please let me know if ive missed something or if it breaks somehow:
PHP:
// set source/export paths and pixel sizes for final sizes
$src="path/to/source.jpg";
$exp="path/to/output.jpg";
$crop_w=300;
$crop_h=200;
$size = getimagesize("$src");
//check image sizes
if( ($size[0] < $crop_w) || ($size[1] < $crop_h) ){
echo 'Image not big enough to crop';
exit();
}
//get differential ratios of image vs crop sizes -
//smaller ratio must be resized
$ratio_w = $size[0]/$crop_w;
$ratio_h = $size[1]/$crop_h;
//square or landscape - shave sides
if($ratio_w >= $ratio_h){
//resize width / shave top&bottom
exec("convert $src -resize x".$crop_h." $exp ");
$size = getimagesize("$exp");
$diff=abs($crop_w-$size[1]);
//dividing 1 by 2 will leave a zero on round down - just force resize
if($diff < 2){
// skip shave - diff too small
exec('convert $exp -resize '.$crop_w.'X! $exp ');
}
else{
//divide difference by 2 for shave amount
$shave = round($diff/2,0,PHP_ROUND_HALF_DOWN); //halve & round difference down to avoid cropping smaller
exec('convert '.$exp.' -shave '.$shave.'x0 '.$exp.' '); //shave sides
//odd $diff leave a rounded down pixel - force height resize
if($diff%2 !==0){//if $diff was not divisible by two then 1 pixel is left from round down
exec('convert '.$exp.' -resize '.$crop_w.'x! '.$exp.' ');
}
}
}
//portrait - shave height
else{
//resize width / shave top&bottom
exec("convert $src -resize ".$crop_w."x $exp ");
$size = getimagesize("$exp");
$diff=abs($crop_h-$size[1]);
//dividing 1 by 2 will leave a zero on round down - just force resize
if($diff < 2){
exec('convert $exp -resize x'.$crop_h.'! $exp ');
}
else{
//divide difference by 2 for shave amount
$shave = round($diff/2,0,PHP_ROUND_HALF_DOWN); //halve & round difference down to avoid cropping smaller
exec('convert '.$exp.' -shave 0x'.$shave.' '.$exp.' '); //shave sides
//odd $diff leave a rounded down pixel - force height resize
if($diff%2 !==0){//if $diff was not divisible by two then 1 pixel is left from round down
exec('convert '.$exp.' -resize x'.$crop_h.'! '.$exp.' ');
}
}
}
Feel free to use / make comments. Php 5.4<, Imagemagick 6.8.8.1, Windows xampp.
I need to the re size and crop image to center. The end result image needs to be 250W x 330H.
I need to re size the image uploaded to 330 height but leave the correct ratio with the width. Then check to see if width is 250 or over, after the re size. If it is not, then I need to re size the image from the original to 250 width but leave the correct ratio with the height.
So if it did re size to 330 height and the width was 250 or over, then I need to crop the image to the center on width to 250. But if it re sized to 250 width, with height being 330 or over, then I need to crop image to the center on height to 330.
I was trying to create it myself but I'm so confused by the crop to center part.
With the use of Wideimage library (http://wideimage.sourceforge.net/):
$thumb = WideImage::load('uploaded_image.png')->resize(250, 330);
if ($thumb->getWidth() > 250 || $thumb->getHeight() > 330) {
$thumb = $thumb->crop('center', 'center', 250, 330);
}
$thumb->saveToFile('cropped_image.png');
I wrote a library to can do just that: Php Image Magician
<?php
require_once('../php_image_magician.php');
$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(250, 330, 'crop');
$magicianObj -> saveImage('racecar_cropped.jpg', 100);
?>
here is a function ive just completed to force an exact pixel size - I cant guarantee it 100% but ive tested it with many options and got perfect results so far, it gives the closest result imo. First it resizes the SMALLEST difference between the source image and specified sizes by calculating ratios. Then trims off excess pixels. I have compensated for odd numbers, negative values, etc. I have had good results so far. Please let me know if ive missed something or if it breaks somehow:
PHP:
// set source/export paths and pixel sizes for final sizes
$src="path/to/source.jpg";
$exp="path/to/output.jpg";
$crop_w=300;
$crop_h=200;
$size = getimagesize("$src");
//check image sizes
if( ($size[0] < $crop_w) || ($size[1] < $crop_h) ){
echo 'Image not big enough to crop';
exit();
}
//get differential ratios of image vs crop sizes -
//smaller ratio must be resized
$ratio_w = $size[0]/$crop_w;
$ratio_h = $size[1]/$crop_h;
//square or landscape - shave sides
if($ratio_w >= $ratio_h){
//resize width / shave top&bottom
exec("convert $src -resize x".$crop_h." $exp ");
$size = getimagesize("$exp");
$diff=abs($crop_w-$size[1]);
//dividing 1 by 2 will leave a zero on round down - just force resize
if($diff < 2){
// skip shave - diff too small
exec('convert $exp -resize '.$crop_h.'X! $exp ');
}
else{
//divide difference by 2 for shave amount
$shave = round($diff/2,0,PHP_ROUND_HALF_DOWN); //halve & round difference down to avoid cropping smaller
exec('convert '.$exp.' -shave '.$shave.'x0 '.$exp.' '); //shave sides
//odd $diff leave a rounded down pixel - force height resize
if($diff%2 !==0){//if $diff was not divisible by two then 1 pixel is left from round down
exec('convert '.$exp.' -resize '.$crop_w.'x! '.$exp.' ');
}
}
}
//portrait - shave height
else{
//resize width / shave top&bottom
exec("convert $src -resize ".$crop_w."x $exp ");
$size = getimagesize("$exp");
$diff=abs($crop_h-$size[1]);
//dividing 1 by 2 will leave a zero on round down - just force resize
if($diff < 2){
exec('convert $exp -resize x'.$crop_h.'! $exp ');
}
else{
//divide difference by 2 for shave amount
$shave = round($diff/2,0,PHP_ROUND_HALF_DOWN); //halve & round difference down to avoid cropping smaller
exec('convert '.$exp.' -shave 0x'.$shave.' '.$exp.' '); //shave sides
//odd $diff leave a rounded down pixel - force height resize
if($diff%2 !==0){//if $diff was not divisible by two then 1 pixel is left from round down
exec('convert '.$exp.' -resize x'.$crop_h.'! '.$exp.' ');
}
}
}
Feel free to use / make comments. Php 5.4<, Imagemagick 6.8.8.1, Windows xampp.
Well how could I change the before image to the after image by using imagemagick?
Is it the -skew command or the -distort, and how can I use it preferably in typo3 and php?
Any help is appreciated!
Using Imagemagick with php and the command line:
// Working on the original image size of 400 x 300
$cmd = "before.jpg -matte -virtual-pixel transparent".
" +distort Perspective \"0,0 0,0 400,0 400,22 400,300 400,320 0,300 0,300 \" ";
exec("convert $cmd perspective.png");
Note:
1/ This is for later versions of Imagemagick - the perspective operator has change.
2/ You need to use +distort not -distort as the image is larger than the initial image boundrys.
Examples of Imagemagick with php usage on my site http://www.rubblewebs.co.uk/imagemagick/operator.php
I think what you're looking for is the Imagick::shearImage function. This creates a checkerboard square and distorts it into a parallelogram (save this as a PHP file and open in your browser to see):
<?php
$im = new Imagick();
$im->newPseudoImage(300, 300, "pattern:checkerboard");
$im->setImageFormat('png');
$im->shearImage("transparent", 0, 10);
header("Content-Type: image/png");
echo $im;
?>
Within a larger script, to shear an image named myimg.png and save it as myimg-sheared.png, you can use:
$im = new Imagick("myimg.png");
$im->shearImage("transparent", 0, 10);
$im->writeImage("myimg_sheared.png");
If shearImage isn't versatile enough, you can try the Imagick::DISTORTION_PERSPECTIVE method via the Imagick::distortImage function.
Perspective distortion should give you what you want. Example:
convert original.png -matte -virtual-pixel white +distort Perspective '0,0,0,0 0,100,0,100 100,100,90,110 100,0,90,5' distorted.png
In TYPO3 you could apply it by (ab)using the SCALE object of the GIFBUILDER. Example:
temp.example = IMAGE
temp.example {
file = GIFBUILDER
file {
format = jpg
quality = 100
maxWidth = 9999
maxHeight = 9999
XY = [10.w],[10.h]
10 = IMAGE
10.file = fileadmin/original.png
20 = SCALE
20 {
params = -matte -virtual-pixel white +distort Perspective '0,0,0,0 0,100,0,100 100,100,90,110 100,0,90,5'
}
}
}