ImageMagick copy the iOS 7 blur effect - ios7

I have an image that I want to blur like on the iOS 7, see image below:
I'm not sure what combination of transformations I need to execute to get the same result. I tried something very basic so far (not sure what I'm doing), but the result isn't here:
convert {$filename} -filter Gaussian -define filter:sigma=2.5 \
-blur 0x40 {$newFilename}
The above code gets executed by php exec function.

If I take this as background.png
and a plain grey rgb(200,200,200) image with a couple of black and white bits and pieces on it, as foreground.png since I don't have any iPhone grabs of the slide-up menu thingy
convert background.png \
\( +clone -gravity south -crop 360x450+0+0 \
-filter Gaussian -define filter:sigma-2.5 -blur 0x40 \) \
-composite \
\( foreground.png -matte -channel a -fx "(u.r<0.1||u.r>0.9)?1:0.3" \) \
-composite result.png
So, I basically clone the background, and select the bottom part with the -crop and blur it, then composite it onto the real background. Then I take the foregound and anywhere it is not black or white, I set it to 30% transparent (so as not to fade the black and white aspects). Then I composite that ontop of the background which by now already has the lower part blurred.
It's probably not 100% but you can diddle around with the numbers and techniques till you achieve Apple-y perfection :-)

Related

ImageMagick convert to png8 (paletted PNG with binary alpha) using background color for partially transparent pixels

I have a 32-bit PNG image like this:
Displayed here with a checkerboard background for visibility, but instead of the checkerboard the image is actually transparent (for your reference: the original 32-bit image). As you can see, around the edges and towards the right, the red pixels are gradually fading out from opaque to transparent.
If I would display this 32-bit image on top of a blue background, the pixels around the edges and towards the right would gradually fade from red to blue.
Now I need to convert this image to an 8-bit PNG with binary alpha. I want the fully transparent area to remain fully transparent, but the partially transparent pixels to gradually blend with the intended background/matte color, in this example blue.
So something like this:
However, I can't seem to figure out how to do this with ImageMagick (using ImageMagick 7.0.8-26 on Mac). I tried as follows:
magick original.png -background 'rgba(0,0,255,0)' png8:output.png
But this results in:
It seems to ignore the blue background color altogether, and just convert the transparency to binary (probably taking fully opaque for ≥128 or fully transparent for <128).
Also if I specifiy rgb(0,0,255) as background color (i.e. no alpha) the result is the same.
Does ImageMagick have some smart 'background blending for partially transparent pixels' option that I don't know about?
Otherwise, I'm guessing I should somehow extract a binary bitmap where the original image is fully transparent (i.e. has alpha value or opacity 0), then flatten the image on a background of my choosing, and then re-apply the boolean alpha bitmap again. But not quite sure how to do this.
I can think of two techniques, but I'm sure folks have better answers.
First Option
Composite the blue background, and then copy the alpha channel from the original source.
magick original.png \( \
+clone \( +clone -fill BLUE -draw 'color 0,0 reset' \) \
-compose DstOver -composite \) \
-swap 0,1 -compose CopyAlpha -composite PNG8:output.png
Second Option
Replace transparent with blue color, but then call -transparent without any -fuzz operation.
magick original.png \( \
+clone -fill BLUE -draw 'color 0,0 reset' \) \
-compose DstOver -composite -transparent BLUE \
PNG8:output.png
Again, I'm sure the above examples can be improved on. YMMV
One way to do that with ImageMagick is to extract the alpha channel, colorize essentially everything except the totally transparent pixels, and composite the original image over the result.
convert input.png \( +clone -alpha extract -transparent black -fill blue \
-channel RGB -colorize 100 +channel \) +insert -composite output.png
That reads the input image, clones it inside the parentheses, and extracts the alpha channel to a black and white mask.
The pure black is made transparent with "-transparent black".
The white, everything except pure black, is changed to blue with "-colorize 100". Setting "-channel RGB" applies that fill color to only the white part of the mask and ignores the transparent.
Then after the parentheses the "+insert" moves that blue-on-transparent image to the back, and "-composite" places the input image over that blue and transparent one.
If you're using ImageMagick version 7, use "magick" instead of "convert". If you're running on Windows, change the end of line backslash "\" to a caret "^", and remove the backslashes that escape the parentheses "\(...\)" to simply "(...)".
Here is one more way in ImageMagick.
convert img.png \
\( -clone 0 -alpha extract -channel rgba \
\( xc:none xc:blue +append \) -clut +channel \
-channel alpha -threshold 0 +channel \) \
+swap -compose over -composite PNG8:result.png
OR FOR Imagemgick 7
magick img.png \
\( -clone 0 -alpha extract -alpha copy -channel rgba \
\( xc:none xc:blue +append \) -clut \
-channel alpha -threshold 0 +channel \) \
+swap -compose over -composite PNG8:result.png
Read the image.
Clone it, extract the alpha channel, create transparent blue color map and apply to the alpha channel, then threshold the alpha channel.
Swap the two images and composite the original over the processed alpha channel.
Save the output.
Thanks to the posted suggestions, however the problem is I don't want to mask any particular color (like black or blue) as transparent. That exact same color may also appear in the non-transparent parts.
After some tweaking, I got the exact result I need:
magick original.png -write MPR:foo -background 'rgb(0,0,255)' -flatten \( MPR:foo -alpha extract -threshold 0 \) -compose copy_opacity -composite png8:result.png
Explanation:
I start with original.png and use -write MPR:foo to save a temporary copy in memory called 'foo'
Then I flatten the image on a blue background, giving the correct color values everywhere (including blended with the background for partially transparent pixels) but losing the opacity data.
Then I side-process a 2nd layer, opening the same original input image (restoring from memory using MPR:foo) and extract its alpha channel which now becomes a grayscale image, containing the original alpha data.
I apply -threshold 0 on that layer, which changes all channel values that were >0 to the maximum (i.e. alpha 255 or fully opaque) and ≤0 remains 0 (fully transparent). So this is now my 'binary alpha' channel.
Then finally I merge these two layers by using a copy_opacity composite which just copies the 2nd layer (the binary alpha) and writes that as the resulting layer's alpha data.
Saving the end result gives me the exact image I need.

