How to improve quality of SVG during resizing (Java)

@mvm,

Thank you for sharing the details. I have observed the issue and issue with ID IMAGINGJAVA-1309 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

Hey! Any updates on this issue? I’m having the same problem using Aspose.Imaging.Net.

@mvm

I suggest you to please try using following sample code on your end.

String baseFolder = "D:\\";
String inputFileName = baseFolder + "logotype.svg";
float scale = 10f;
Image image = Image.load(inputFileName);
try
{
   PngOptions pngOptions = new PngOptions();
   SvgRasterizationOptions svgRasterizationOptions = new SvgRasterizationOptions();
   svgRasterizationOptions.setPageSize(Size.to_SizeF(image.getSize()));
   svgRasterizationOptions.setScaleX(scale);
   svgRasterizationOptions.setScaleY(scale);
   pngOptions.setVectorRasterizationOptions(svgRasterizationOptions);

   image.save(inputFileName+".png", pngOptions);
}
finally
{
   image.close();
}

This is wrong suggestion ! Already checked it before !

In this case will resize raster image after converting in PNG format.
The quality of vector image will lost if coefficient of scaling too much.
The main sense of using SVG is scaling image BEFORE it will rasterized to PNG or other format and in this case could be saved quality of image. Look into example of code in this topic please:

Image image = Image.load(FileName);
image.resize(Math.round(width), Math.round(height));

should be working properly on SVG images.
Or implement it other way, that resize() method will working with SVG format of images.

@mvm,

I have observed your comments and mentioned these in tickets.

@mvm,

Can you please try to use following sample code on your end and share feedback with us if there is still an issue.

String baseFolder = “D:\”;
String inputFileName = baseFolder + “logotype.svg”;
float scale = 10f;
Image image = Image.load(inputFileName);
try
{
PngOptions pngOptions = new PngOptions();
SvgRasterizationOptions svgRasterizationOptions = new SvgRasterizationOptions();
svgRasterizationOptions.setPageWidth(image.getWidth() * scale);
svgRasterizationOptions.setPageHeight(image.getHeight() * scale);
pngOptions.setVectorRasterizationOptions(svgRasterizationOptions);

image.save(inputFileName+".png", pngOptions);

}
finally
{
image.close();
}

Are you kidding guys ?
It is working absolutely same. Please, look on my previous comment.
I was test this suggestion, with:

svgRasterizationOptions.setPageSize(Size.to_SizeF(image.getSize()));

and with:

svgRasterizationOptions.setPageWidth(image.getWidth() * scale);
svgRasterizationOptions.setPageHeight(image.getHeight() * scale);

and it is working same way. Yes, it is working, but working wrong.
It resize image after rasterize and lost the quality of image.
But right way is resize vector image before it will rasterize !

@mvm

We are sorry for your inconvenience. We are verifying it further on our end and will share feedback with you ASAP. We request for your patience in this regard.

Sorry for my perseverance !
Of course, I understand, that it is not simple to make so flexible method, which will working for all formats.
I will waiting for results and I wish luck to you.

P.S. I did not tested on versions high, than 19.4.
Tell me please, if exists a possible, that you fixed it in newer versions ?

@mvm,

Sure, we will share feedback with you soon.

@mvm,

We’ve double checked the issue and found that current implementation works well when up-scaling with scale factor 10 and more, but loses quality with smallest scale factor values. Just to ensure, that we’re on the same page, can you please share the required logo width and height. We have also added the issue with ID IMAGINGJAVA-1431 to support native Svg resize in the future releases as well.

Well, originally I have SVG image with size 165x40.
I have not possible to define factor of scale because I am using Template for Documents,
which customers creating themselves and I can retrieve only width and height.
But, usually it resizing to resolution around of 350x85 pixels.
Actually, exists a possible, that it could be resized to 650x155 pixels.
In this topic uploaded archive, which contain source code and sample of SVG image.
If I understood correctly, that vector image after load over

com.aspose.imaging.Image.load(stream);

should be resized using

com.aspose.imaging.Image.resize(width, height);

method without lost quality of image and after that converting to other formats or execute some other activity with this image. Is that correct ?

@mvm,

Thank you for sharing the information. We will get back to you as soon as the concerned IMAGINGJAVA-1431 will be resolved and request for your patience in this regard.

Hello !

I seen that you closed this issue:

IMAGINGJAVA-1309 ---- Status : Closed

May I know is that you resolve this problem ?
Do you have any updates for now ?

Thanks in advance !

@mvm,

Can you please try to use following sample code on your end and share feedback with us.

String baseFolder = "D:\\";
String inputFileName = baseFolder + "logotype.svg";
float scale = 10f;
Image image = Image.load(inputFileName);
try
{
  PngOptions pngOptions = new PngOptions();
  SvgRasterizationOptions svgRasterizationOptions = new SvgRasterizationOptions();
  svgRasterizationOptions.setPageWidth(image.getWidth() * scale);
  svgRasterizationOptions.setPageHeight(image.getHeight() * scale);
  pngOptions.setVectorRasterizationOptions(svgRasterizationOptions);

  image.save(inputFileName+".png", pngOptions);
}
finally
{
  image.close();
}

The issues you have found earlier (filed as IMAGINGJAVA-1431) have been fixed in this update.

Hello !

This is the great news. I will try new 19.11 version of Aspose Imaging library in the nearest future.
Actually for now I am using 19.10 version of Aspose Imaging library and I tried your solution.
I found, that your:

SvgRasterizationOptions.setPageSize(Size.to_SizeF());

method started working much better.
It is looks like in this method you resize first and after that executing image rasterization.
It still a little bit blurred but manipulating of Quality option I reach much better quality.
So, tell me please, do you implemented .resize() method for vector images in Image class ?

Thank you in advance !

@mvm,

I suggest you to please consider using following sample code on your end using latest Aspose.Imaging for Java 19.11.

String[] fileNames = new String[]
        {
                "Logotype.svg",
                "sample_car.svg",
                "rg1024_green_grapes.svg",
                "MidMarkerFigure.svg",
                "embeddedFonts.svg"
        };

PointF[] scales = new PointF[]
        {
                new PointF(0.5f, 0.5f),
                new PointF(1f, 1f),
                new PointF(2f, 2f),
                new PointF(3.5f, 9.2f),
        };

for (String inputFile : fileNames)
{
    for (PointF scale : scales)
    {
        String outputFile = String.format(Locale.ENGLISH, "%s_%2.1f_%2.1f.png", inputFile, scale.getX(), scale.getY());
        Image image = Image.load(inputFile);
        try
        {
            image.resize((int)(image.getWidth() * scale.getX()), (int)(image.getHeight() * scale.getY()));
            image.save(outputFile, new PngOptions());
        }
        finally
        {
            image.close();
        }
    }
}

Yes, this is exactly, what I was talking about ! Great, now this is working as it should be.
Did you implemented that only for SVG or for all type of vector formats (like SVG, EMF, WMF) ?

@mvm,

The approach that we have shared applies to SVG as this is what has been requested in associated ticket.