Why use point, the processing speed is so slowly

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
webwudi
Posts: 4
Joined: 2014-08-19T21:11:28-07:00
Authentication code: 6789

Why use point, the processing speed is so slowly

Post by webwudi »

i use imagick function point in PHP write a code to operate pixel to change image brightness.
but the processing speed surprisingly slow ! Need several minutes to estimate ! Just like the browser collapsed !

but if use imagick to resize image or add Text on image ... and so on
these common functions are very quickly !

i don't know why ? please help me !!!!!!!!!!!!!!
----------------------------------------------------------------------------------------------------------------------
<?php
$im = new Imagick($src);
$imgSize = $im->getImageGeometry();
$w = $imgSize["width"];
$h = $imgSize["height"];

$image = new Imagick($src);
$image->newImage($w, $h, "transparent");
$draw = new ImagickDraw();

for($y=0; $y<50; $y++){
for($x=0; $x<$w; $x++) {
$p = $im->getImagePixelColor($x, $y);
//i deleted the code for change brightness for the code looks shortly
$draw->setFillColor($p);
$draw->point($x, $y); //use point to redraw image Pixel
}
}

$image->drawImage($draw);

$image->setImageFormat('jpeg');
header('Content-type: image/jpeg');
echo $image;
destroy($image);
?>
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Why use point, the processing speed is so slowly

Post by snibgo »

Using any interpreted language to loop through all the pixels, one by one, will be slow. For interpreted languages, always use a higher-level process that acts on the entire image in a single command.

If you must do processing pixel-by-pixel, use a compiled language.
snibgo's IM pages: im.snibgo.com
webwudi
Posts: 4
Joined: 2014-08-19T21:11:28-07:00
Authentication code: 6789

Re: Why use point, the processing speed is so slowly

Post by webwudi »

snibgo wrote:If you must do processing pixel-by-pixel, use a compiled language.
Thanks snibgo :) I have a little understand
but my programming technology is little superficial hehe

so how should I do in the PHP procedure ,
is use of PHP function to call ImagickMagick command line In order to realize this goal ? ????

can U give some suggestions ?
thanks thanks thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Why use point, the processing speed is so slowly

Post by fmw42 »

I am not much of a programmer, but it looks like all you are doing is cropping a subsection from an image and making it into a new image.

see
http://us3.php.net/manual/en/imagick.cropimage.php
webwudi
Posts: 4
Joined: 2014-08-19T21:11:28-07:00
Authentication code: 6789

Re: Why use point, the processing speed is so slowly

Post by webwudi »

fmw42 wrote:to fmw42 Not the corpImage
My thoughts was pixel-to-pixel rewrite a image ,
Although it looks like the corpImage , but it could change any pixels color !

snibgo tould : use a compiled language. i think he is right ! hehe
i just don't know how to use in PHP code ??
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Why use point, the processing speed is so slowly

Post by fmw42 »

The whole point of Imagemagick is to avoid your having to process pixel-by-pixel on your own. Imagemagick has compiled code to do most of the things you want to do to process images. So they take care of the pixel-to-pixel processing. You just have to find the right method to do what you want.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Why use point, the processing speed is so slowly

Post by snibgo »

I think webwudi wants to apply change the brightness of pixels within a certain rectangle (the entire image width, but just the first 50 lines). For example, at the command line (Windows BAT syntax):

Code: Select all

convert ^
  in.png ^
  -region x50 ^
  -brightness-contrast 30 ^
  out.png
I don't know how to do this in PHP.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Why use point, the processing speed is so slowly

Post by fmw42 »

As far as I know (though I am not an Imagick expert), Imagick has no equivalent to -region or -brightness-contrast. However, the something like the latter can be achieved by using either http://us3.php.net/manual/en/imagick.levelimage.php or http://us3.php.net/manual/en/imagick.si ... timage.php. To work on a subsection, one would have to use http://us3.php.net/manual/en/imagick.cropimage.php, then use one either levelImage or sigmoidalcontrastImage to make the brightness contrast adjustments, the composite the modified subsection back into the original to make the output using http://us3.php.net/manual/en/imagick.compositeimage.php

Note that Imagick is not as well supported, nor as fully functional as Imagemagick. So you may want to just use PHP exec() to run Imagemagick commands such as the one provided by user snibgo.
webwudi
Posts: 4
Joined: 2014-08-19T21:11:28-07:00
Authentication code: 6789

Re: Why use point, the processing speed is so slowly

Post by webwudi »

snibgo wrote:to snibgo :
ThankS although I know this comand !~

perhaps my thoughts was strange,
But if U think :
if U don't want change the whole image brightness but just only want change a local of the image brightness !!!

In this case, you cannot use a simple command to realize it
:?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Why use point, the processing speed is so slowly

Post by fmw42 »

snibgo's command allows you to change the brightness only is a small rectangular region. Imagemagick has other masking operations to do it in an irregular region.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Why use point, the processing speed is so slowly

Post by snibgo »

As Fred says, use a mask. Examples, where m.png is black/white mask:

Code: Select all

convert ^
  in.png ^
  -mask m.png ^
  -brightness-contrast 50 ^
  +mask ^
  bm.png

convert ^
  in.png ^
  -mask m.png ^
  +level 90%%,100%% ^
  +mask ^
  bm2.png

convert ^
  in.png ^
  ( +clone ^
    +level 90%%,100%% ^
  ) ^
  +swap ^
  m.png ^
  -composite ^
  bm3.png
snibgo's IM pages: im.snibgo.com
Post Reply