Convert PDF/Images to a flip based effect in GIF

I want to convert a PDF or sequence-of-images to a flip based effect in GIF (similar to the one below).
Is there any softwares available handy I could use to produce this output? or do I have to write scripts using imageMagicK? please suggest?
Thanks in advance!
Cool project! This isn't production-ready, military-hardened, bullet-proof code, but the following will do most of the heavy lifting as regards getting set up, appending the individual pages, distorting pages as they turn and finally putting the whole lot together in an animated GIF sequence.
#!/bin/bash
################################################################################
# flipbook
# Mark Setchell
#
# Give me 4 pages as parameters and I create an animated GIF book out of them
# called book.gif.
#
# Requires ImageMagick
################################################################################
# Names of the 4 pages
p0=${1:-page0.gif} # Use first arg, or "page-0.gif" if none given
p1=${2:-page1.gif}
p2=${3:-page2.gif}
p3=${4:-page3.gif}
# Get width and height of images - I assume, but do not check all are identical sizes
read w h < <(convert "$p0" -format "%w %h" info: )
((twow=w+w))
# Layout first and last flat double-page spreads
convert "$p0" "$p1" +append frame0.png
convert "$p2" "$p3" +append frame4.png
# Make right page taller and thinner and save as "distorted.png"
((deltah=20*h/100))
((deltaw=20*w/100))
((hplusdeltah=h+deltah))
((wminusdeltaw=w-deltaw))
((hplus2deltah=h+deltah+deltah))
points="0,0 0,$deltah $wminusdeltaw,0 $wminusdeltaw,0 $wminusdeltaw,$hplus2deltah $wminusdeltaw,$hplus2deltah 0,$hplus2deltah 0,$hplusdeltah"
convert "$p1" +matte -virtual-pixel transparent \
-resize ${wminusdeltaw}x${hplus2deltah}! +repage \
-distort Perspective "$points" +repage distorted.png
# Make second frame by overlaying distorted right page ontop of pages 0 and 3
convert "$p0" "$p3" +append \
-bordercolor white -border 0x$deltah \
+repage \
distorted.png \
-geometry +${w}x \
-composite frame1.png
# Make left page taller and thinner and save as "distorted.png"
((deltaw=70*w/100))
((wminusdeltaw=w-deltaw))
points="0,0 0,0 $wminusdeltaw,0 $wminusdeltaw,$deltah $wminusdeltaw,$hplus2deltah $wminusdeltaw,$hplusdeltah 0,$hplus2deltah 0,$hplus2deltah"
convert "$p2" +matte -virtual-pixel transparent \
-resize ${wminusdeltaw}x${hplus2deltah}! +repage \
-distort Perspective "$points" +repage distorted.png
# Make third frame by overlaying distorted left page ontop of pages 0 and 3
convert "$p0" "$p3" +append \
-bordercolor white -border 0x$deltah \
+repage \
distorted.png \
-geometry +${deltaw}x \
-composite frame2.png
# Make left page taller and thinner and save as "distorted.png"
((deltaw=20*w/100))
((wminusdeltaw=w-deltaw))
points="0,0 0,0 $wminusdeltaw,0 $wminusdeltaw,$deltah $wminusdeltaw,$hplus2deltah $wminusdeltaw,$hplusdeltah 0,$hplus2deltah 0,$hplus2deltah"
convert "$p2" +matte -virtual-pixel transparent \
-resize ${wminusdeltaw}x${hplus2deltah}! +repage \
-distort Perspective "$points" +repage distorted.png
# Make fourth frame by overlaying distorted right page ontop of pages 0 and 3
convert "$p0" "$p3" +append \
-bordercolor white -border 0x$deltah \
+repage \
distorted.png \
-geometry +${deltaw}x \
-composite frame3.png
# Make final animation from frame0.png...frame4.png
convert -gravity center -delay 100 frame*.png -background white -extent ${twow}x${hplus2deltah} book.gif
So, if you start with the following as page0.gif, page1.gif, page2.gif and page3.gif...
You will get this as book.gif
If your book has more than 4 pages, you can do four at a time and then append the animations quite simply.
Updated Answer
It seems you are unfortunate enough to have to use Windows - which is very cumbersome in BATCH. I am no expert, but can get around in BATCH a little. I think the script above is pretty easy to translate though. I'll get you started but you will need to do some yourself - you can always ask a new question if you get stuck - questions are free!
The first part of the script just picks up the parameters supplied on the command line, so it'll look like this:
REM Pick up commandline parameters
set p0=%1
set p1=%2
set p2=%3
set p3=%4
Then we need to work out the width and height of the input images, something like this:
REM Get width and height of images in variable "w" and "h"
FOR /F %%A IN ('identify -format "w=%%w\nh=%%h" %p0%') DO set %%A
All the stuff in my original script inside ((..)) is just simple maths which can be done in BATCH using SET /A, so the lines that look like this:
((twow=w+w))
((deltah=20*h/100))
will look like this:
SET /A TWOW=w+w
SET /A DELTAH=20*h/100
The rest is just convert commands - you will need to do a couple of things there:
Replace line continuations at ends of lines, so change \ to ^
Where I use $variable or ${variable}, replace it with %variable%
Double any % signs I have, so % becomes %%
Change \( to ^( - I think
change any single quotes ' to double quotes "
Best to just work through it and see what happens as you convert each line and ask another question if you can't work it out.
There is some good info at these places - ss64 - general, ss64 - set command on BATCH in general. Also, an English guy called Alan Gibson, uses IM with Windows very competently and you can see his scripts here, and also more generally here for inspiration on how to be effective with IM under Windows.

Imagemagick convert resize then crop

I have over 1000 images on different resolutions, (for example 1234x2122, 4400x5212 , etc) and I want to convert all of them to fixed 100x100 size, so.
first I need to resize the images keeping proportions, and get 100xA or Ax100, where A > 100 (it depends width and height of image, for some images width > height, and for some images height > width).
Crop this image to 100x100 from center
Is there a simple convert command, that I can use for all my images?
You would use the area-fill (^) geometry modifier on the -resize operation to unify the down-scale. For cropping the center, -extent with -gravity Center will work.
convert input.jpg -resize 100x100^ \
-gravity Center \
-extent 100x100 \
output.jpg
Update
As Mark Setchell pointed out in the comments, the mogrify utility can be leveraged to batch convert items.
mogrify -path ./path/to/write/results/ \
-resize 100x100^ \
-gravity Center \
-extent 100x100 \
./path/to/source/files/*
Reminder: Mogrify will overwrite original file with resulting image(s), unless you set the -path parameter.
To keep the aspect ratio and don't fill anything (extent), rather crop from the longer edge which is out of aspect you could do
convert input.jpg -resize 100x100^ \
-gravity Center \
-crop 100x100+0+0 +repage \
output.jpg
with option to crop more from one side if you like

Converting (any) PDF to black (K)-only CMYK

This is related to:
Converting PDF to CMYK (with identify recognizing CMYK).
Script (or some other means) to convert RGB to CMYK in PDF?
... but a bit more specific here: say I have an RGB PDF, where the text color is "rich black" (R:0 G:0 B:0 gone to C:100 M:100 Y:100 K:100), and diverse images and vector graphics.
I would like to convert this to a CMYK PDF, using a free command line tool (so it is batch scriptable under Linux), which
has contents only in the black (K) channel:
Preserves vector graphics (+ text glyphs) - colors become grayscale in black (K) channel only
Images get converted to grayscale in black (K) channel only
Thanks in advance for any answers,
Cheers!
As hinted in my comment to #Mark Storer, it turns out that forcing a gray print only on the K plate in CMYK, may not be so trivial ... I guess it depends much on what is being used as "preflight" preview device - for Linux, the only thing I can find is ghostscript with tiffsep, which is what I use for 'sanity check' regarding CMYK separations.
Anyways, I got a lot of help in this thread on comp.lang.postscript:
PDF to PDF (gs?): rich RGB black to plain K (CMYK) black? - comp.lang.postscript | Google Groups
... and one workflow that works for me is:
Convert PDF to PS using ghostscript's ps2write
Use ghostscript to convert this PS back to PDF, while executing replacement functions in HackRGB-cmyk-inv.ps
Use ghostscript's tiffsep to check actual separations
In respect to, say, this PDF generated by OpenOffice: blah-slide.pdf, the command lines would be:
# PDF to PS using `ps2write` device of `ghostscript`
gs \
-dNOPAUSE \
-dBATCH \
-sDEVICE=ps2write \
-sOutputFile=./blah-slide-gsps2w.ps \
./blah-slide.pdf
# PS to PDF using replacement function in HackRGB-cmyk-inv.ps
gs \
-dNOPAUSE \
-dBATCH \
-sDEVICE=pdfwrite \
-sOutputFile=./blah-slide-hackRGB-cmyk-inv.pdf \
./HackRGB-cmyk-inv.ps \
./blah-slide-gsps2w.ps
# check separations
gs \
-dNOPAUSE \
-dBATCH \
-dSAFER \
-sDEVICE=tiffsep \
-dFirstPage=1 \
-dLastPage=1 \
-sOutputFile=p%02d.tif \
blah-slide-hackRGB-cmyk-inv.pdf \
\
&& eog p01.tif 2>/dev/null
This should only work on RGB values where R=G=B (and hopefully grayscale values), and only on text colors, and it also flattens text information - but it should be possible to confirm via tiffsep that the text indeed ends up only on the K plate.
As mentioned in the newsgroup post, this is not extensively tested, but looks promising so far...
Cheers!
As an improvement to sdaau's great answer, I can recommend using pdftops from xpdf for converting pdf to ps, instead of ghostscript ps2write, because the latter e.g. causes the font to become staircasey, and is said to not preserve the original pdf accurately. Compare by zooming into text areas of the resulting pdfs.
I suggest you convert the PDF using GS twice. Once to a Shades Of Gray colorspace, and then to CMYK.
I'm not sure it'll work, but I'd be a bit surprised if it didn't. G->CMYK sounds like a brain-dead X -> 0 0 0 X conversion. At least if you stick to "device gray" and "device CMYK" instead of some calibrated color space that'll tweak things this way and that.

How to remove an image's solid background using ImageMagick convert command

I'm finding a way to make the solid background of an image become as transparent as possible (with little halo). ImageMagick is a powerful tool however it's not easy to use. I tried one command
convert image.jpg \
-bordercolor white \
-border 1x1 \
-alpha set \
-channel RGBA \
-fuzz 20% \
-fill none \
-floodfill +0+0 white \
-shave 1x1 \
image.png
, which works kinda well. However, this requires me to know the exact color of the solid background while I need something to work with any background color. Anyone could help me with this? Th
As a heuristic, you could ask for the color of the pixels at the corners (http://studio.imagemagick.org/pipermail/magick-users/2005-March/015034.html) and then see which color is used the most. Not exactly fool-proof, though...