Question

I need to resize texture image files after COLLADA Export in Sketchup Ruby.

Is there a way to read images (PNG, JPG) from disk, resize them and save them back to disk using plain Sketchup Ruby API?

I know how to do the file read/write, just have no idea about the "image decode, resize, encode" -part.

Most image resize examples I've found involve some image processing library or gem that is not available in Sketchup ruby distribution.. Would be nice to have a portable solution that would be easy to distribute with my SU Ruby plugin.

Was it helpful?

Solution

The SketchUp Ruby API doesn't include methods to manipulate images directly.

You can create materials in the model which uses textures, but not just load an image resource.

There is a method you might be able to use: Material.write_thumbnail (SU8M1+), based on your description you could iterate over the materials in the model and export thumbnails.

However, the method has an very annoying limitation:

Material.write_thumbnail has a behaviour which limits it usability in some scenarios.

If you specify a dimension equal or larger to either the width or height of the original, then the method fails. In other words, the resolution argument must be 1px smaller than the smallest unit of either the length or width of the original texture.

This produces problems with textures with a big ratio between width and height. If you want to produce thumbnails that are max 128x128px and the material you generate the thumbnail for has a texture of 64x512px then it fails. As for that given texture the max size for the thumbnail would be 63px (1px smaller than smallest dimension.) – resulting in a thumbnail of 8×63.

For that reason the method should have ideally accepted width and height for the thumbnail in order to be truly usable. Alas. Hopefully this will be improved in a later version.

(More info on material in SketchUp: http://www.thomthom.net/thoughts/2012/03/the-secrets-of-sketchups-materials/#material-thumbnails)

To get real control over images when developing SketchUp plugins you do unfortunately need to use a third party library.

You can look at the plugin "Texture Resizer" by Aerilius who's done a lot of work in this area. http://sketchucation.com/forums/viewtopic.php?f=323&t=40720#p360679

OTHER TIPS

It appears so if you look here http://www.sketchup.com/intl/en/developer/docs/ourdoc/image, the methods height= and width= should get you what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top