[Plugin] ImageTrimmer

[Plugin] ImageTrimmer

Postby TIG » Thu Dec 16, 2010 4:48 pm

This new tool comes packaged with the SKMtools available for download from here
http://forums.sketchucation.com/viewtop ... 77#p293677


TIG (c) 2010 - 2016
All Rights Reserved.
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES,INCLUDING,WITHOUT LIMITATION,THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.


ImageTrimmer.rb


Needs 'image_class.rb' and 'EdgeDetector.jar' [in the SKM_tools folder]


Adds a new Tool 'ImageTrimmer':

Menu > Tools > ImageTrimmer

It takes a Selected Image [it must be a .png file] with transparent areas
and it finds all of those areas' boundaries and it adds 'Edges' to them.
It makes a new Component named after the Image containing faces with the
Image applied as the Texture of a new Material with a matching name.
All transparent Faces are removed as 'trimmed' outlines or 'holes'.
The minimum size for a self-contained transparent area is 2x2 pixels -
single transparent pixels or 'linear' pixels that are only a single pixel
wide are merged into the solid areas.
If the Image contains no suitable transparent pixels the Tool exits early
with a warning, with no changes made.
The processes show a % progress in the Command Line, it might take some
time as there can be a lot of computation to do...
The number of edges in a 'Trimmed' Component is effectively the number of
pixels on the transparent areas' boundaries - this can run into several
thousand short edges!
An Instance of the new Component is placed at the Origin - it is initially
made 'flat': you are asked if you want the new Component to 'Stand-up' -
answer 'Yes' to rotate the Component's entities so it is vertical, or 'No'
to leave it 'flat'.
You are asked if you want to 'Flatten' the new Component's Material Texture
PNG [remove transparency] so it will receive shadows - 'Yes' to convert...
You are asked if you want to delete the original Image - answer 'Yes' to
delete it, or 'No' to keep it.
A sub-folder is made in the Model's folder called 'TrimmedImages'.
The Tool uses it for the 'temporary' files needed to make its analysis and
calculations. On completion you are prompted to delete these temporary
files - you answer 'Yes' to delete them, or 'No' to keep them - these files
are named after the original Image - e.g. 'StandingMan1.png' = the original
'imagefile' as extracted from the Image, 'StandingMan1[Edges].png' = a copy
of the 'imagefile' but processed so that there is a one pixel 'black'
outline of the transparent areas and all other areas colored 'white'.
If you chose to 'flatten' the png there will also be another file
'StandingMan1[Flattened].png' = a copy of the 'imagefile' but processed so
there are no transparent areas. You may find keeping these images useful ?
There is a temporary 'data' file 'StandingMan1[Edges].dat' which contains
'01' data used to construct the Sketchup Edges - this should be auto-erased
whatever option you choose. If the 'TrimmedImages' folder is empty after
the Tool completes it is deleted from the Model's folder.
You are asked if you want to 'Simplify the Outline', 'Cancel' to leave the
outline with the maximum 'pixelation', or enter a 'Factor' and press OK to
simplify the outline.
A very small 'Factor' will make few changes to the outlines [if any].
A 'Factor' ~1 will leave the outlines almost unchanged.
A 'Factor' ~2 [default] will usually simplify an outline quite acceptably.
A 'Factor' ~3 will sometimes simplify the outline too much.
A higher 'Factor' [5-10] will often make a very simplified outline that
might only be acceptable in exceptional circumstances.
This 'Simplification' is undoable. Remember that you can always Select a
Component later and Simplify its outline - see below...
The simplification processes show a % progress in the Command Line - the
higher the 'Factor' you entered the quicker the outlines are simplified.

It is one/two/three/four step undo-able depending on what you chose to do:
undo the standing-up of the component, if changed.
undo the deletion of the original Image, if deleted.
undo simplification, if simplified.
undo the 'ImageTrim' completely.

The Tool is useful for auto-trimming transparent areas out of Images for
'cardboard' cutouts of people etc. After the Component is made you might
want to change its origin/axes to say its bottom center and make it into a
'face-me' component etc so it always faces the camera etc...
If you already have a Component using a transparent Material then save its
Texture as an external Image file then Import as an Image, place over the
original Component to resize appropriately and the run 'ImageTrimmer' on
the Selected Image...


Menu > Tools > ImageTrimmer-Simplifier

