Free Support Forum - aspose.com

How to improve quality of SVG during resizing (Java)

Hello, Aspose Team !

In my Java code, I try to load images, using Aspose.Imaging library.
For reach better quality of generated document we want to use SVG format of image in logo.
The method should read different type of images,
then resize to box of document and reload to BufferedImage for next processing.
I found example of code in your manual for WMF format and implemented the same for SVG format.
Here is my method:

public BufferedImage readToImage(InputStream stream, float width, float height) {
	try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
		com.aspose.imaging.Image image = com.aspose.imaging.Image.load(stream);
		image.resize(Math.round(width), Math.round(height));
		if (image != null) {
			com.aspose.imaging.imageoptions.PngOptions options = new com.aspose.imaging.imageoptions.PngOptions();
			if (image.getFileFormat() == com.aspose.imaging.FileFormat.Svg) {
				options.setVectorRasterizationOptions(new com.aspose.imaging.imageoptions.SvgRasterizationOptions() {{
					setBackgroundColor(com.aspose.imaging.Color.getTransparent());
					setPageWidth(width);
					setPageHeight(height);
				}});
			}
			if (image.getFileFormat() == com.aspose.imaging.FileFormat.Wmf) {
				options.setVectorRasterizationOptions(new com.aspose.imaging.imageoptions.WmfRasterizationOptions() {{
					setBackgroundColor(com.aspose.imaging.Color.getTransparent());
					setPageWidth(width);
					setPageHeight(height);
				}});
			}
			options.setCompressionLevel(0);
			image.save(output, options);
			options.close();
			image.close();
		}
		return ImageIO.read(new ByteArrayInputStream(output.toByteArray()));
	} catch(Exception fail) {
		logger.error(fail.toString());
		return null;
	}
}

I tried that method with Aspose.Imaging 18.11 version, as well with newest 19.4 version of library,
but unsuccessful, because every time I got an Exception as below:

class com.aspose.imaging.internal.Exceptions.NotImplementedException:
The method or operation is not implemented
com.aspose.imaging.fileformats.svg.SvgImage.resize(Unknown Source)
com.aspose.imaging.Image.resize(Unknown Source)
com.infor.daf.docgen.api.Template.readToImage(Template.java:2038)
...

It is throwing on line: image.resize(Math.round(width), Math.round(height));

Our company have a bought license, but first of all I interesting:
Is the problem with .resize() method still exists in library ?
Or I have implemented something wrong ?

@mvm,

I have observe the sample code shared by you and have observed the information provided as well. I request you to please provide the source file reproducing issue on your end that we may verify on our end to help you further.

NotImplementedException.zip (31.4 KB)
jUnitTest with clean implementation of using Aspose.Imaging.
An Exception occurred in line:65 when calling .resize() method.

@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 !