Results 1 to 10 of 10

Thread: Preserve original datestamp with mogrify?

  1. #1
    Join Date
    Nov 2005
    Location
    Lincolnshire, UK
    Beans
    1,461
    Distro
    Ubuntu 10.04 Lucid Lynx

    Preserve original datestamp with mogrify?

    I must be missing something... The following code produces new files with the current date/time when resizing whereas I wish to retain the original date/time.
    Code:
    mogrify -resize 75% *.jpg
    How do I preserve the original date/time stamp on files that are resized with mogrify?

    (or should I be using a different command? )

  2. #2
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: Preserve original datestamp with mogrify?

    Do you mean the timestamp on the file itself, or the date & time information held within the photograph?

    If the former, then of course the timestamp will change, as the file has been modified.

    If you mean the latter, then sorry I don't have the answer for you.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  3. #3
    Join Date
    Nov 2005
    Location
    Lincolnshire, UK
    Beans
    1,461
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Preserve original datestamp with mogrify?

    Thanks Paddy. I simply want to retain the original file timestamp after resizing to help find files in future.

    I was hoping that mogrify would allow this in a similar way that "cp -p" preserves the original timestamp when copying files.

    The EXIF timestamp info does remain unchanged after resizing but this does not really help when using a file manager.

    Ideas please anyone?

  4. #4
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: Preserve original datestamp with mogrify?

    I think you may be out of luck. In your place, I would write a script to record the timestamps, perform the mogrify, and restore the timestamps (using 'touch').

    It's an unusual request. I'm curious as to why you would want to do this?
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  5. #5
    Join Date
    Nov 2005
    Location
    Lincolnshire, UK
    Beans
    1,461
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Preserve original datestamp with mogrify?

    Quote Originally Posted by Paddy Landau View Post
    ...It's an unusual request. I'm curious as to why you would want to do this?
    Thanks Paddy. I have, for example, a directory with around 450 large jpgs in it, with dates over a 10 month period. I need to resize all of these to make the filesizes smaller. While I could sub-divide the jpgs into many different directories, it would be easier to leave them in one directory and then find selected ones by the original dates. Unfortunately, mogrify will give them all the same date and almost the same time.

    I realise I could write a script (if I could figure it all out!) but was hoping to find a quick and easy method, preferably using a single command.

    Anyone - Are there any other commands and/or short scripts that may do this?

  6. #6
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: Preserve original datestamp with mogrify?

    I've knocked up something quick for you, Zill.

    Because it's a knock-up, you need to use it most carefully as instructed. It's not idiot-proof.

    Set-up

    1. Create a directory in your home directory called bin.
    2. Reboot, which will cause ~/bin to be added to your path automatically.
    3. Create a file within ~/bin called mogtime (you may choose a different name).
    4. Open mogtime (with gedit or your favourite text editor), paste in the code that I've pasted at the bottom of this post, and save it.
    5. Change the permissions of mogtime to allow execution (find mogtime in Nautilus; right-click and select Properties > Permissions > Execute > Allow executing file as program).

    How to use

    1. Copy the files that you want to mogrify into an empty directory. This is important, because if my code has an error, you will still have your original files.
    2. Open mogtime and find the line within the code (line 51) that contains nothing but a backslash. Before the backslash, type all the parameters exactly as you want to pass to mogrify, but leave off the name of the file. For example, if you wanted to call mogrify with each file as:
      mogrify -flip -implode 5 filename
      then you would change the line so that that section of mogtime looked like this:
      Code:
      mogrify \
              -flip -implode 5 \
              "${FILENAME}"   # Change the line above this one.
    3. Save your changes.
    4. In a terminal, navigate to the directory with your copied files (not the original files).
    5. Enter the following command:
      Code:
      find -xdev -maxdepth 1 -name '*.jpg' -execdir mogtime {} \;
      If your files are *.png, change *.jpg in the command to *.png (or whatever you need). This is the command that will run mogrify (via mogtime) on every file within that directory.

    Although I've tested the script, I have not done so thoroughly. Therefore, please be sure to run it in the copied directory and not on your original files!

    And here is the code... (I hope it works well)
    Code:
    #!/bin/bash
    
    # Pass a file to mogrify, but retain the timestamp to the nearest second.
    #
    # Amend the mogrify line below with the correct parameters.
    #
    # Parameters:
    #    The input file and nothing else.
    #
    # Return codes:
    #    101    The file does not exist or is not a regular file
    #    102    Did not have permission to read the file
    #    103    Did not have permission to modify the file
    #    Any other code: As returned by mogrify.
    
    # Find the last parameter.
    FILENAME="${1}"
    
    # Check that the file exists and is readable and writeable.
    if [[ ! -f ${FILENAME} ]]
    then
        echo "The file does not exist or it is not a regular file:  ${FILENAME}" >&2
        exit 101
    fi
    if [[ ! -r ${FILENAME} ]]
    then
        echo "I cannot read the file: ${FILENAME}" >&2
        exit 102
    fi
    if [[ ! -w ${FILENAME} ]]
    then
        echo "I cannot modify the file: ${FILENAME}" >&2
        exit 103
    fi
    
    # Record the time stamp to the nearest second.
    TIMESTAMP="$( ls -l --time-style='+%Y%m%d%H%M.%S' "${FILENAME}" | cut  --delimiter=' ' --fields=6 )"
    
    # Mogrify the file.
    
    #  ==================================================================================================
    # Insert the parameters to mogrify on the line below 'mogrify'. Be sure  to end the line with a
    # single backslash.
    #  ==================================================================================================
    mogrify    \
        \
        "${FILENAME}"    # Change the line above this one.
    #  ==================================================================================================
    
    RETURNCODE=${?}
    
    # Reset the timestamp.
    touch -t ${TIMESTAMP} "${FILENAME}"
    
    # Exit with the appropriate code.
    exit ${RETURNCODE}
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  7. #7
    Join Date
    Jan 2006
    Location
    U.S.A.
    Beans
    971
    Distro
    Ubuntu 15.10 Wily Werewolf

    Re: Preserve original datestamp with mogrify?

    Nice work. I'd say somebody owes somebody a pint.
    Favorite man page quote: "The backreference \n, where n is a single digit, matches the substring previously matched by the nth parenthesized subexpression of the regular expression." [excerpt from grep(1)]

  8. #8
    Join Date
    Nov 2005
    Location
    Lincolnshire, UK
    Beans
    1,461
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Preserve original datestamp with mogrify?

    Quote Originally Posted by Paddy Landau View Post
    I've knocked up something quick for you, Zill...
    Many thanks for that Paddy - your script works perfectly and does exactly what I wanted.

    As BoneKracker suggests, I really owe you a pint. However, I hope can make do with a bucket of popcorn for now.


  9. #9
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    5,263
    Distro
    Ubuntu

    Re: Preserve original datestamp with mogrify?

    Quote Originally Posted by Zill View Post
    ... I really owe you a pint.
    Pay it forward instead. One day you'll help someone else in an area where you are good.
    Always make regular backups of your data (and test them).
    Visit Full Circle Magazine for beginners and seasoned Linux enthusiasts.

  10. #10
    Join Date
    Nov 2005
    Location
    Lincolnshire, UK
    Beans
    1,461
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Preserve original datestamp with mogrify?

    Quote Originally Posted by Paddy Landau View Post
    Pay it forward instead...
    Will do - and thanks again for all your work on this. Much appreciated and I am sure your script will be of use to others in the future finding this thread via search engines.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •