I have a simple task, it is auto generating some images for PCB designs.
This all worked, nicely. There are two parts
- Get KiCAD to render the 3D model of board, and make an image.
- Get OpenSCAD to render a view of my case design.
In both cases I want a transparent background, anti-aliasing, and cropped around the edges.
KiCAD
KiCAD has a command line that lets you make a nice image render, on a transparent background. However I was not easily able to control which layers show, and it showed stuff I did not want shown. This may have improved since. But also I wanted to change a user layer to an Edge Cut and remove the original Edge Cut to allow me to render a broken out version of the PCB. So I wrote a simple function to edit the KiCAD file before rendering.
I rendered larger and then auto-cropped in gimp.
| In panel view |
| Cut out of panel |
OpenSCAD
For the case designs, I have OpenSCAD make STL files ready to print, but I also wanted a render as a PNG. So I asked OpenSCAD to render the STL. STL has no colour so I picked white. But OpenSCAD does not render on to a transparent background.
I got around this by setting white/shades for the object and a very specific light blue background which is not in the object. OpenSCAD did not do anti-aliasing which makes if very easy - render larger - change the blue to transparent, and scale down for anti-aliasing.
I did all that in one imagemagick command. Worked well.
Upgrades
I have since upgraded KiCAD, and OpenSCAD, and gimp.
GIMP
GIMP is really good, and I do not want to dis it, honest. But gimp scripting is nightmare.
You would expect something like a simple auto-crop would be a simple command line. It is not. I had to make a script-fu script and put in the right directory (which was version specific). On upgrade I have to move it, but did not work. I have to tell the command line which interpreter to use now.
Even then, the script is a nightmare. I just want to auto-crop and images, so maybe three steps: load image, auto crop image, save image.
If you search you find examples (not for latest gimp) like this!
I managed to make a much smaller script, but still. One annoyance seems to be that there is no one simple place to find documentation of what commands exist and what arguments for scripts. Also, confusingly, some are plugin- and some are gimp-. I got it working but mostly by trusting examples I found not finding clear documentation.
When I upgraded, it did not work. This was difficult. Most commands had changed name, as had some arguments. I eventually got loading the file to work, and setting background colour, and even autocrop. But save file would not work. It seems a path and a string are not the same somehow, but my filename string worked for load, not for save, so inconsistent.
I gave up. I realised that I was doing this for the KiCAD PCB images as well, and in that case I used imagemagick.
In imagemagick it is magick filename -trim filename, so really simple, and worked.
KiCAD
The KiCAD update created something odd where it was showing the User.1 layer I used for V-Cuts, which it did not before. I had to change my tool to strip that layer. And now the images cropped correctly.
OpenSCAD and Background colour
But the OpenSCAD was bugging me - this was not a new issue as such, just I ignored it before. The PNG images seemed to have a black background. Noticeably when I look at a thumbnail on MacOS. This was not happening on the KiCAD based images.
I assumed it was the background setting in the PNG, and that was somewhat confirmed when I used pngcheck which reported the image was a 16-bit greyscale image with a background of 0x00FF. That is nearly black. I found no way to change this. imagemagick seemed to be setting only an 8 bit value for background on a 16 bit greyscale image.
In desperation I coded some C to update a PNG to set the background to a white level, got it all working, but nothing changed! My code was setting to 0x00FF. I tracked down, the image is 8 bit greyscale not 16, so 0x00FF is correct! pngcheck was misreporting as 16 bit (or was counting the 8 bits alpha as part of it, confusingly).
The fix, eventually, was to avoid greyscale as it clearly upset the thumbnails. I forced the original format which was 8 bit RGBA but simply prefixing the output file in imagemagick with PNG00:
This was a lot of yak shaving this morning.