Have an account?

» Forgot your username or password?

» Register for Free

Piczard v1.x Support Forum Archive


The Piczard v1.x Support Forum is no longer active. This is a static read-only copy of it.
It's not possible to post new messages or replies.
Search | Active Topics

Can you auto rotate - based on EXIF orientation?
gabloom
#1 Posted : Monday, November 21, 2011 10:13:09 PM(UTC)
Rank: Newbie

Joined: 11/21/2011(UTC)
Posts: 1
Points: 3

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Is it possible to read the EXIF, and based on the orientation, auto rotate the image?
sergioturolla
#2 Posted : Tuesday, November 22, 2011 5:59:25 PM(UTC)

Rank: Administration

Joined: 3/11/2011(UTC)
Posts: 159
Points: 489

Thanks: 0 times
Was thanked: 28 time(s) in 28 post(s)
Hi,
Yes, this kind of filter can be easily implemented.

Example:
Code:
using System;

using CodeCarvings.Piczard;
using CodeCarvings.Piczard.Processing;

namespace MyNameSpace
{
    public class MyAutoRotateFilter
        : ImageProcessingFilter
    {

        public MyAutoRotateFilter()
        {
        }

        protected override void Apply(ImageProcessingActionExecuteArgs args)
        {
            // This filter uses another ImageProcessingFilter returned by the "LoadImageProcessingActions" method
            throw new Exception("Cannot invoke the Apply method.");
        }

        protected override void LoadImageProcessingActions(ImageProcessingActionLoadArgs args)
        {
            // Search for the orientation EXIF property
            int orientationPropId = 274;
            int propertyIndex = Array.IndexOf(args.SourceImage.PropertyIdList, orientationPropId);
           
            if (propertyIndex >= 0)
            {
                // Property found
                ushort orientation = BitConverter.ToUInt16(args.SourceImage.GetPropertyItem(orientationPropId).Value, 0);
               
                // Here is a lsit of possible values: http://www.impulseadventure.com/photo/exif-orientation.html
                switch (orientation)
                {
                    case 1:
                        // TopLeft
                        // -> Nothing to do
                        break;
                    case 2:
                        // TopRight
                        // -> Must flip horizontal
                        args.LoadImageProcessingActions(ImageTransformation.FlipHorizontal);
                        break;
                    case 3:
                        // BottomRight
                        // -> Must fotate 180
                        args.LoadImageProcessingActions(ImageTransformation.Rotate180);
                        break;
                    case 4:
                        // BottomLeft
                        // -> Must flip vertical
                        args.LoadImageProcessingActions(ImageTransformation.FlipVertical);
                        break;
                    case 5:
                        // LeftTop
                        // -> Must rotate 90 and then flip horizontal
                        args.LoadImageProcessingActions(new ImageTransformation(100F, 90F, true, false));
                        break;
                    case 6:
                        // RightTop
                        // -> Must rotate 90
                        args.LoadImageProcessingActions(ImageTransformation.Rotate90);
                        break;
                    case 7:
                        // RightBottom
                        // -> Must rotate 270 and then flip horizontal
                        args.LoadImageProcessingActions(new ImageTransformation(100F, 270F, true, false));
                        break;
                    case 8:
                        // LeftBottom
                        // -> Must rotate 270
                        args.LoadImageProcessingActions(ImageTransformation.Rotate270);
                        break;
                }

            }
        }

    }
}


Then you can use the custom filter as any other Piczard filter.
Example:
Code:
// Usage demo 1
new MyAutoRotateFilter().SaveProcessedImageToFileSystem("~/Source.jpg", "~/Dest1.jpg");

// Usage demo 2
ImageProcessingJob job = new ImageProcessingJob();
job.Filters.Add(new MyAutoRotateFilter());
job.Filters.Add(new ScaledResizeConstraint(100, 100));
job.Filters.Add(CodeCarvings.Piczard.Filters.Colors.DefaultColorFilters.Grayscale);
job.SaveProcessedImageToFileSystem("~/Source.jpg", "~/Dest2.jpg");


Kind regards

Sergio Turolla
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.