Crop image on angle - php

I'm currently looking for a way to crop an image, but on an angle.
I don't think I can just rotate the image first as the script is supplied with specific x,y coordinates of each corner.
So if you can imaging this, image is uploaded, 1280x720.
Along with the image it's supplied with x,x coordinates for the crop zone.
However the top left and top right coordinates will not have the same y position.
Heres an examples
Before
After
Any ideas ?

You'll still need to use trigonometry methods to rotate the image, but you can mimic a crop-at-an-angle by mixing opacity copying and trimming.
First. Create an Image Mask
If all the points are giving to you, and the image size is defined, simply draw the area that needs to be extract
WIDTH=819
HEIGHT=616
TOP_LEFT=669,117
TOP_RIGHT=784,155
BOTTOM_LEFT=544,495
BOTTOM_RIGHT=659,534
convert -size $WIDTHx$HEIGHT xc:black -fill white -stroke white \
-draw "polyline $TOP_LEFT $TOP_RIGHT $BOTTOM_RIGHT $BOTTOM_LEFT" \
mask.png
Masking and Background Removal
This method of masking will turn off the alpha-channel and set the background to transparent. When we compose the two images, the resulting image will only display what's within the area we defined in the mask. (note: you may need to adjust the -background to white, or transparent.)
convert source.jpg mask.png -alpha Off -compose CopyOpacity \
-composite -background transparent copyOpacity.png
Calculate Degree to Rotate
If you have two points on a square angle, you should be able to follow the atan method. Most language will have an atan2 function. Other trigonometry questions "Rotating a rectangle" & "How to calculate the angle between two points relative to the horizontal axis?"
DELTA_Y=$(($HEIGHT-155-534))
DELTA_X=$((784-659))
DEGREE=`awk "BEGIN { pi=4.0*atan2(1.0,1.0)+90; print atan2($DELTA_Y,$DELTA_X)*180/pi; }"`
convert copyOpacity.png -rotate $DEGREE -trim final.png
Luckily, you can do everything in one step.
#!/bin/bash
WIDTH=819
HEIGHT=616
TOP_LEFT=669,117
TOP_RIGHT=784,155
BOTTOM_LEFT=544,495
BOTTOM_RIGHT=659,534
DELTA_Y=$(($HEIGHT-155-534))
DELTA_X=$((784-659))
DEGREE=`awk "BEGIN { pi=4.0*atan2(1.0,1.0)+90; print atan2($DELTA_Y,$DELTA_X)*180/pi; }"`
convert source.jpg \( -size $WIDTHx$HEIGHT xc:black -fill white -stroke white \
-draw "polyline $TOP_LEFT $TOP_RIGHT $BOTTOM_RIGHT $BOTTOM_LEFT" \) \
-alpha Off -compose CopyOpacity -composite \
-background transparent -rotate $DEGREE -trim \
final.png

Related

Reduce image colors based on colorlookup table excluding transparent

I'm trying to reduce the number of colors in the image below using -remap in imagemagick.
olympic-logo.png
colortable.png which consists of this two color A12E64, FF0000
Using the following code:
convert olympic-logo.png +dither -remap colortable.png olympic-logo-remap.png
Output:
olympic-logo-remap.png
Expected Output:
olympic-logo-expected.png
Is there a way to ignore transparent area so It won't get mapped to get the expected output?
Thanks and more power.
You can put a copy of the original image to one side before doing what you already did and then restore the alpha from that afterwards like this:
convert rings.png -write MPR:orig +dither -remap colortable.png MPR:orig -compose copyalpha -composite result.png
where MPR: is a "Memory Program Register", i.e. a named lump of RAM.
Your input image does not have a constant background color. It is mostly black with a large white border. You can see that if you turn alpha off:
convert olympic-logo.png -alpha off aoff.png
So you can modify Mark Setchell's command by adding -background black -alpha background to it.
convert xc:"#A12E64" xc:"#FF0000" +append colortable.png
convert olympic-logo.png -background black -alpha background -write MPR:orig +dither -remap colortable.png MPR:orig -compose copy_opacity -composite result.png
Does this now work for you? If not, try making the background all white.

Extracting certain parts on image with PHP

