Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resize and convert a SVG image using a gimp script ?
#1
I'm using gimp 2.10 on a Debian 12 system.

My gimp script successfully convert the SVG to PNG, but it doesn't resize it.

Here is my script ~/.config/GIMP/2.10/scripts/convertresize.scm:
Code:
(define (convertresize in_filename out_filename width height)
   (let* (
           (image (car (gimp-file-load RUN-NONINTERACTIVE in_filename "")))
           (drawable (car (gimp-image-get-active-layer image)))
           (gimp-image-scale-full image width height INTERPOLATION-CUBIC)
           (gimp-layer-resize-to-image-size drawable)
       )
       (gimp-file-save RUN-NONINTERACTIVE image drawable out_filename out_filename)
       (gimp-image-delete image)
   )
)

 I launch it with the following command:
Code:
gimp -i -b '(convertresize "./image.svg" "./image.png" 24 24)' -b '(gimp-quit 0)'

So, if someone knows what I'm doing wrong ?
Reply
#2
I have finally found my error. The transformations have to be done outside of the section that loads the image.

The following script works:

Code:
(define (convertresize in_filename out_filename width height)
   (let* (
           (image (car (gimp-file-load RUN-NONINTERACTIVE in_filename "")))
           (drawable (car (gimp-image-get-active-layer image)))
       )
       (gimp-image-scale-full image width height INTERPOLATION-CUBIC)
       (gimp-layer-resize-to-image-size drawable)
       (gimp-file-save RUN-NONINTERACTIVE image drawable out_filename out_filename)
       (gimp-image-delete image)
   )
)
Reply
#3
Good for you, but it would have been much simpler and faster with ImageMagick in a shell script. (imagemagick in your repo, + potrace if you haven't got it already)

Code:
convert input.svg -resize WIDTHxHEIGHT output.png

And  if you stick with Gimp, INTERPOLATION-NOHALO is a better option, the only thing "Cubic" has for itself is a low CPU usage, but on modern PCs you won't notice the difference.
Reply
#4
Thanks for the advice about INTERPOLATION-NOHALO.

I have a preference for GIMP because after some tests with imagemagik and inkscape, the quality of the converted/resized images seems to be better with GIMP.
Reply
#5
(11-13-2024, 12:46 PM)Alpha504 Wrote: Thanks for the advice about INTERPOLATION-NOHALO.

I have a preference for GIMP because after some tests with imagemagik and inkscape, the quality of the converted/resized images seems to be better with GIMP.

... and you use Cubic?

In Imagemagick there is an -interpolate option (to be used with  -scale) to define the interpolation algorithm used.

You'll see plenty of bad advice over interpolation algorithms on the web, because what works for some images won't work on others.

The interpolation algorithms have a frequency response (a bit like low-pass audio filters). But the high frequency response is not a simple decreasing curve, the response curves have bumps on high frequencies (strong bumps for simple algorithms like linear and cubic). So when the image that you scale has spatial components with frequencies that fall on these bumps they produce artefacts: roof and floor tiles, but also edges of oblique lines in CGI (so, typically, SVG renders). Using a different algorithm can put the frequency response bumps elsewhere so they no longer are a problem with the current image (but that doesn't make that new algorithm universal solution).

The general solution is to apply a good low-pass filter to the image before scaling it down, so that the image has no spatial frequency components in the bumps area. This good low pass filter is called "Gaussian blur". You just blur the image enough to blur out the details that will anyway be invisible in the scaled down output. And IM has a -blur operator that you can insert before the -scale.
Reply
#6
(11-13-2024, 01:27 PM)Ofnuts Wrote: ... and you use Cubic?

In Imagemagick there is an -interpolate option (to be used with  -scale) to define the interpolation algorithm used.

With Imagemagick, I didn't use the interpolate option, I'm very new to image manipulation softwares. But for my manual tests with GIMP I used Cubic.

From Imagemagik, I only used "mogrify" with "resize" and "format" options, and the SVG to PNG convertion was very bad, very different from the original image.
Reply
#7
(11-13-2024, 02:40 PM)Alpha504 Wrote:
(11-13-2024, 01:27 PM)Ofnuts Wrote: ... and you use Cubic?

In Imagemagick there is an -interpolate option (to be used with  -scale) to define the interpolation algorithm used.

With Imagemagick, I didn't use the interpolate option, I'm very new to image manipulation softwares. But for my manual tests with GIMP I used Cubic.

From Imagemagik, I only used "mogrify" with "resize" and "format" options, and the SVG to PNG convertion was very bad, very different from the original image.

I did a couple of tests before posting and things are fine, scaling down 2X or even scaling up 3X. Can you post an example?
Reply
#8
(11-13-2024, 04:06 PM)Ofnuts Wrote: I did a couple of tests before posting and things are fine, scaling down 2X or even scaling up 3X. Can you post an example?

I have attached a screenshot of my experiments, and the original SVG file of size 96x96. This file is a modified image I took from MATE Desktop environment "mate" theme. I have only modified the colors, replacing blue colors by green ones.

I ran the following command:

Code:
mogrify -format png -resize 128x128 ./nm-device-wired.svg

   


Attached Files
.svg   nm-device-wired.svg (Size: 111.05 KB / Downloads: 19)
Reply
#9
You are starting from SVGs. SVG files, being vector graphics,  can be scaled losslessly. If you convert them to raster first and scale next. the cale is don on ratser and not correct.  A good CLI tool to scale SVG as vector and only then output the result to PNG  is rsvg-convert


rsvg-convert --width=400 --height=400 nm-device-wired.svg -o nm-device-wired-rsvg.png


So the 4+x upscale is perfectly sharp;

   

I found rsvg-convert already installed on my Ubuntu so you probably already have it too (part of librsvg2-bin, on which many popular packages depend)
Reply
#10
Thanks for the information. For converting a lot of files, it will probably be faster than the shell script I wrote that do multiple call to my gimp script.
Reply


Forum Jump: