Enhancing starfield 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?".
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

And it's a Nikon D90, APS-C'ish sensor.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

I've ignored the "radial blur" problem. At resolutions for movies, it doesn't matter.

Code: Select all

%IM%convert ^
  %SRC% ^
  ( -clone 0 ^
    -statistic maximum 10x10 ^
    +write sx.png ^
  ) ^
  ( -clone 0 ^
    -colorspace Gray -blur 0x4 -threshold 12%% ^
    +write sh.png ^
  ) ^
  ( -clone 1-2 ^
    -alpha off ^
    -compose CopyOpacity -composite ^
    -background Black -compose Over -layers flatten ^
    +write sl.png ^
  ) ^
  -delete 1-2 ^
  smask.png ^
  -alpha off ^
  -compose Lighten -composite ^
  stars_out.png
Each "+write s*.png" line is only for debugging, and can be removed.

sx.png is the colour of each star, spread out to a square. (Strictly speaking, it is the greatest red value, greatest green value and greatest blue value within the square.)

sh.png is black paper, with white holes "punched" in the paper. The size of the hole depends on the star's original size and its lightness.

We put the black paper with holes over the colours for the stars. This is sl.png.

Then we take the original image and sl.png. The output pixels will be the lighter from these two sources. The result looks fairly good to me, at full resolution and reduced to a laptop screen. However, the stars bottom-left have been masked out of the processing.

I've put a jpg version at http://snibgo.com/imforums/stars_out.jpg

For animation, the mask may need refining. With my current script, the border between black and white may make sudden jumps, causing stars to suddenly change size. If so, the smask.png code will need to change, probably to make a proper grayscale mask instead of binary black/white.
snibgo's IM pages: im.snibgo.com
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

May I ask some really dumb, newbie questions? ("No. Go RTM" is an OK answer of course! Although I have done that already ...)

This is just about the mechanics of IM. So in your script:

%IM%convert ^
%SRC% ^
( -clone 0 ^
-statistic maximum 10x10 ^
+write sx.png ^
) ^

Does that make a working copy of the original file (indexed as '1') and apply the next operation (-statistic) to it, then write a copy of the outcome of that to file (for debugging as you already said)?

( -clone 0 ^
-colorspace Gray -blur 0x4 -threshold 12%% ^
+write sh.png ^
) ^

Does that make another working copy of the original (indexed as '2') and apply the next two operations (-blur and -threshold, assuming -colorspace is not an operation as such) to it? And write a copy.

( -clone 1-2 ^
-alpha off ^
-compose CopyOpacity -composite ^
-background Black -compose Over -layers flatten ^
+write sl.png ^
) ^

Does this make working copies of images '1' and '2', indexed as '3' and '4'?

Are the following operations within the brackets applied to both '3' and '4'? Or just '4' (the image at the top of the stack)? At the end of this procedure, does "-compose Over" and "-layers flatten" leave the end result in a new version of '3'?

-delete 1-2 ^

Does that remove '1' and '2' and leave what was '3' at the top of the stack as a new version of '1'?

smask.png ^
-alpha off ^
-compose Lighten -composite ^
stars_out.png

Does this apply '0' and '1' to smask.png, output the result as stars_out.png, and then discard '0' and '1' (as it's the end of the overall command)?

Cheers.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

Almost exactly correct. "Yes, yes, yes..." until:
kevinbuckley70 wrote:( -clone 1-2 ^
-alpha off ^
-compose CopyOpacity -composite ^
-background Black -compose Over -layers flatten ^
+write sl.png ^
) ^

Does this make working copies of images '1' and '2', indexed as '3' and '4'?
It makes copies of '1' and '2', but the open-parenthesis "(" means "start a new list", so these are indexed as '0' and '1' in the new list, which contains only 2 images. (Incidentally, I call it a list of images, not a stack of images. Yes, I know some IM developers call it a stack.)

"-alpha off" operates on all the images in the current list. "-compose" is a setting. "-composite" merges the two images in the list into one, so this is now number '0' in the sub-list. It is flattened against black. Close-parenthesis ")" means "end the sub-list and move any images to the end of the containing list". So image '0' in the sub-list becomes image '3' in the outer list.

"Yes, yes, yes" until:
kevinbuckley70 wrote:smask.png ^
-alpha off ^
-compose Lighten -composite ^
stars_out.png

Does this apply '0' and '1' to smask.png, output the result as stars_out.png, and then discard '0' and '1' (as it's the end of the overall command)?
After "smask.png", we have three images in the list. "-composite" merges these three into one. So, after "-composite", we have only one image. The "convert" command should always finish with an output image specification (eg a filename or "NULL:" or "info:" or "txt:" etc), into which it will put all the images in the list, and there should be at least one image. If I gave IM three images to save, IM would call them stars_out-0.png, stars_out-1.png and stars_out-2.png.


Most operations operate on all the images in the current list, and only on those images.
snibgo's IM pages: im.snibgo.com
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

OK - I think I have got that. So 'clone' (used within the parentheses) uses the 'outside' list to create 'local' copies which, within the parentheses, are referenced from '0' again'?

Presumably then, as a programmer would say, the 'outside' list images are 'out of scope' within the parentheses: no way to access them, except with clone?

At the end of the operations within the parentheses, are however many images remaining in the 'local' list (local-'0' '1' '2' etc.) pushed (appended) onto the end of the outside list?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

Yes, that's it, with one small "gotcha".

"-clone" and "+clone" can be used in the main outer list, or within parentheses. When used in the main outer list, the number refers to the image with that index in that list. When used in a parenthesis, the number refers to the image with that index in the containing or "parent" list.

"-delete", "-swap" etc also take index numbers. These always refer to images with that index in the current list.

When I use "-clone", it is always within parentheses. I don't think I ever use it outside.

Parentheses can be nested, of course.

Code: Select all

%IM%convert ^
  xc:red ^
  xc:lime ^
  xc:blue ^
  ( -clone 1 ^
    ( -clone 0 ^
      +write info: ^
    ) ^
  ) ^
  null:
"( -clone 1" starts a new list, and copies image '1' from its parent into it, so this is lime, and is numbered '0' in the current list.

"( -clone 1" starts a new list, and copies image '0' from its parent into it, so this is also lime.

We can evade the "scoping rules" with "mpr:". This saves the image list as a named in-memory structure, accessible for the remainder of the command. I typically use it for single images, rather than multiple images.

Code: Select all

%IM%convert ^
  xc:red ^
  xc:lime ^
  xc:blue ^
  +write mpr:MY_THREE ^
  -delete 0-2 ^
  ( xc:purple ^
    mpr:MY_THREE ^
    +write info: ^
  ) ^
  NULL:
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: Enhancing starfield images

Post by fmw42 »

According to the user docs at http://www.imagemagick.org/Usage/basics/#clone

"The Clone Image Operator can be used without parenthesis, and will just copy images from the current image list and directly append them. However this is not its intended use and is to be discouraged as it will produce a different result if you later surround that set of operations by parenthesis."

So it is safest to use clones only within parenthesis. I think it even fails if done outside parens in IM 7. If you need to replicate an image outside of parenthesis, then use -duplicate. see http://www.imagemagick.org/Usage/basics/#duplicate
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

That's all been really, really helpful - thanks!
Post Reply