After you have made 'Trimmed' Component you can simplify its outline by
Selecting it and activating this tool.
If you do not have an acceptable Selection a dialog warns, the tool exits.
A dialog asks for the 'Factor, 'Cancel' to leave the outline unchanged or
enter a 'Factor' and press OK to simplify the outline.
A very small 'Factor' will make few changes to the outlines [if any].
A 'Factor' ~1 will leave the outlines almost unchanged.
A 'Factor' ~2 [default] will usually simplify an outline quite acceptably.
A 'Factor' ~3 will sometimes simplify the outline too much.
A higher 'Factor' [5-10] will often make a very simplified outline that
might only be acceptable in exceptional circumstances.
This 'Simplification' is undoable.
The simplification processes show a % progress in the Command Line - the
higher the 'Factor' you entered the quicker the outlines are simplified.
Idea: if 'simplifying' with a Factor of 2 produces a result that's not
quite what you want, don't 'undo it' be re-simplify the simplified version
with a Factor of 2 again, you might get a surprisingly improved outline...



Donations:
By Paypal.com to info @ revitrev.org




ImageTrimmer-Simplifier-Examples.PNG
2
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101216

Postby Ben Ritter » Thu Dec 16, 2010 5:31 pm

TIG, awesome and looking forward to trying it out. :enlight:
0

Ben Ritter 
 

Re: [Plugin] ImageTrimmer 20101216

Postby simon le bon » Thu Dec 16, 2010 10:49 pm

Dear Tig,
Even if I comment rarely, I have a great respect for your production and your generosity.
Each tool you make, I feel how was we able to do without it.
This is presently one more time the case.

Thank yu so much,

simon.
1
User avatar
simon le bon 
 

Re: [Plugin] ImageTrimmer 20101216

Postby shura » Fri Dec 17, 2010 1:02 am

This is just overwhelming. I was wanting this feature for so long... thank you TIG for making this happen!

As I understand the prcedure, the way to trace a silhouette is to make an inverted transparency for it. With Flatten and Cleanup I get a nice result. Sadly I did'nt get Simplify Contours to work, maybe wrong angle?

( :sketchstatic: :sketchstatic: :sketchstatic: )

jump.jpg
jumpclose.jpg
0
User avatar
shura 
 

Re: [Plugin] ImageTrimmer 20101216

Postby Dave R » Fri Dec 17, 2010 2:04 am

Very cool, TIG. Thank you.

0
Etaoin Shrdlu


%

(THERE'S NO PLACE LIKE)

G28 X0.0 Y0.0 Z0.0

M30

%
User avatar
Dave R 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101216

Postby pilou » Fri Dec 17, 2010 2:20 am

Astonished package! :thumb: :shock: :fro:
0
Is beautiful that please without concept!
My Little site :)
User avatar
pilou 
Top SketchUcator
 

Re: [Plugin] ImageTrimmer 20101216

Postby mitcorb » Fri Dec 17, 2010 5:04 am

Out of the Blue!
Thank you!
0
I take the slow, deliberate approach in my aimless wandering.
User avatar
mitcorb 
Top SketchUcator
 

Re: [Plugin] ImageTrimmer 20101216

Postby srx » Fri Dec 17, 2010 10:11 am

That is great for png trees shadows. Thank you.
0
www.saurus.rs
User avatar
srx 
 

Re: [Plugin] ImageTrimmer 20101216

Postby thomthom » Fri Dec 17, 2010 10:17 am

This is great! It's been asked for for such a long time.
How about an option to simply the edges - make the resulting silhouette simpler?

You can use the Douglas-Peucker algorithm port I used in Edge Tools:
viewtopic.php?f=323&t=24593#p210736
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101216

Postby TIG » Fri Dec 17, 2010 11:18 am

thomthom wrote:This is great! It's been asked for for such a long time.
How about an option to simply the edges - make the resulting silhouette simpler?
You can use the Douglas-Peucker algorithm port I used in Edge Tools:
viewtopic.php?f=323&t=24593#p210736

Thanks for the tip... At the moment it slavishly follows every pixel!
This is only v1.0 so I'm sure there are lots of improvements possible.
My use of using one function specific small Java .jar executables reading 'ini' files to pass arguments etc seems to work better than I expected... So far in alphabetical order, there's...
Bumpmapper.jar and BumpmapperInverted.jar to make gray-scale bumpmap pngs from imagefiles.
EdgeDetector.jar that finds a given png's edges and makes the 'edged' version and a .dat file = 01's used by SUp.
Gifxtractor.jar that makes pngs of all frames in a given animated gif file.
Thumbnailer.jar that makes a 64x64 thumbnail of an image.
ThumbnailerRGB.jar that makes a 64x64 thumbnail of a color from its RGB values.
UnZipDirectory.jar that uncompresses zip files OR skm files.
ZipDirectory.jar that compresses zip files OR skm files in a given folder.
Any ideas for other similar 'jar' tools always welcomed - I only taught myself Java in the last week or two, specifically to do image manipulation parallel to Ruby, but I'm sure there are other possible uses too... :geek:
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101216

