Expanding round images.

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
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Expanding round images.

Post by gborbonus »

Hi guys,

I'm trying to write an image manipulation script that will take a square image, cut out the right size in a perfect circle, I've got the code for that, so good there.

Now, I need to be able to copy the edge of a circle continually until it makes the circle 30px greater in radius.

there is text in the middle that cannot move. There could be icons and logos in it as well. None of this should move. Thus the reason I'm trying to do it this way.

But here's an example(the image is mirrored on purpose):
Image

Image

I know they aren't perfect, but imagine the white border on the first image is transparent space, and the red in the second image is an exact match to the red in the image.

Also, if there is a line that is horizontal, the expanded image should keep that line horizontal. Lets say in the above image that the word "The"(backwards on the picture) has a line under it, black, expanding all the way to the edge. I need to be able to keep that line going, even to the new edge, and vertically as well.
I know this is not an easy thing to do, it might not even be possible with normal logic since this depends on relative logic.

I'm not good with photos and colors and such, I'm good at programming.


If you have any ideas on how this might be accomplished. Please let me know.
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Expanding round images.

Post by snibgo »

First tip: don't use JPEG for graphics images like this. It makes everything much harder.

The first part is easy. You might:

(a) Decide how much you want expanding. Make the parts of the square that aren't in the circle transparent. Then expand the circle outwards. This might be with blur or (more awkwardly) resize.

(b) For this example, you could floodfill the grey, turning it red. Then do a circular crop (using a mask).

Both these techniques are good for solid colours.

The second part is harder, and needs a precise definition of the problem. Would the lines always be horizontal? Always a solid colour? Always black? Are any other pixels every black? Assuming the most favourable answers, the solution wouldn't be trivial. Perhaps a horizontal blur, in two directions for the two sides, would do it.
snibgo's IM pages: im.snibgo.com
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Expanding round images.

Post by gborbonus »

the image is uploaded by a customer, and the no, the lines won't be always one thing or another. I use PNG, and the upload domain I used made transparent turn grey.

I'm not sure how to do this with IM, but I imagine a radial repeat of the , lets say 10, pixels might just do the trick, and use blur to fill in the gaps.So, from center to edge, ignore the first 300+ and only get the last 10 pixels, then copy them along the same radial line. This would leave a small gap between lines, where blur could just fill it in.


I have no idea how to do this. Thus, I am here.
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Expanding round images.

Post by snibgo »

Next tip: use a hoster such as dropbox.com that doesn't mangle images.

I've added an underline, circus.png:
Image

The following makes a crop of just the circle, another crop from just a "ring" of pixels (more or less the red ring) which it blurs radially, and finally composes the circle over the blur.

Windows BAT script.

Code: Select all

set SRC=circus.png

FOR /F %%L ^
IN ('%IM%convert ^
  -ping %SRC% -format "WW=%%w\nHH=%%h\nRMAX=%%[fx:max(w,h)/2]" ^
  info:') ^
DO set %%L

set /A POL_WI=RMAX*3
set /A POL_HT=RMAX

%IM%convert ^
  -size %WW%x%HH% radial-gradient: -negate ^
  +clone ^
  ( -size %POL_HT%x%POL_WI% gradient: ^
  -rotate 90 ^
  +distort Polar ^"%RMAX%^" ^
  +repage -flop ^
  -gravity center -crop %WW%x%HH%+0+0 +repage ^
  ) ^
  -combine ^
  sb_maskang.png

set BLUR_SIG=10

%IM%convert ^
  circus.png ^
  ( +clone ^
    -fill Black -colorize 100 ^
    -fill White -draw "circle 89,89 89,7" ^
  ) ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  c0.png

%IM%convert ^
  %SRC% ^
  ( +clone ^
    -fill Black -colorize 100 ^
    -fill None -stroke White -strokewidth 5 -draw "circle 89,89 89,8" ^
  ) ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  -write c1.png ^
  sb_maskang.png ^
  -compose Blur ^
  -set option:compose:args 0x%BLUR_SIG%+0+360 -composite ^
  -alpha off ^
  -write c2.png ^
  c0.png ^
  -compose Over -composite ^
  cExtended.png
The three convert commands could be combined into one.

cExtended.png:
Image
You would then crop this down to the required size.
snibgo's IM pages: im.snibgo.com
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Expanding round images.

Post by gborbonus »

That is perfect, but why are the outer edges gradient?


I'm attempting to figure out what you're doing, translating bat to bash (though I don't really know bat).
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Expanding round images.

Post by snibgo »

The radial streaks are from the ring pixels not being an even shade of red. Also I didn't take the time to find the exact centre of the circle.

I left in "-write c1.png" and "-write c2.png". Looking at those should give an insight to how it works. They aren't needed; remove them when it works.

For conversion to bash, the only tricky bit should be the initial "for" statement. In bash, it would be something like:

WW=`convert -ping %SRC% -format "%w" info:`
HH=`convert -ping %SRC% -format "%h" info:`
RMAX=`convert -ping %SRC% -format "RMAX=%[fx:max(w,h)/2]" info:`

%SRC% in bash is $SRC$, or something.
snibgo's IM pages: im.snibgo.com
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Expanding round images.

Post by gborbonus »

The only thing I was worried about.

ok, I'm going to play with this for a bit and figure out how to make this work.
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Expanding round images.

Post by anthony »

A start point to figure it out is Polar De-polar Cycle Distortions...
http://www.imagemagick.org/Usage/distorts/#polar_tricks

It is a good way of handling circles in image, though may need some extra 'scaling' or 'super-sampling' tricks to improve quality of results, when quality matters. JPEG input is not very high quality!


The other technique that your image example may lend itself to is the use of Vector Graphics, which define images in terms of 'drawing there components'. That is formats such as SVG, and Postscript. IM can do this though it is not really designed for it.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
gborbonus
Posts: 33
Joined: 2014-07-01T06:53:30-07:00
Authentication code: 6789

Re: Expanding round images.

Post by gborbonus »

Thanks Anthony,

I figure I can use http://www.imagemagick.org/Usage/distorts/#depolar to flatten the image, crop out the top and bottom sets of pixels, append/prepend the cropped image multiple times, then use polar distortion to turn it back to a circle, generating a larger version of the original image with extended border.

I'll test that sometime this week.

Thank you.
Thank you,
Greg Borbonus
*nix system administrator
http://linkedin.com/in/gregborbonus
Skype: greg.borbonus
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Expanding round images.

Post by fmw42 »

Perhaps try the compositing technique I outlined at viewtopic.php?f=1&t=25893, but use -morphology edgeout.
Post Reply