I would like to be able to extract radar data from gif which occurs every hour.
This is an example of one gif:
Data colors can vary according data legend on the right. I would also like that country borders are not visible, as gif is drown in order: base layer, radar data layer, borders layer - I only need pure radar data.
Any suggestion or solution will be very much appriciated!
All the radar data seems to be highly saturated, so you could extract the saturation onto its own layer and threshold it at, say, 85% and use that as the opacity like this:
convert radar.gif \
\( +clone -colorspace hsl -channel S -separate -threshold 85% \) \
-compose copyopacity -composite result.gif
which gives you this, where all the unsaturated colours are now transparent:
As you can see, it is correctly picking up and retaining all the colours in the "key" on the right, but it also picks up the mauve on the left of the image. Let's make that specific tone transparent:
convert radar.gif \
\( +clone -colorspace hsl -channel S -separate -threshold 85% \) \
-compose copyopacity -composite \
-fuzz 10% -fill none -opaque "rgb(160,153,255)" result.gif
which gives this:
Now, I presume we need to get rid of the sickly yellow too:
convert radar.gif \
\( +clone -colorspace hsl -channel S -separate -threshold 85% \) \
-compose copyopacity -composite \
-fuzz 10% -fill none \
-opaque "rgb(160,153,255)" \
-opaque "rgb(254,251,175)" result.gif
Finally, let's crop to the top left 480x480 px:
convert radar.gif -crop 480x480+0+0 +repage \
\( +clone -colorspace hsl -channel S -separate -threshold 85% \) \
-compose copyopacity -composite \
-fuzz 10% -fill none \
-opaque "rgb(160,153,255)" \
-opaque "rgb(254,251,175)" result.gif
You can use 'imagecreatefromgif' to retrieve the gif and load it as an GD object (requires GD enabled in the php.ini). From there you can use quite a few image manipulation tricks to cut down to what you want from the gif. The PHP.net site has an example on what you can do with it.
First of all, if you are interested just in the information coded by colours, which us present in some specific frame of the gif, read here about how you can access only one frame if that gif:
Accessing gif frames
After that, considering you have a rectangle image, you can scan pixel by pixel, as a matrix, using php function 'imagecolorat'.

JPG -> PNG Release outer background

I do have fowllowing article pics, here as an example a mirror. Of course there can be other geometric forms like bottles, chairs.
The pictures are all in JPEG-format. I want to convert these files to PNG-format. But I want to get rid of the outer white background.
Is there a way to do it by script in php oder ImageMagick?
You can do it like this:
convert frame.jpg -fuzz 10% -fill none -draw 'color 10,10 floodfill' result.png
I have drawn the effect in red here so you can see it.
You can use ImageMagick's -draw to isolate a color, and "floodfill" it to transparency.
convert input.jpg \
-fill transparent \
-fuzz 20% \
-draw 'color 15,15 floodfill' \
out.png
See Color Fill Primitives for other great examples.
For PHP's Imagick library, you would do something like...
$img = new Imagick('input.jpg');
$draw = new ImagickDraw();
$draw->setFillColor('transparent');
$draw->color(15,15, Imagick::PAINT_FLOODFILL);
$img->drawImage($draw);
$img->writeImage('out.png');

Imagemagick clipping out shapes and adding an expanded background

Using imagemagick, I'm looking to add an expanded background color to an image. My source looks like this:
And this is the desired output (the background color will be white, but this is for clarity):
Step 1 seems to be filling the background with my desired color, but I can't find a way to remove the background outside the shape. This will also be a problem if the source image contains any of the desired background color already (because it will be made transparent).
Ideas?
It is not perfect but should do the job:
convert ( in.png -resize 200% -flatten -negate -morphology Dilate Disk:20 \
-fuzz 90% -fill none -draw "matte 0,0 floodfill" -fill green \
-colorize 100% -resize 50% ) in.png -composite out.png

How to make fade edges in image magic?

I want to create faded edges image effect using imagemagic commands. Please help me for a command to make effect as like http://postimg.org/image/h51e4twyp/
Start with Doge
curl -o doge.jpg http://i0.kym-cdn.com/photos/images/newsfeed/000/581/296/c09.jpg
Next, blur the edges.
convert doge.jpg -alpha set -virtual-pixel transparent -channel A -morphology Distance Euclidean:1,20\! +channel doge.png
You will need to output it as a .png
Imagemagick documentation
Edit: The above example is for ImageMagick v6.
For IMv7 try:
convert doge.jpg \( +clone -alpha extract -virtual-pixel black -gamma 2 +level 0,100 -white-threshold 99 -morphology Distance Euclidean:1,200! -sigmoidal-contrast 3,0% \) -compose CopyOpacity -composite doge_im7.png

Categories