Reducing a JPGs resolution without losing quality

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
Craig321
Posts: 3
Joined: 2016-08-09T04:57:37-07:00
Authentication code: 1151

Reducing a JPGs resolution without losing quality

Post by Craig321 » 2016-08-09T05:03:58-07:00

Hi,

I'm giving ImageMagick a go as no matter what I do, the standard C# image functions are useless and will lose image quality.

ImageMagick is giving a slightly better output, but I'm still having trouble.

Original Image: removed, problem solved
Resized: removed, problem solved
Perfect Photoshop resize, use to compare: removed, problem solved

The code:

Code: Select all

	using (MagickImage mImage = new MagickImage(@"c:\users\craig\desktop\orig.jpg))
	{
		mImage.Quality = 82;
		mImage.Density = new Density(72);
		//mImage.UnsharpMask(2, 0.5, 0.7, 0);
		mImage.ColorSpace = ColorSpace.RGB;
		//mImage.FilterType = FilterType.Mitchell;
		
		mImage.Resize(400, 0);
		mImage.Write(output);
	}
The link to the resized image above was generated use the exact code above. I have also been trying the various filter types and unsharp masks, but had no luck.

I have also tried the mImage.Thumbnail() method, but no luck there.

I linked a Photoshop resized version of the image, which is spot on, no visible quality loss or change.

As you can see, it seems to lose some colour or sharpness, but I just can't figure out how to fix it.

Any ideas?

Thanks.
Last edited by Craig321 on 2016-08-09T09:18:54-07:00, edited 1 time in total.

User avatar
fmw42
Posts: 25561
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reducing a JPGs resolution without losing quality

Post by fmw42 » 2016-08-09T08:57:50-07:00

This works fine for me in command line:

Code: Select all

convert o_1apnir2tu29c12iknpq1q4pij9f.jpg -unsharp 0x2+1+0 -resize 400 -quality 82 -density 72 result.jpg
I do not know Magick.Net, but make your unsharp equivalent to mine above. See http://www.imagemagick.org/script/comma ... hp#unsharp

You may also want to put your quality and density assignments at the end of the command where they properly should be.

Craig321
Posts: 3
Joined: 2016-08-09T04:57:37-07:00
Authentication code: 1151

Re: Reducing a JPGs resolution without losing quality

Post by Craig321 » 2016-08-09T09:17:18-07:00

Hmm, how strange, using that in .NET is making the image really dark for some reason, so I guess those commands don't really translate directly in to .NET?

Code: Select all

	using (MagickImage mImage = new MagickImage(@"c:\users\craig\desktop\orig.jpg"))
	{
		mImage.UnsharpMask(0, 2, 1, 0);
		mImage.Resize(400, 0);
		mImage.Quality = 82;
		mImage.Density = new Density(72);
		
		mImage.Write(@"c:\users\craig\desktop\output.jpg");
	}
	
I did have a further play earlier though, before seeing your reply, and came up with this which is almost spot on (the difference is unnoticeable unless you sit and switch between the two images and look closely):

Code: Select all

			using (MagickImage mImage = new MagickImage(@"c:\users\craig\desktop\orig.jpg"))
			{
				mImage.Quality = 90;
				mImage.Density = new Density(96);
				mImage.UnsharpMask(12, 6, 0.5, 0); // GIMP unsharp settings
				mImage.ColorSpace = ColorSpace.sRGB;
				mImage.FilterType = FilterType.Lanczos;
				mImage.Resize(400, 0);

				mImage.Write(@"c:\users\craig\desktop\output.jpg");
			}
			
Above code works really nicely in case anyone is looking for this :)

User avatar
fmw42
Posts: 25561
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reducing a JPGs resolution without losing quality

Post by fmw42 » 2016-08-09T09:40:24-07:00

You need to look at the magick.net documentation on unsharp and see what arguments match the documentation link I provided for command line unsharp.

Craig321
Posts: 3
Joined: 2016-08-09T04:57:37-07:00
Authentication code: 1151

Re: Reducing a JPGs resolution without losing quality

Post by Craig321 » 2016-08-10T01:03:20-07:00

The arguments definitely match, it'd be the unsharp mask which might raise questions, but I double checked and they definitely match to your command line command. Even so, with the command you posted, I get a really dark image. I wonder if the .NET library does something different, or maybe a default somewhere it different... not sure.

Either way, my last block of code is pretty much spot on. Thank you for your help :)

User avatar
fmw42
Posts: 25561
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reducing a JPGs resolution without losing quality

Post by fmw42 » 2016-08-10T09:10:29-07:00

Maybe Net does not auto calculate the radius when set to 0, like the command line does. If not, then try radius=2 or radius=3, then 2,1,0

User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Reducing a JPGs resolution without losing quality

Post by dlemstra » 2016-08-13T15:12:06-07:00

This was a bug in the Sample and will be fixed in the next release of Magick.NET. Not sure if the bug report post is from @Craig321 so here is the link to it: https://magick.codeplex.com/discussions/657068
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

Post Reply