Postby thomthom » Fri Dec 17, 2010 11:20 am

You've made a great deal of tools there. Too bad my apartment is such a mess still - don't get much time to look at all these new goodies.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101216

Postby shura » Fri Dec 17, 2010 11:25 am

With that .jar business I resign right away. just want to put another +1 here: simplifying edges? Oh yes please! (drooling)
0
User avatar
shura 
 

Re: [Plugin] ImageTrimmer 20101216

Postby TIG » Fri Dec 17, 2010 11:48 am

I had dallied with some outline simplification ideas... but brought v1.0 to market without that functionality as my brain hurt.
I'll think about it again - e.g. even a simple square hole 32x32 pixels currently has 128 edges when 4 would do!!
A square hole on the diagonal has 256 edges when 4 would do!!!
I'm sure I can clone some ideas from others' code ;) :D
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101216

Postby thomthom » Fri Dec 17, 2010 12:04 pm

TIG wrote:I'll think about it again - e.g. even a simple square hole 32x32 pixels currently has 128 edges when 4 would do!!
A square hole on the diagonal has 256 edges when 4 would do!!!
I'm sure I can clone some ideas from others' code ;) :D

With the simplification algorithm I linked to, then those 128 edges should become 4. You feed it a set of points that form a curve (for separate each curve into sets and you get a simplified set.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101216

Postby CadFather » Fri Dec 17, 2010 3:14 pm

speechlessly amazing!
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101217

Postby TIG » Fri Dec 17, 2010 4:12 pm

Here's v1.1 viewtopic.php?p=294815#p294815
It's got an 'outline simplifier' option added.
This is also available a separate tool to simplify the outlines of existing ImageTrimmed components.
See the notes for details...
[Thanks to ThomThom and Douglas-Peucker fro the simplification algorithm ideas...]
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101217

Postby CadFather » Fri Dec 17, 2010 4:30 pm

not sure what happened but when run, the script opened edge 'detector.jar' file and there it stayed..
it opened with winrar so will check this out

EDIT: yes if winrar (i guess any other zipping app as well) is set to open jar files too - trimmer will run winrar -- uncheking the file association makes trimmer work fine
0
Last edited by CadFather on Fri Dec 17, 2010 4:50 pm, edited 1 time in total.
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101217

Postby TIG » Fri Dec 17, 2010 4:46 pm

CadFather wrote:not sure what happened but when run, the script opened edge 'detector.jar' file and there it stayed..
it opened with winrar so will check this out

You need to have Java enabled on you computer - it usually is...
Did you extract most files into SKMtools subfolder as the instructions...
The script doesn't open 'EdgeDector.jar' - rather it should 'execute' it as it's a very small app that does just one thing ?
What OS system are you using ? XP ??
What version of IE do you have ?
What version of SUp ? I guess NOT 9.1 :roll:
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101217

Postby CadFather » Fri Dec 17, 2010 4:51 pm

well TIG, with the likes of you, it feels like SU 10.2! :lol:
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101216

Postby tomasz » Fri Dec 17, 2010 8:48 pm

Thanks for the interesting plugin and the inspiration TIG!
TIG wrote:I only taught myself Java in the last week or two

Two weeks is not that much. I will give it a try!
0

tomasz 
SU2TH & SU2KT Developer
 

Re: [Plugin] ImageTrimmer 20101217

Postby TIG » Sat Dec 18, 2010 10:31 am

CADfather

I have looked at your '.jar' issue... I can reproduce it! IF I let a 'zip/unzip' application like AlZip or WinRar 'poach' the '.jar' file-type as one that it will open. If so then when Ruby tries to 'open' a '.jar' file instead of it 'executing' in Java [like it should], it 'opens' in the decompression application...

The solution = look at your WinRar settings and ensure '.jar' is NOT one of its file-types for opening...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101217

Postby CadFather » Sat Dec 18, 2010 10:51 am

thanks TIG, that's exactly what i did - (perhaps wasn't too clear in the edit)

one specu-question: could you, adjust the sampling rate/area of the script? suppose you didn't want so much precision, it could perhaps speed up the trimming process - having said that, the simplifier is well integrated
once the trim is achieved
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101217

