Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to write GPS Exif Metadata #3633

Closed
RollingSquirrel opened this issue Apr 16, 2023 · 4 comments
Closed

Unable to write GPS Exif Metadata #3633

RollingSquirrel opened this issue Apr 16, 2023 · 4 comments
Labels

Comments

@RollingSquirrel
Copy link

Modify Metadata - GPS Exif Metadata

What are you trying to achieve?

I am trying to add geo location data to an existing image without this information.
One example is taking a screenshot and adding exif data with the withMetadata method.

When you searched for similar issues, what did you find that might be related?

I have found several discussions and issues about the metadata and the development from using other libraries to using sharp itself.
This allowed me to find several samples that work with common exif data, e.g. Make or Model.
I have found the related documentation here: https://sharp.pixelplumbing.com/api-output#withmetadata

Code Sample

// given any image
const image = sharp(imageBuffer);

const outputImage = await image.withMetadata({
	exif: {
		IFD0: {
                        // these work without issues
			Make: 'Make',
			Model: 'Model',
			// I am unable to set these - strings are expected (error messages)
                        GPSLatitudeRef: 'N',
			GPSLatitude: '51,30,12.3456',
			GPSLongitudeRef: 'W',
			GPSLongitude: '0,7,54.321'
		}
	}
}).toBuffer();

I have tried to process an image with existing location data.
The information is not lost after processing with image.withMetadata().toBuffer().
I think I am probably not passing the right arguments to the withMetadata call but I am unable to find similar issues or further documentation.

Any help is appreciated.

@lovell
Copy link
Owner

lovell commented Apr 17, 2023

EXIF stores GPS metadata in IFD3, see also #2767

@RollingSquirrel
Copy link
Author

Thanks for the quick reply.

I have changed the example according to the referenced issue:

image.withMetadata({
		exif: {
			IFD0: {
				Make: 'TestMake',
				Model: 'TestModel'
			},
			IFD3: {
				GPSLatitudeRef: 'N',
				GPSLatitude: '55/1 43/1 5287/100',
				GPSLongitudeRef: 'E',
				GPSLongitude: '37/1 35/1 5571/100',
				GPSAltitudeRef: 'Sea level',
				GPSAltitude: '90514/693',
				GPSSpeedRef: 'K',
				GPSSpeed: '114272/41081',
				GPSImgDirectionRef: 'M',
				GPSImgDirection: '192127/921',
				GPSDestBearingRef: 'M',
				GPSDestBearing: '192127/921',
				GPSDateStamp: '2020:07:28'
			}
		}
	});

Using sharp again to parse the processed image and then parsing the raw exif data results in the expected output. 😃

Thanks for the guidance - I had trouble finding any reference to this issue.

I suggest adding a quick link / some information where to search (if there is external documentation available) to the documentation of the withMetadata function. Providing types for the IFDX parameters would be awesome if possible.

@lovell
Copy link
Owner

lovell commented Apr 17, 2023

Commit d6b60a6 updates the documentation to add an EXIF GPS example, thanks for the suggestion.

Happy to accept a PR to improve the TypeScript definitions, if you're able.

@lovell lovell closed this as completed Apr 17, 2023
@RollingSquirrel
Copy link
Author

I have found the following:
DefinitelyTyped/DefinitelyTyped#53942
#2760

These type definitions seem to be providing some help for the IFD0 values.
From my limited understanding of the EXIF specification I cannot tell if these are actually typed correctly.
But this looks like a great starting point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants