0

I am working with pyvips to test some cropping speeds vs. other packages. I need to access my x and y resolution. I am able to, but for some reason pyvips is rounding to the nearest mm. I opened my TIFF in QGIS and the resolution is 0.009330691929342804433,-0.009330691929342804433. I am unsure how to capture this number using pyvips.

img = pyvips.Image.new_from_file(ortho, access='sequential')
print(img.xres)
print(img.yres)

output:
1.0 -type='float'
1.0 - type='float'

Edits

Link to get sample image

Using pyvips 8.12

QGIS Image:

enter image description here

EDITS After @jcupitt answer:

from PIL import Image
from PIL.TiffTags import TAGS

with Image.open('image.tif') as img:
    meta_dict = {TAGS[key] : img.tag[key] for key in img.tag.keys()}
print(meta_dict)

Output:
...'ModelPixelScaleTag': (0.009330691929340133, 0.009330691929339859, 0.0)

Edits After @jcupitt Edits:

Used exifread, values are also in there:

import exifread

f = open('image.tif', 'rb')
tags = exifread.process_file(f)
for tag in tags.keys():
    if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
        print("Key: %s, value %s" % (tag, tags[tag]))

Output:

Key: Image ImageWidth, value 3564
Key: Image ImageLength, value 2640
Key: Image BitsPerSample, value [8, 8, 8, 8]
Key: Image Compression, value Uncompressed
Key: Image PhotometricInterpretation, value 2
Key: Image StripOffsets, value []
Key: Image SamplesPerPixel, value 4
Key: Image RowsPerStrip, value 1
Key: Image StripByteCounts, value []
Key: Image PlanarConfiguration, value 1
Key: Image ExtraSamples, value Unassociated Alpha
Key: Image SampleFormat, value UnsignedUnsignedUnsignedUnsigned
Key: Image Tag 0x830E, value [(0.009330691929340133,), (0.009330691929339859,), (0.0,)]
Key: Image Tag 0x8482, value [(0.0,), (0.0,), (0.0,), (-13646305.785530405,), (5497215.247385725,), (0.0,)]
Key: Image Tag 0x87AF, value [1, 1, 0, 7, 1024, 0, 1, 1, 1025, 0, 1, 1, 1026, 34737, 25, 0, 2049, 34737, 7, 25, 2054, 0, 1, 9102, 3072, 0, 1, 3857, 3076, 0, 1, 9001]
Key: Image Tag 0x87B1, value WGS 84 / Pseudo-Mercator|WGS 84|
Key: Image Tag 0xA480, value <GDALMetadata>
  <Item name="acquisitionEndDate">2021-10-08T20:43:00+00:00</Item>
  <Item name="acquisitionStartDate">2021-10-08T20:13:00+00:00</Item>
  <Item name="isCalibrated">False</Item>
</GDALMetadata>
2
  • @jcupitt I added a link to download a sample image as well as a picture of the image's properties from QGIS. Side question, are you the author of pyvips? I've seen your name all over the place.
    – Binx
    Nov 18, 2021 at 17:20
  • 1
    Yes, I did libvips and pyvips. I added an answer after checking your sample image.
    – jcupitt
    Nov 18, 2021 at 17:53

1 Answer 1

0

The sample image you linked has none of the standard libtiff resolution tags. Here's what you see with tiffinfo (exiftool is similar):

$ tiffinfo image.tif 
TIFFReadDirectory: Warning, Unknown field with tag 33550 (0x830e) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33922 (0x8482) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34735 (0x87af) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34737 (0x87b1) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 42112 (0xa480) encountered.
TIFF Directory at offset 0x8 (8)
  Image Width: 3564 Image Length: 2640
  Bits/Sample: 8
  Sample Format: unsigned integer
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Extra Samples: 1<unassoc-alpha>
  Samples/Pixel: 4
  Rows/Strip: 1
  Planar Configuration: single image plane
  Tag 33550: 0.009331,0.009331,0.000000
  Tag 33922: 0.000000,0.000000,0.000000,-13646305.785530,5497215.247386,0.000000
  Tag 34735: 1,1,0,7,1024,0,1,1,1025,0,1,1,1026,34737,25,0,2049,34737,7,25,2054,0,1,9102,3072,0,1,3857,3076,0,1,9001
  Tag 34737: WGS 84 / Pseudo-Mercator|WGS 84|
  Tag 42112: <GDALMetadata>
  <Item name="acquisitionEndDate">2021-10-08T20:43:00+00:00</Item>
  <Item name="acquisitionStartDate">2021-10-08T20:13:00+00:00</Item>
  <Item name="isCalibrated">False</Item>
</GDALMetadata>

You can see the resolution (I suppose?) in the unofficial tag 33550. libvips currently has no way of reading these extra, undocumented tags, so you're out of luck.

Are you cropping many small tiles to feed to an ML system? You'll probably find fetch is quicker than crop. Have a look at:

https://github.com/libvips/pyvips/issues/100#issuecomment-493960943

9
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 18, 2021 at 17:53
  • Interesting. I looked through the meta data using PIL and found the resolution. See recent edits.
    – Binx
    Nov 18, 2021 at 18:04
  • Yes I am cropping many small tiles to feed to an ML system. Thanks, I'll take a look at fetch!
    – Binx
    Nov 18, 2021 at 18:06
  • 1
    I updated again.
    – jcupitt
    Nov 19, 2021 at 8:41
  • 1
    Sure, if someone volunteers to add GDAL metadata support.
    – jcupitt
    Nov 20, 2021 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.