News:

2023-03-15 Major improvements to the new Geolocation feature

Main Menu

Cannot strip GPS EXIF data from JPG files

Started by Alastair Gordon, February 02, 2023, 11:36:47 PM

Previous topic - Next topic

Alastair Gordon

I have just started using exiftool, and it seems like an amazing product (thanks, Phil). It works flawlessly for extracting and displaying EXIF data, but I also need to strip GPS data from the EXIF metadata in JPG files. I followed Phil's instructions, but I seem unable to create a new JPG file with the GPS data removed. I have tried:

eval(`exiftool -gps:all= image.jpg`);
eval(`exiftool -gps:all= -overwrite_original image.jpg`);

but neither overwrite the target file with GPS data removed. I admire the difficulty of overwriting existing files, but I seem unable to get around it and update my jpg files. Perhaps my ignorance of eval in PHP is part of the problem. Any suggestions are much appreciated. I am definitely a newbie.

Website in Apache running on Windows 10.
Alastair Gordon

StarGeek

Test your commands on the command line before trying to call them from another script.

Use FAQ #3 to verify the locations of the GPS tags.  They may show up in the XMP group as well.  Composite tags can be ignored, as they are not actually embedded in the file.

Example of the most common locations.
C:\>exiftool -G1 -a -s -gps* y:\!temp\Test4.jpg
[GPS]           GPSVersionID                    : 2.3.0.0
[GPS]           GPSLatitudeRef                  : North
[GPS]           GPSLatitude                     : 40 deg 41' 21.12"
[GPS]           GPSLongitudeRef                 : West
[GPS]           GPSLongitude                    : 74 deg 2' 40.20"
[GPS]           GPSAltitudeRef                  : Above Sea Level
[GPS]           GPSAltitude                     : 10 m
[XMP-exif]      GPSAltitude                     : 10 m
[XMP-exif]      GPSAltitudeRef                  : Above Sea Level
[XMP-exif]      GPSLatitude                     : 40 deg 41' 21.12" N
[XMP-exif]      GPSLongitude                    : 74 deg 2' 40.20" W
[Composite]     GPSAltitude                     : 10 m Above Sea Level
[Composite]     GPSLatitude                     : 40 deg 41' 21.12" N
[Composite]     GPSLongitude                    : 74 deg 2' 40.20" W
[Composite]     GPSLatitudeRef                  : North
[Composite]     GPSLongitudeRef                 : West
[Composite]     GPSPosition                     : 40 deg 41' 21.12" N, 74 deg 2' 40.20" W

Finally, depending upon what programs you use, the data may be in an XMP sidecar file, which will have the same name as the image except with a .xmp extension.   This is most common with RAW file types such as NEF/CR2.
* 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).

Alastair Gordon

Thanks, StarGeek, but I still do not know the code I need to simply strip the gps data and then write the modified content, either to the same jpg file, or to another file. Exiftool has worked perfectly for me for capturing and displaying exif data on my website. The problem is exclusively with stripping location data from a jpg file and saving the new GPS-less content into the same or a different file. Please remember my newbie status (that hopefully is diminishing every day). Thanks again.

By the way, do I get an email notification when a response is posted? I'm also a newbie on this forum.
Alastair Gordon

Phil Harvey

Hi Alastair,

You can click on the "No Alerts or Emails" button at the bottom of the posts to change this to get alerts if you want.  (I think the forum emails are working presently.)

There are many ways to do what you want, but I would suggest something like this:

exiftool -location:all= FILE

This should delete GPS tags as well as other location-specific information such as City, Country, etc.

Here is another option (as mentioned by StarGeek).  This command deletes any tag with a name starting with "GPS":

exiftool "-gps*=" FILE

These commands and the commands that you mentioned (-gps:all=) should all overwrite the target file, with the original file being preserved with an "_original" suffix on the file name unless the -overwrite_original option is used.

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

StarGeek

Quote from: Alastair Gordon on February 03, 2023, 09:03:58 AMThanks, StarGeek, but I still do not know the code I need to simply strip the gps data and then write the modified content, either to the same jpg file, or to another file.

Sorry for not clairfying.  The command you listed would be the correct command for removing the EXIF GPS tags

Example:
C:\>exiftool -G1 -a -s -gps* y:\!temp\Test4.jpg
[GPS]          GPSVersionID                    : 2.3.0.0
[GPS]          GPSLatitudeRef                  : North
[GPS]          GPSLatitude                    : 40 deg 41' 21.12"
[GPS]          GPSLongitudeRef                : West
[GPS]          GPSLongitude                    : 74 deg 2' 40.20"
[Composite]    GPSLatitude                    : 40 deg 41' 21.12" N
[Composite]    GPSLongitude                    : 74 deg 2' 40.20" W
[Composite]    GPSPosition                    : 40 deg 41' 21.12" N, 74 deg 2' 40.20" W

C:\>exiftool -P -overwrite_original -gps:all= y:\!temp\Test4.jpg
    1 image files updated

C:\>exiftool -G1 -a -s -gps* y:\!temp\Test4.jpg

C:\>

Phil's example is more complete.  But neither of these will edit data in a XMP sidecar file unless you explicitly name that file, i.e.  use exiftool -location:all= file.NEF will not edit the file.XMP sidecar, which would be the most likely location in the case of RAW file types.

All of these commands create backup files.  Add the -overwrite_original option to suppress this.

If you want to leave the original file alone and create a new copy with the edited information, see the -o (-out) option.
* 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).

Alastair Gordon

