Image scaling Macro

Need help, or want to share a macro? Post here!
JAndersM
Posts: 46
Joined: Tue Dec 22, 2015 1:35 pm

Image scaling Macro

Postby JAndersM » Sat Jan 16, 2016 9:43 pm

I made a Macro for easy scaling of images in Image workbench. It works for images imported as planar image in the 3D space.
You need two points in the image that you know the distance between for the real object:
* run the Macro - a dialogue pops up
* click on the two points in the image that you know the true distance between
* Select the Image Plane in the tree view
* Enter the true distance in mm between the points in the text field of the dialogue and click OK
*The image is scaled and the dialogue closes.

(I have tried to make this version more user guiding and mistake safe than the earlier version I mentioned in an another forum tread)
scale.FCMacro.zip
(1.4 KiB) Downloaded 85 times
triplus
Posts: 7051
Joined: Mon Dec 12, 2011 4:45 pm

Re: Image scaling Macro

Postby triplus » Sat Jan 16, 2016 10:35 pm

I tested the macro and it seems to work. I am not 100% sure of the use cases or if i am doing things as i should therefore you might share more info on that.

P.S. Thanks for sharing it.
JAndersM
Posts: 46
Joined: Tue Dec 22, 2015 1:35 pm

Re: Image scaling Macro

Postby JAndersM » Sun Jan 17, 2016 9:18 am

I made it as I found a drawing of the Pi Camera and wanted to make a camera house to attach a lens for macro photos. To do that I had to scale the image containing the drawing to the correct size in FreeCAD 3D pace. When the drawing is imported it is scaled 1 pixel equals 1 mm, way too large.
As I thought this will be a common task using the Image Workbench I took the time to write a general solution (rather than just calculating xSize and ySize by hand).
With the macro I:
import the camera image in Image Workbench 3D plane
Start the macro
mouse click upper left corner of the camera board
mouse click upper right corner of the camera board
Select the Image Plane in the tree view
Enters 25 (as the camera board is 25 mm across)
Presses Ok and the Image is scaled
Switches to Part design workbench and starts modeling

So the intended use is to scale imported drawings or images as base for modeling.

Possible improvements:
I did try to draw a line between the first point and the mouse pointer using DraftTracker when selecting points but got an DraftToolbox not found exception and skipped the attempt (rather than taking the time to figure out how it should be done...). It would be nice with a line thou.
Change the input to accommodate user preferenced units not only mm. How?
triplus
Posts: 7051
Joined: Mon Dec 12, 2011 4:45 pm

Re: Image scaling Macro

Postby triplus » Sun Jan 17, 2016 1:23 pm

Thanks for additional explanation.
It would be nice with a line thou
http://www.freecadweb.org/wiki/index.ph ... ing_a_line
http://www.freecadweb.org/wiki/index.ph ... g_function
Change the input to accommodate user preferenced units not only mm. How?

Code: Select all

unit = App.ParamGet("User parameter:BaseApp/Preferences/Units")
unit.GetInt("UserSchema")
Should get you user set unit. As this is simple dialog therefore you could do the conversion yourself and appropriate unit mm/inch can be added when the dialog is created.
JAndersM
Posts: 46
Joined: Tue Dec 22, 2015 1:35 pm

Re: Image scaling Macro

Postby JAndersM » Sun Jan 17, 2016 2:15 pm

Yes found these earlier. So you create a line object if you want a line between the last clicked point to the mouse-pointer. Then delete the object afterwards. Is that how I should do it?
Should get you user set unit. As this is simple dialog therefore you could do the conversion yourself and appropriate unit mm/inch can be added when the dialog is created.
Thanks! Now found the unit class. There is no standard unit handling function converting user units to internal mm? How about handling decimal formats (. or ,)?
Edit: Used QLocale for format localisation.
renatorivo
Posts: 1858
Joined: Tue Feb 21, 2012 8:07 pm
Location: Torino - Italy

Re: Image scaling Macro

Postby renatorivo » Sun Jan 17, 2016 3:22 pm

JAndersM wrote:I made a Macro for easy scaling of images in Image workbench.
Thanks,
I think that can be created the page "Macro_Scale_image" in the wiki
triplus
Posts: 7051
Joined: Mon Dec 12, 2011 4:45 pm

Re: Image scaling Macro

Postby triplus » Sun Jan 17, 2016 3:24 pm

Another idea you probably could use Draft Dimension for this exact purpose.
JAndersM
Posts: 46
Joined: Tue Dec 22, 2015 1:35 pm

Re: Image scaling Macro

Postby JAndersM » Sun Jan 17, 2016 7:42 pm

Here is a new version using DraftTracker to create a line between the measurement points.
scale.FCMacro.zip
(1.58 KiB) Downloaded 358 times
renatorivo wrote:I think that can be created the page "Macro_Scale_image" in the wiki
I do not understand, sorry.
triplus
Posts: 7051
Joined: Mon Dec 12, 2011 4:45 pm

Re: Image scaling Macro

Postby triplus » Sun Jan 17, 2016 8:23 pm

I tested it and it works as advertised.
JAndersM wrote:
renatorivo wrote:I think that can be created the page "Macro_Scale_image" in the wiki
I do not understand, sorry.
You can apply for Wiki account and after publish the macro in Wiki section.

http://www.freecadweb.org/wiki/index.ph ... os_recipes
JAndersM
Posts: 46
Joined: Tue Dec 22, 2015 1:35 pm

Re: Image scaling Macro

Postby JAndersM » Mon Jan 18, 2016 8:07 am

Ok, thanks