Regex in conditions

Started by Tobias, June 08, 2018, 05:03:12 PM

Previous topic - Next topic

Tobias

Hi!

Currently I'm having quite a hard time figuring out some conditional expressions for Exiftool to work with.
Big picture: I'm trying to run Exiftool with a set of argument files on every JPEG file that gets uploaded to an image gallery website.
One of these argument files should try to fix the messed up EXIF data that was written by my mobile phone (HTC One M8). So first I'm trying to match the exact type of make and model case-insensitive (because both upper and lower case versions exist depending on firmware version ::)).
Took me a while to figure out why this failed:


> exiftool -g -n -f -make -model IMG_20180504_182516.jpg
---- EXIF ----
Make                            : HTC
Camera Model Name               : m8

> exiftool -model -if '$make =~ /^htc$/i' IMG_20180504_182516.jpg
1 files failed condition

> exiftool -v3 IMG_20180504_182516.jpg
[...]
+ [IFD0 directory with 11 entries]
  | 0)  Make = HTC
  |     - Tag 0x010f (4 bytes, string[4]):
  |         001e: 48 54 43 00                                     [HTC.]
  | 1)  Model = m8
  |     - Tag 0x0110 (3 bytes, string[3]):
  |         002a: 6d 38 00                                        [m8.]
[...]


It seems the regex isn't matching at the line/string end ($) modifier. I also tried to match the string terminating null-byte sequence with the regular expressions ^htc\0$ and ^htc\x00$, but to no avail.

Second problem I ran into was trying to remove the ImageWidth and ImageHeight tags from IFD1 (they had a value of 0 anyway) and move/add them to IFD0 where they belong as far as I understand (correct?).
Looks like this only works when specifically defining which group to use:


> exiftool -g -a -f -n -EXIF:IFD1:All -if '$imagewidth eq 0' IMG_20180504_182516.jpg
1 files failed condition

> exiftool -g -a -f -n -EXIF:IFD1:All -if '$EXIF:IFD1:imagewidth eq 0' IMG_20180504_182516.jpg
---- EXIF ----
Image Width                     : 0
Image Height                    : 0
Compression                     : 6
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : 2
Thumbnail Offset                : 2800
Thumbnail Length                : 18474


Is this expected behaviour?

Tobias

StarGeek

See -if option docs note #3 which requires reading reading a bit of -p option docs.

tl;dr, use $$/ to prevent the $/ from being interpreted as a new line character.

Myself, I tend to use (?:$) (or just ($) if I'm not capturing anything) as it won't break anything if I need to copy/paste the regex into something like Regex101.com.

For the second question, if you run exiftool -a -g -ImageWidth IMG_20180504_182516.jpg you'll see that there is two ImageWidth tags.  File:ImageWidth is that actual size of the image and EXIF:ImageWidth which can be set to any value.  The File group tag has priority if you don't indicate a group (you just need Exif:ImageWidth). 

Also, you might want to use == instead of eq in this case as you are doing a numeric comparison rather than a string comparison.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

Phil Harvey

Note that according to the EXIF specification, EXIF:ImageWidth should not exist in JPEG images.  (The -validate option will warn about this.)

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Tobias

Thank you both of you!
I read to docs more than once, but also changed the conditions numerous times until I ended up using the $ in that regex. Should have read the docs once more!
Regarding the two ImageWidth tags you are obviously right, too, StarGeek.

Great to have the -validate option. I didn't know it existed. Will have a closer look at it!