Many thanks, Phil and StarGeek. I certainly appreciate your timely responses. Just to be clear, the ONLY type of image files I am dealing with are .jpg. I guess that means that I am not dealing with a .XMP sidecar file, is that correct?

Also, when I use the following eval in my PHP code for a different part of the website, I capture all the EXIF data from the same .jpg files and everything works perfectly:

eval('$exif=' . `exiftool -php $origFile`);

But in the following code, I am attempting to strip GPS/location data and overwrite the original .jpg file (fear not, I have a backup):

eval(`exiftool -gps:all= -overwrite_original $origJPG`);
eval(`exiftool -location:all= -overwrite_original $origJPG`);

where $origJPG is the full path to the file, starting with https://domain.com/. It also doesn't work if the path is within the same directory (htdocs) as the PHP code with the eval statements.

The problem is that the original file is never overwritten and no new files appear following execution of either (or both) of these eval commands. I know I am doing something wrong, either in the eval statements or in the surrounding PHP script. But I am baffled!

Alastair Gordon

StarGeek

Quote from: Alastair Gordon on February 03, 2023, 12:22:48 PMwhere $origJPG is the full path to the file, starting with https://domain.com/.

Exiftool can only operate on local file paths.  It cannot read files from an URL.
* 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).

Alastair Gordon

Your right, StarGeek. In fact, I used local paths in my test script with the same result. But here are some interesting new observations.

eval(`exiftool -gps:all= $origFile`); in my test PHP script does, in fact, work and successfully strips the GPS data from the jpg file AND overwrites the original file. However, it throws an error and stops the PHP script at that point. BUT when I run the same script on the same file a 2nd time, all the GPS data is gone. So exiftool did exactly what I asked it to do, but it threw an error and stopped the rest of the PHP script from executing.

The error message was:
Parse error: syntax error, unexpected 'image' (T_STRING) in C:\Apache\htdocs\admin\exifDisplayAnyFile.php(149) : eval()'d code on line 1

By the way when I run the same script on the same file a 2nd time, the same error message is thrown and the PHP script aborts. But it is clear to see that all the GPS data is then gone.

Progress, but still baffled.
Alastair Gordon

StarGeek

Quote from: Alastair Gordon on February 04, 2023, 03:22:06 PMHowever, it throws an error and stops the PHP script at that point. BUT when I run the same script on the same file a 2nd time, all the GPS data is gone. So exiftool did exactly what I asked it to do, but it threw an error and stopped the rest of the PHP script from executing.

The error message was:
Parse error: syntax error, unexpected 'image' (T_STRING) in C:\Apache\htdocs\admin\exifDisplayAnyFile.php(149) : eval()'d code on line 1

This isn't an exiftool error response. A quick search seems to indicate there is probably a typo on or around the mentioned line.

Looking deeper, I think the problem is that you're using eval to process the output of exiftool as if it was PHP code.  Your command would cause exiftool to respond with
  1 image files updated
And then eval tries to process that as php.
* 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

Quote from: StarGeek on February 05, 2023, 02:25:11 AMI think the problem is that you're using eval to process the output of exiftool as if it was PHP code.

I thought that too, but I didn't look deeper.  I should have remembered the exiftool -php option example:

<?php
eval('$array=' . `exiftool -php -q image.jpg`);
print_r($array);
?>

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

Alastair Gordon

Thanks Phil and StarGeek.

Yes, you're right, even though line 149 is the line throwing the error, eval makes it a line of PHP code, as you say, and that's what PHP is complaining about. In another part of the script where I am just reading the EXIF data, and not trying to change anything, I included the -PHP option, and it has always worked perfectly.

So I will run off and add the -PHP option to eval(`exiftool -gps:all= image.jpg`), and see if that fixes it.

Sorry if a little distracted. Grandbaby number 11 was just born last night. Another name to get wrong!
Alastair Gordon

Phil Harvey

That won't fix it.  The PHP eval is only good for evaluating the exiftool -php option output.

You just want to use backticks, and no eval at all.

Maybe

$result = `exiftool -gps:all= -overwrite_original $origJPG`;
print_r($result);

But I don't know PHP so I'm just guessing here.

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

StarGeek

Quote from: Phil Harvey on February 05, 2023, 09:29:44 AMYou just want to use backticks, and no eval at all.

That matches the examples I was finding.  I couldn't find any examples that included both backticks and eval. They were always separate and backtick examples were assigned to a variable to capture the output.
* 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).

Alastair Gordon

It works! The trick was adding -php to both reading GPS data (as I already had) and to removing GPS data. Here are the working exiftool statements in PHP:

eval('$exif=' . `exiftool -php $origFile`); 
captures GPS data from JPG file into PHP associative array named $exif.

eval(`exiftool -php -gps:all= $origFile`);
Strips GPS data from JPG file, saves backup of original (adds '_original' to original file name), overwrites original JPG file

Another discovery: exiftool will NOT work with file names that contain a SPACE or '%20' (Hex 20 Ascii for space). Easy to create some PHP to solve that problem.

Again, thanks so much for your help.

Alastair Gordon

StarGeek

Quote from: Alastair Gordon on February 05, 2023, 02:06:30 PMAnother discovery: exiftool will NOT work with file names that contain a SPACE or '%20' (Hex 20 Ascii for space).

The rules for the command line still apply.  If you have a space in the name or some other Windows special character, such as an ampersand, then the argument needs to be quoted or the characters escaped.

Exiftool will only see %20 as exactly what it is, the percent sign followed by two numbers.  As previously stated, exiftool won't read an URL.  Or URI, never can keep them straight.
* 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).