"Toon" script problem only with some images

A plethora of command-line scripts that perform geometric transforms, blurs, sharpens, edging, noise removal, and color manipulations.
Post Reply
josecarlosrz19
Posts: 5
Joined: 2017-05-29T11:01:34-07:00
Authentication code: 1151

"Toon" script problem only with some images

Post by josecarlosrz19 »

I clarify I use PHP (exec) and the ImageMagick version I'm using is: 6,7,8,9 (from "LIB_VERSION_NUMBER").

First of all, I save the picture from a URL with PHP. This is ok.

Then, I execute:

Code: Select all

convert picture.jpg -resize 600 picture.jpg
This is ok too, the picture is getting resized successfully.

Then, I execute the code from Toon script (thanks to Fred for these amazing scripts).

With some pictures it is working really great, but with this image: http://i.imgur.com/vg4tzQW.jpg ImageMagick is not saving the file, but I don't think that's Imagemagick fault, because with other images it works well, for example:
But with the picture linked first, the file is not saved at all. That's the problem.

A curious thing is that, in the second step, when I change the resize width to less or equal than 591, it magically works, and I get this:

http://i.imgur.com/LvGVySo.jpg

That's the desired result!

I'm using the method 2 of the script, like this (I use PHP variables):

Code: Select all

"convert {$tmpA1} \( -clone 0 -blur 0x{$blur} \) \( -clone 0 -fill black -colorize 100 \) \( -clone 0 -define convolve:scale='!' -define morphology:compose=Lighten -morphology Convolve  'Sobel:>' -negate -evaluate pow {$gain} -negate -level {$smoothing}x100% \) -delete 0 -compose over -composite {$modulating} picture_new.jpg"
This is the "verbose info" of both images, if it helps:

With IM 6,7,8,9
With IM 7
I ran the script with ImageMagick 7, and it's working there with no problems, but I have to stay with 6,7,8,9 because of the hosting company.

So, why is this happening? And how to avoid it? I could set the resize width to 591, but is that going to work with all images?

Thanks in advance for the help, and thanks to Fred for the scripts.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Toon" script problem only with some images

Post by fmw42 »

I have no problem getting an output file for your 960x720 troublesome image using either ImageMagick 6.9.8.9 or 6.7.8.9 on my Mac OS X Sierra. The image is a bit blurry without changing any other arguments but -m 2 as

Code: Select all

toon -m 2 your image.jpg  result.png
What are the exact arguments you are using!

Why are you cutting out the code and putting it into PHP? You could just run the script from PHP exec()? Perhaps you are not setting some variable properly. Try running my script directly in PHP exec() with fixed numerical arguments. Does that fail?

Are you running my latest version of the script?

If you are having trouble with such an old version as 6.7.8.9, there could be a bug for your OS that I do not understand. Perhaps turn off OpenMP or set the threads to 1. Before trying that try editing the script file and comment out line 259

Code: Select all

trap "rm -rf $dir; exit 1" ERR
as

Code: Select all

#trap "rm -rf $dir; exit 1" ERR
Do you get any error messages?

You can run the modified script as toon2 after editing and saving in PHP exec() as

Code: Select all

