How to stop auto rotate

Discussions on NConvert - the command line tool for image conversion and manipulation

Moderators: XnTriq, helmut, xnview

MitchellFerguson
Posts: 5
Joined: Wed May 31, 2023 9:35 pm

How to stop auto rotate

Post by MitchellFerguson »

When I use nconvert it rotates images after converting them

Code: Select all

nconvert -quiet -out tiff
Looked through the -help but didn't see anything that looked like it would do what I needed and search only turned this up viewtopic.php?f=57&t=38239&p=153383&hil ... te#p153383 Thanks in advance for any help :D
User avatar
xnview
Author of XnView
Posts: 43897
Joined: Mon Oct 13, 2003 7:31 am
Location: France

Re: How to stop auto rotate

Post by xnview »

please use -no_auto_rotate
Pierre.
MitchellFerguson
Posts: 5
Joined: Wed May 31, 2023 9:35 pm

Re: How to stop auto rotate

Post by MitchellFerguson »

xnview wrote: Thu Jun 01, 2023 9:27 am please use -no_auto_rotate
secret options!
Thanks :D

Edit:

I got an error, Bad Argument -'no_auto_rotate'

Edit2:

Changed it from (this causes the error)

Code: Select all

nconvert -no_auto_rotate -out tiff
to

Code: Select all

nconvert -out tiff -no_auto_rotate
and it runs but still rotates the image.
User avatar
xnview
Author of XnView
Posts: 43897
Joined: Mon Oct 13, 2003 7:31 am
Location: France

Re: How to stop auto rotate

Post by xnview »

which version do you use? It works with latest version
Pierre.
MitchellFerguson
Posts: 5
Joined: Wed May 31, 2023 9:35 pm

Re: How to stop auto rotate

Post by MitchellFerguson »

xnview wrote: Fri Jun 02, 2023 8:06 am which version do you use? It works with latest version
Image

I did some more testing and it works fine for images I found online but images I've taken seem to be the problem :| It's weird, exiftool seems to cause the same behavior. I thought if I stripped the file of all the metadata it would behave but it rotated the image as well using

Code: Select all

exiftool -all= 
but then I went to convert it using nconvert and it didn't rotate it.
User avatar
xnview
Author of XnView
Posts: 43897
Joined: Mon Oct 13, 2003 7:31 am
Location: France

Re: How to stop auto rotate

Post by xnview »

could you post an image file
Pierre.
MitchellFerguson
Posts: 5
Joined: Wed May 31, 2023 9:35 pm

Re: How to stop auto rotate

Post by MitchellFerguson »

I think I understand the problem now :) These pictures were taken with my phone and apparently holding your phone sideways results in your photos being taken correctly and holding your phone straight up and down results in them being rotated but software doesn't reflect this for some reason.

I took two example photos to test, this fuse I photographed holding the phone vertically.

Image

It displays as I took it but you can see in the bottom right of the image the orientation is seen as rotated 90 degrees. If I convert it to some other format like tiff then I assume nconvert just reads the orientation information and outputs it that way.

Image

though one curious thing is that once it is converted that Orientation tag isn't present in the program I'm using (Picasa) anymore

Image

(rotated after nconverted to tiff)

The pen image was taken horizontally and has an orientation tag of "Normal"

Image

When converted to tiff it retains its orientation

Image

I tried to attach them but got an error "Sorry, the board attachment quota has been reached."
https://www.mediafire.com/file/eqfzoqca ... s.zip/file
MitchellFerguson
Posts: 5
Joined: Wed May 31, 2023 9:35 pm

Re: How to stop auto rotate

Post by MitchellFerguson »

I enlisted the help of ChatGPT to solve the problem :D If anyone finds themselves in the same boat. Test on copies first. Python code.

Code: Select all

from PIL import Image
import os

def get_image_orientation(image_path):
    # Open the image file
    with Image.open(image_path) as img:
        try:
            # Get the EXIF information
            exif = img._getexif()
            if exif is not None:
                # Check for the orientation tag (tag number 0x0112)
                if 0x0112 in exif:
                    orientation = exif[0x0112]
                    # Rotate the image based on the orientation
                    if orientation == 3:
                        img = img.rotate(180, expand=True)
                    elif orientation == 6:
                        img = img.rotate(270, expand=True)
                    elif orientation == 8:
                        img = img.rotate(90, expand=True)
                    # Update the EXIF information with the new orientation
                    img = img._getexif()
                    img[0x0112] = 1
                    # Save the rotated image
                    img.save(image_path)
                    return "Normal"
        except AttributeError:
            pass
    # If orientation information is not available or invalid, return None
    return None

def scan_directory(directory_path):
    # Iterate over all files in the directory
    for filename in os.listdir(directory_path):
        file_path = os.path.join(directory_path, filename)
        # Check if the file is an image
        if os.path.isfile(file_path) and is_image_file(filename):
            # Check the image orientation
            orientation = get_image_orientation(file_path)
            if orientation is None:
                continue #print(f"{filename}: not rotated (orientation information not available)")
            elif orientation == "Normal":
                continue #print(f"{filename}: not rotated")
            else:
                print(f"{filename}: rotated")

def is_image_file(filename):
    # Check if the file extension corresponds to an image format
    image_extensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp"]
    return any(filename.lower().endswith(ext) for ext in image_extensions)
	
# Provide the path to the directory you want to scan
directory_path = "path/to/directory"
scan_directory(directory_path)