Coloring page from color picture ?

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
dognose
Posts: 265
Joined: 2005-03-08T22:16:37-07:00

Post by dognose »

The edge effect can be used to create lines..

-blur 0x2 -edge 15 -edge 1

But, it really depends on what kind of picture you have and want to get out.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

You can also try reducing colors to define some extra boundaries.

-

Code: Select all

colorspace Gray  +dither -colors 8 -edge 1 -normalize
Basically first try to form the approriate areas of colors then convert those
areas into edges.

Eg:
-

Code: Select all

segment 1x1 +dither -colors 4 -edge 1 -normalize
However all these methdos may need adjustment depending on the image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

You could modify my example form: http://www.rubblewebs.co.uk/imagemagick/code.php

If you need help contact me
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

I have modified my code for you to try. The outputs need reversing by the looks of things as you want the black areas white and the white areas black ?

I Just commented out the method I was not trying for speed and renamed the outputs for comparison.
Make sure you CHMOD directory to 777 and if you allow anyone to use this code you will need to add some security checks - VERY IMPORTANT

Code: Select all

<?php
// If the form has been submitted do this
if ( $Submit ) { 
// Temporary upload image name
$original_image = $_FILES['filename']['tmp_name'];
// Get the image dimensions
$size=GetImageSize( $original_image );
// Name to save the image as - in this case the same as the original
$new_image = $_FILES['filename']['name'];
// Strip any malicious code from the user input
$new_image = escapeshellarg( $new_image );

// Ran this code first and the result has been renamed to House 1
//exec("/usr/local/bin/convert $original_image colorspace Gray  +dither -colors 8 -edge 1 -normalize $new_image");
// Ran this code second and the result has been renamed to House 2
//exec("/usr/local/bin/convert $original_image segment 1x1 +dither -colors 4 -edge 1 -normalize $new_image");

echo "File uploaded";

}
else { ?>

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<input type="file" name="filename"  />
<input type="Submit" name="Submit" value="Submit" />
</form>
 <?php } ?> 
The images are :
Original image - http://www.rubblewebs.co.uk/imagemagick/House.jpg
House1 - http://www.rubblewebs.co.uk/imagemagick/House1.jpg
House2 - http://www.rubblewebs.co.uk/imagemagick/House2.jpg
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

I am having a problem with that cocktail22. I have tried another example on the froum and it does not work. I have also tried some other ideas about making the black transparent but that failed as well.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

Its simple when you read the instructions !!!

All you need to do now is optimise the method and code !

Code: Select all

exec("/usr/local/bin/convert House6.jpg -negate House9.jpg");
http://www.rubblewebs.co.uk/imagemagick/House9.jpg
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

Try this code now:

Code: Select all

<?php 
// If the form has been submitted do this 
if ( $Submit ) { 
// Temporary upload image name 
$original_image = $_FILES['filename']['tmp_name']; 
// Get the image dimensions 
$size=GetImageSize( $original_image ); 
// Name to save the image as - in this case the same as the original 
$new_image = $_FILES['filename']['name']; 
// Strip any malicious code from the user input 
$new_image = escapeshellarg( $new_image ); 

exec("/usr/local/bin/convert $original_image segment 1x1 +dither -colors 2 -edge 1 -normalize $new_image"); 
exec("/usr/local/bin/convert $new_image -negate $new_image_1");
unlink($new_image);

echo "File uploaded"; 

} 
else { ?> 

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<input type="file" name="filename"  /> 
<input type="Submit" name="Submit" value="Submit" /> 
</form> 
 <?php } ?> 
There must be a better way to combine this into one exec statement.

I need a beer now :?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

Code: Select all

<?php 
// If the form has been submitted do this 
if ( $Submit ) { 
// Temporary upload image name 
$original_image = $_FILES['filename']['tmp_name']; 
// Get the image dimensions 
$size=GetImageSize( $original_image ); 
// Name to save the image as - in this case the same as the original 
$new_image = $_FILES['filename']['name']; 
// Strip any malicious code from the user input 
$new_image = escapeshellarg( $new_image ); 

exec("/usr/local/bin/convert $original_image segment 1x1 +dither -colors 2 -edge 1 -normalize -negate $new_image"); 

echo "File uploaded"; 

} 
else { ?> 

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<input type="file" name="filename"  /> 
<input type="Submit" name="Submit" value="Submit" /> 
</form> 
 <?php } ?> 
Looks like you can put -negate on the same line and it works OK.

Getting my beer now
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

Imagemagick never seems to give an error it just does not work. There is an option to output an error but that never works for me either.

I have just tried my code again and a bit of my security checking is causing an error sometimes so here is the code with that removed.

Code: Select all

<?php 
// If the form has been submitted do this 
if ( $Submit ) { 
// Temporary upload image name 
$original_image = $_FILES['filename']['tmp_name']; 
// Get the image dimensions 
$size=GetImageSize( $original_image ); 
// Name to save the image as - in this case the same as the original 
$new_image = $_FILES['filename']['name']; 

exec("/usr/local/bin/convert $original_image segment 1x1 +dither -colors 2 -edge 1 -normalize -negate $new_image"); 

echo "File uploaded";
echo "<img src=\"".$new_image."\">";

} 
else { ?> 

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<input type="file" name="filename"  /> 
<input type="Submit" name="Submit" value="Submit" /> 
</form> 
 <?php } ?> 
Things required with ImageMagick are:
Directory CHMOD to 777 - You have so thats OK
Path to ImageMagick. You will need to check this with your host. Mine is /usr/local/bin/ so convert becomes /usr/local/bin/convert
Version - I am using version 6.2.8
You need to keep the exec code all on one line, if you put an end of line in ( rather than line wrap ) you will need to add a \
e.g.
All on one line
exec("/usr/local/bin/convert filename_path segment 1x1 +dither -colors 2 -edge 1 -normalize -negate file_save_path");

Carried over 2 lines
exec("/usr/local/bin/convert filename_path segment 1x1 +dither -colors \
2 -edge 1 -normalize -negate file_save_path");

Does any ImageMagic work ? Try hard coding a file and running that e.g.

Code: Select all

<?php
exec("/usr/local/bin/convert filename_path segment 1x1 +dither -colors 2 -edge 1 -normalize -negate file_save_path");
?>
<img src="file_save_path">
I do not know how to change the grey to white that is comming from the -negate.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

You do realise that your IM command call is going bad!

You are forgetting a '-' infront of segment, meaning that operator is not being applied.

But all in all this is a great little IM cookbook recipe.
I'll be adding it to the new photo modification page in IM examples
http://www.cit.gu.edu.au/~anthony/graph ... k6/photos/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

Thats strange cocktail22 !

I have added some echo comands to the code to see what is being sent - or not sent. Give this a go.

Code: Select all

<?php 
// If the form has been submitted do this 
if ( $Submit ) { 

// Display what is being posted
echo "<br> filename = ".$filename;
echo "<br> HTTP_POST_VARS filename = ".$HTTP_POST_VARS["filename"];
echo "<br> _POST filename = ".$_POST["filename"];

// Temporary upload image name 
$original_image = $_FILES['filename']['tmp_name'];
// Display what original_image contains
echo "<br> original_image = ".$original_image;

// Get the image dimensions 
$size=GetImageSize( $original_image ); 
// Name to save the image as - in this case the same as the original 
$new_image = $_FILES['filename']['name']; 
echo "<br> new_image = ".$new_image;

exec("/usr/local/bin/convert $original_image -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate $new_image"); 

echo "File uploaded";
echo "<img src=\"".$new_image."\">";

} 
else { ?> 

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<input type="file" name="filename"  /> 
<input type="Submit" name="Submit" value="Submit" /> 
</form> 
 <?php } ?> 
I suppose it depends on how the server is setup; I would have thought if the form was submitting OK and the code was running and the image was not uploaded you would be getting an error on the // Get the image dimensions
$size=GetImageSize( $original_image ); line.

At least the ImageMagick code is working :shock:

anthony - I think it is good to have a digital photo cookbook as quite a lot of people seem to be using ImageMagick for digital photos in one way or another. Your example looks to output a better image than the one I have been using although the first time I tried it out it didn't negate the image ! I have also tried a gif image http://www.rubblewebs.co.uk/imagemagick/test_piglet.gif
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Of course a technique for a real world image will be very different to a technique for a cartoon like image with black outline borders.

Piglet is a cartoon image, not a real world one. It has limited colors.

The problem is that the the existing black outlines are twining under the edge detection.

Edge twining for black lines can be removed by negatinging the image before the edge detection. (so edges expand into the black, instead of out of). Edge detection is afer all designed for white on black images (IE mathematicaly relevent data is white on black).

Code: Select all

   convert piglet.gif -segment 1x1 +dither -colors 2 \
               -negate -edge 1 -normalize -negate -despeckle \
               -colorspace gray x:
For a properly outlined cartoon you can also just try to remove the colors
in the image, to just leave black. EG threshold, or even

Code: Select all

   convert piglet.gif -background white -flatten \
              -colorspace gray -normalize -sigmoidal-contrast 10x0% \
              x:
before appling the edge detect for cartoom with black outlines.

The problem with this method is that piglets black outline is highly variable in width and does not completely outline the color areas. That is it is 'patchy'.

Another method I found for this specific image was

Code: Select all

  convert piglet.gif -fill white -fuzz 10% -opaque black \
             -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate \
             -despeckle x:
However some -blur can improve line widths, but with a loss of internal lines.

Code: Select all

   convert piglet.gif -fill white -fuzz 10% -opaque black -blur 0x1 \
              -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate \
              -despeckle x:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

My code was for two types of modifications. The first was to prevent black lines from 'twinning' into two seperate lines, by negating the image before applying the edge detection. The second was to just try and extract just the black elements (image outlines), this was not too successfull, with the 'piglet' image from rubbleworks, which I now use a cropped version on the IM example pages.

With regard to the 'shrink and crop to fill a box'.

A little off the subject, but as long as the -resize uses a '<' then it should only shrink images, never enlarge. Actually you may be better just getting the dimensions of your original image, then resize and crop as appropriate
with the correct amounts to fill the desired space.

That way you can ensure images will not enlarge if they are already too small.

The center crop will also only trim images never add extra space to an image (unless you make it a viewport crop with a '!" flag, but that only adds virtual canvas space, not real image space.

I recomend some test images of, very small, very large, long and thin (a line), and tall (page borders), and make sure it does what you want.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Post by Bonzo »

Sorry I have only just seen your post cocktail22.

This bit of code should work OK but you will probably need to split it into 2 pages like you did with the last bit !

Code: Select all

<?php
// If the form has been submitted do this
if ( $Submit ) {  
// Temporary upload image name
$original_image = $_FILES['filename']['tmp_name'];
// Get the image dimensions
$size=GetImageSize( $original_image );
// Temp image
$temp_image = 'temp_'.$_FILES['filename']['name'];
// Name to save the image as - in this case the same as the original
$new_image = $_FILES['filename']['name'];

if ( $size[0] > 600 || $size[1] > 794 ){
exec("/usr/local/bin/convert -size {$size[0]}x{$size[1]} $original_image -thumbnail 600x794 $temp_image");
exec("/usr/local/bin/convert $temp_image -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate -despeckle $new_image"); 
unlink($temp_image);
}

else {
exec("/usr/local/bin/convert $original_image -segment 1x1 +dither -colors 2 -edge 1 -normalize -negate -despeckle $new_image"); 
}
echo "File uploaded";
}
else { ?>
<p>File to upload:</p>
<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<input type="file" name="filename"  />
<input type="Submit" name="Submit" value="Submit" />
</form>
<?php } 
?>
There was something funny going on with my PC as I ran the code on 3 different images and then downloaded the folder to my PC. When I looked at the images the small image was always full colour in the thumbnail !

Anyway I hope this sorts your final problem out.

P.S. The temp colored file uploaded should only be in the memory? But to delete any file use unlink('file_name'); or unlink($file_name);
steve0

Re: Coloring page from color picture ?

Post by steve0 »

Useful info!Thank you for share it!
Post Reply