<?php
exec("bash path2/toon -m 2 path2/input path2/output 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>
Insert your path2/ as the path to the script and to your input and output images, if they are not all in the same directory where you run the PHP command.
josecarlosrz19
Posts: 5
Joined: 2017-05-29T11:01:34-07:00
Authentication code: 1151

Re: "Toon" script problem only with some images

Post by josecarlosrz19 »

Thanks a lot for your answer. It was really illustrative. We are getting a little closer to the solution.

The reason I rewrote it in PHP is because I use Windows on local machine, but I have the hosting space now that is running on Linux, so it opens a lot of possibilities for the support.

I tried the points you mentioned, here are my results:

I created a folder name "fred" with these files:
  • index.php
  • toon
  • toon2
Ok, first, I am using this PHP code (index.php):

Code: Select all

// Save an image from a URL

$contents = file_get_contents('http://i.imgur.com/vg4tzQW.jpg');
file_put_contents('image.jpg', $contents);

// Resize the image --- NOTE: THIS IS WHAT CAUSES THE PROBLEM

exec("convert image.jpg -resize 600 image_resized.jpg");

// Code from your comment

exec("bash toon2 -m 2 image_resized.jpg image_toon.jpg 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
So there is going to be 3 jpg's in the /fred folder at the end of the script:
  • image.jpg, which is saved from the url provided in the first line
  • image_resized.jpg, the output for the -resize statement
  • image_toon.jpg, the output for the toon command
The problem happens when I resize the image. This is what I get when I DO NOT resize the image:
toon2: line 96: type: toon2: not found
dirname: missing operand
Try 'dirname --help' for more information.
basename: missing operand
Try 'basename --help' for more information.
Ok, some alerts, but nothing fatal. The 3 files are there. All is running excellent.

But when I add "convert -resize 600", I get this:
toon2: line 96: type: toon2: not found
dirname: missing operand
Try 'dirname --help' for more information.
basename: missing operand
Try 'basename --help' for more information.
convert: UnableToAcquireString `Cannot allocate memory' @ fatal/string.c/AcquireString/135.
I remarked the 600 because if, for example, I resize to 500, or even if I don't resize at all, it works normally and the 3 files are created with success, and it's kind of strange.

I'm googling now. In a link I have read the word OpenMP, I'm going to search about it and will come here in a moment.

Also I deleted one of the layers in the script, and left it like this:

Code: Select all

convert $dir/tmpI.mpc \
		\( -clone 0 -blur 0x$blur \) \
		\( -clone 0 -fill black -colorize 100 \) \
		-delete 0 -compose over -composite \
		$modulating \
		"$outfile"
And this is the error I get:
convert: Insufficient memory (case 4) `image_toon.jpg' @ error/jpeg.c/JPEGErrorHandler/316.
Once again, if I remove the -resize line, it will the create the image (only not with the expected result).

I think we are really close now. Thanks for your help!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Toon" script problem only with some images

Post by fmw42 »

I suspect you have a flawed or old version of libjpeg. So when you write a jpg file it may not be a good file. Try resizing but save to png or tif or miff or ppm. Do they work when read as input to my script?

If they work, then the issue is with the JPG written by the resize command. This could be just too old a version of libjpeg or it could be a flaw in ImageMagick. I suspect the former, since I can resize from jpg to jpg and run the script on my IM 6.7.8.9 just fine on the resize 600 jpg.

Do you have libjpeg installed?

What do you get from these typed in a TERMINAL or CMD window.

Code: Select all

convert -list format
What does it say for JPG?

Code: Select all

convert -list configure
What does it say for the line starting with DELEGATES?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: "Toon" script problem only with some images

Post by snibgo »

What is your first error?
josecarlosrz19 wrote:Insufficient memory
This might be a memory limitation imposed by your system. This often happens on servers where, after much head-scratching, it transpires that a system manager has imposed limits on process or accounts.
snibgo's IM pages: im.snibgo.com
josecarlosrz19
Posts: 5
Joined: 2017-05-29T11:01:34-07:00
Authentication code: 1151

Re: "Toon" script problem only with some images

Post by josecarlosrz19 »

fmw42 wrote: 2017-06-09T19:26:58-07:00 What do you get from these typed in a TERMINAL or CMD window.

Code: Select all

convert -list format
What does it say for JPG?

Code: Select all

convert -list configure
What does it say for the line starting with DELEGATES?
This is for JPG/JPEG:

Code: Select all

JPEG* JPEG rw- Joint Photographic Experts Group JFIF format (62)
JPG* JPEG rw- Joint Photographic Experts Group JFIF format (62)
And this is for DELEGATES:

Code: Select all

DELEGATES bzlib fontconfig freetype gs jpeg jng jp2 lzma openexr pango png rsvg tiff x11 xml wmf zlib
snibgo wrote: 2017-06-09T19:28:24-07:00 What is your first error?
When I use "-resize jpg" the error is:

Code: Select all

convert: UnableToAcquireString `Cannot allocate memory' @ fatal/string.c/AcquireString/135.
But when I use "-resize png" as Fred asked, the error is:

Code: Select all

toon: line 307: 976043 Segmentation fault convert $dir/tmpI.mpc \( -clone 0 -blur 0x$blur \) \( -clone 0 -fill black -colorize 100 \) \( -clone 0 -define convolve:scale='!' -define morphology:compose=Lighten -morphology Convolve 'Sobel:>' -negate -evaluate pow $gain -negate -level ${smoothing}x100% \) -delete 0 -compose over -composite $modulating "$outfile"
Thank you both for your help!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: "Toon" script problem only with some images

Post by snibgo »

josecarlosrz19 wrote:UnableToAcquireString `Cannot allocate memory'
You have memory problems. I suggest you talk to your system admin.
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: "Toon" script problem only with some images

Post by fmw42 »

Line 307 in my script is basically then end of my script. It is an end of if condition. The only other line is exit 0, that means all ended well.

I do not know what is wrong. I suspect that you server has limitations as snibgo pointed out. It may be limiting the file size you can use or limiting memory usage.

As I mentioned earlier, it runs fine on IM 6.7.8.9 on my Mac OS X. Since it fails for other formats such as PNG, then I suspect it is not your libjpeg, even though it is old at version 62 --- current versions are 90.

If the script runs fine on smaller JPG files, then it is likely a limitation. You can check this by setting the quality lower on your resize command and see if that allows the script to work. If it does, then it is probably a file size limitation. If it does not, then it may be an image dimension limitation.

convert input.jpg -resize 600 -quality 50 result.jpg

Try different quality values. Your input image before resizing has a quality of 72. Perhaps your old version of IM is not preserving that quality value or setting it to the default of 92, which would give a larger file size.
josecarlosrz19
Posts: 5
Joined: 2017-05-29T11:01:34-07:00
Authentication code: 1151

Re: "Toon" script problem only with some images

Post by josecarlosrz19 »

snibgo wrote: 2017-06-09T20:40:48-07:00 You have memory problems. I suggest you talk to your system admin.
fmw42 wrote: 2017-06-09T20:46:38-07:00 convert input.jpg -resize 600 -quality 50 result.jpg
I tried to -quality 1 and the error persists.

You are right, it's an old ImageMagick version, I will try to talk to the hosting support.

Any things should I ask to them?

Thanks for the help.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Toon" script problem only with some images

Post by fmw42 »

They probably will not want to upgrade. Typically, they are limited to versions of ImageMagick that come bundled with the Linux version they are using. I do not know what they use if the server is Windows.

You could ask if they can install a current version of IM in your own directory, if they cannot upgrade their shared IM.

Also ask them if they have any file size or image dimension limitation for your account or for the whole server.

It is also possible that they could try reinstalling their OS with ImageMagick. Perhaps ImageMagick got corrupted.

Perhaps snibgo will have other suggestions.
josecarlosrz19
Posts: 5
Joined: 2017-05-29T11:01:34-07:00
Authentication code: 1151

Re: "Toon" script problem only with some images

Post by josecarlosrz19 »

fmw42 wrote: 2017-06-09T21:08:56-07:00 They probably will not want to upgrade. Typically, they are limited to versions of ImageMagick that come bundled with the Linux version they are using. I do not know what they use if the server is Windows.

You could ask if they can install a current version of IM in your own directory, if they cannot upgrade their shared IM.

Also ask them if they have any file size or image dimension limitation for your account or for the whole server.

It is also possible that they could try reinstalling their OS with ImageMagick. Perhaps ImageMagick got corrupted.

Perhaps snibgo will have other suggestions.
Ok, I just opened a ticket.

Waiting their response now.

Will notify here too.

Thank you and snibgo for the help.
Post Reply