Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How rotate image without quality loss #418

Closed
1 of 2 tasks
dalibor-sanzeru opened this issue Apr 4, 2019 · 5 comments
Closed
1 of 2 tasks

How rotate image without quality loss #418

dalibor-sanzeru opened this issue Apr 4, 2019 · 5 comments
Labels

Comments

@dalibor-sanzeru
Copy link

dalibor-sanzeru commented Apr 4, 2019

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am using the latest version of Magick.NET

System Configuration

  • Magick.NET version: latest for
  • Environment (Operating system, version and so on): Windows 10 pro

Question

How can I rotate image without quality loss? Currently with this code when multiple times rotation is called - quality is degradating with each rotation. When I Set image.Quality = 100, quality isnt degradated, but image has full size -> e.g. from 200kB -> 600kB.

THANKS!

protected virtual Size RotateAndSaveImage(double degrees, string imageFullPath)
        {
            Size newSize = null;
            byte[] imageContent = this.osService.ReadFileContent(imageFullPath);

            using (var stream = new MemoryStream(imageContent))
            {
                stream.Position = 0;

                using (MagickImage image = new MagickImage(stream))
                {
                    image.Rotate(degrees);
                    newSize = new Size(image.Width, image.Height);

                    this.osService.SaveFileContent(imageFullPath, image.ToByteArray());
                }
            }

            return newSize;
        }
@dlemstra
Copy link
Owner

dlemstra commented Apr 4, 2019

Do you have some example images that show de degradation? There can be some when you do a rotation at an "odd" angle like 15.7 and I don't think that you can avoid that. And what are your input images?

@dalibor-sanzeru
Copy link
Author

dalibor-sanzeru commented Apr 4, 2019

Hi,
thanks for fast response.
User is rotating 90 degrees (turn to right) e.g. 12times in a row (because he is user :) ).

I was experimenting with quliaty. When is set quality to 100 - image is after rotations without loss but have 600KB e.g.

Images are attached. The worst one is rotated multiple times.
notrotated
rotated

@dlemstra
Copy link
Owner

dlemstra commented Apr 4, 2019

That last image is not rotated?

If it is possible you should avoid the intermediate rotations when you save the image. When the user has done 10 times a 9 degree rotation you somehow only remember the total rotation (90 degrees) and rotate the original image 90 degrees and then save it.

@dalibor-sanzeru
Copy link
Author

Scenario:
User on web-UI has button - 'rotate-90-degrees-right'.
I need to rotate each time he clicks.

  • First time on upload I am using quality 80 percent => image.Quality = 80.

  • Then I rotate: When then load image from storage and call rotate image.quality is get on image.Quality automatically to 80 again.

  • Each time by rotation quality image is decreased to 80 by calling this function above (RotateAndSaveImage)

  • Question also is: Can I rotate image without quality loss? Something like use rotation with 100 quality and then save as 80. When i set quality to 100 it has 600KBytes after first rotation.

@dlemstra
Copy link
Owner

When a JPEG image is opened, edited and saved again it results in additional image degradation. It's very important to minimize the number of editing sessions between the initial and final version of a JPEG image. My advise would be to keep a copy of the original image and do your operations on that image and then save the result as a separate image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants