yyyy273 wrote:Also, could you clarify what
from your script does?
I needed to find the FIRST non-transparent pixel in the image, which is the next segment to find in the image. Remember that segments that have been found are flood-filled with transparency
to remove them.
The
sed script does a text search of the
'txt:' enumerated pixel image format. Then...
1d; just junk the first line, or the header
/ 0)/d; if this string is present pixel is fully-transparent, junk the whole line, (and start again with the next line)
At this point we either have nothing (EOF) or we have found the non-transparent pixel, so....
s/:.*//; Junk anything following the ':' which is color info
q Print the line (default 'sed' action) and then quit!
Basically just return the first non-transparent pixel.
NOTE: I search for ' 0)' rather than something else like 'none'
as pixels may be transparent, and not the color 'none' which represents fully-transparent black!
So the loop is.
- find first non-transparent pixel
- flood fill that pixel to transparency and save into a new image
- use new image to mask previous image to return the segment that was found and print it to the output pipeline (regardless of what color or 'fuzz' was used)
- move new image into old image before looping.
Outside the loop, the images found are concatenated (a multi-image MIFF file format, is just a concatenation!), and then save it to the given file name.
The output can be a multi-image format like GIF, or separate files like PNG or JPEG. User can add a '%02d' in the filename for multiple images, just as in normal IM commands.
For example....
Code: Select all
segment_image input.png gif:- |\
convert gif:- -layers CompareOverlay segments.gif
or multiple transparent images...
Code: Select all
segment_image input.png segment_%02d.png
or flattened to create 'blue screen' JPEG images...
Code: Select all
segment_image input.png miff:- |\
convert miff:- -bordercolor blue -border 0x0 segment_%02d.jpg
Pipe input works too. for example - remove background first...
Code: Select all
convert input.png -transparent white miff:- |\
segment_image - segment_%02d.png
I always try to ensure my scripts are 'pipe' friendly like this, and as I describe in
Hints for Better Scripts
Now if we can get a C equivalent, we can build this into IM, for speed, and so all pre and post operations in the same command!