Postby TIG » Sat Dec 18, 2010 11:04 am

CadFather wrote:thanks TIG, that's exactly what i did - (perhaps wasn't too clear in the edit)

one specu-question: could you, adjust the sampling rate/area of the script? suppose you didn't want so much precision, it could perhaps speed up the trimming process - having said that, the simplifier is well integrated
once the trim is achieved

Sorry - didn't see the 'edit' ! :roll:
It first has to 'get' all of the edges of the solid/transparent areas - it's 'easier' to draw all of the edges as boundaries to these pixels - this is the most complex outline - a Factor 2 simplification will often improve the steppiness of these edges, BUT it can trim small solid areas or leave small transparent areas... Most of the time in the initial processing is finding which edge bits get faces or 'holes' and of course therefore which are part of which 'loops'... The 'simplifier' works on the face perimeter loops once they are made - it's much easier than trying to decide which edges could be the loops initially... You will have seen that the simplifier at Factor 2 reprocesses the faces' loops quite quickly... I can see how to easily recast the code to allow simplification before processing :?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101217

Postby CadFather » Sat Dec 18, 2010 1:53 pm

i see.. still a pretty amazing feat.. in fact so are the images produced in the folder.. Thanks again
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101217

Postby TIG » Sat Dec 18, 2010 3:08 pm

CadFather wrote:i see.. still a pretty amazing feat.. in fact so are the images produced in the folder.. Thanks again

The Image's original 'image' file, 'edged' .png and a .dat file [01s] are put into the temporary folder [it is removed IF it's empty] made inside the SKP model's folder. If you answer 'No' when asked if you want to remove temporary files you can open/use them. I didn't automate this deletion because you might want the Image's 'image' file and/or the 'edged' .png. The .dat file does go but it's just a load of 0s and 1s !!! If you leave one of the dialogs open you can always read/copy it before it 'goes' ;)
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101217

Postby CadFather » Sat Dec 18, 2010 6:27 pm

very useful indeed.. :)
0
User avatar
CadFather 
PluginStore Author
PluginStore Author
 

Re: [Plugin] ImageTrimmer 20101217

Postby solo » Mon Dec 20, 2010 5:21 pm

What am I doing wrong? I have a .png. I made it a component, selected it and then clicked on Image trimmer and get a warning 'Image trimmer: You selected an Image first.', so I exploded and tried again with same warning, I tried simplyfier and it works, but trimming does not, what am I doing wrong?
0
If you see a toilet in your dreams do not use it.
User avatar
solo 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101217

Postby TIG » Mon Dec 20, 2010 7:12 pm

solo wrote:What am I doing wrong? I have a .png. I made it a component, selected it and then clicked on Image trimmer and get a warning 'Image trimmer: You selected an Image first.', so I exploded and tried again with same warning, I tried simplyfier and it works, but trimming does not, what am I doing wrong?

You don't use 'ImageTrimmer' on a 'Component' - you use it on a plain imported [unexploded] 'Image' - check in Entity Info to see what it is...
After it's 'trimmed' that Image is copied as a Material applied to Faces inside a new Component...
so then you can no longer 'trim' it, but of course you can 'simplify' the outlines later within the Component... :?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: [Plugin] ImageTrimmer 20101217

Postby Oxer » Mon Dec 20, 2010 9:43 pm

Hello TIG!

I don't know how use the plugin, i do this:
1. I import a png file like image.
2. I make a new component, its name the same like image.
3. I make a new material with the png file with the same name, and i apply this material to the face inside the component.
4. I select the image into SU and run the plugin.
Nothing happens, only the plugin creates a folder named "TrimmedImages" in the same directory that Model file, it contains the png file.
What i do bad?

I use Mac with OSX 10.6 and SU 8.

EDIT: Appears this message in Console Ruby
Consola.jpg
0
Last edited by Oxer on Mon Dec 20, 2010 9:54 pm, edited 1 time in total.
"The result is the end what is important is the process" by Oxer
http://www.oxervision.blogspot.com/
User avatar
Oxer 
 

Re: [Plugin] ImageTrimmer 20101217

Postby Dave R » Mon Dec 20, 2010 9:49 pm

Oxer, you might try importing the image as an image and then immediately running the plugin on it. See if that does what you want.
0
Etaoin Shrdlu


%

(THERE'S NO PLACE LIKE)

G28 X0.0 Y0.0 Z0.0

M30

%
User avatar
Dave R 
Global Moderator
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 

Next


 

Return to Plugins

Who is online

Users browsing this forum: alexkwak, kinza3452 and 8 guests