@@ -161,38 +161,6 @@ def APP(self, marker):
161
161
# plus constant header size
162
162
self .info ["mpoffset" ] = self .fp .tell () - n + 4
163
163
164
- # If DPI isn't in JPEG header, fetch from EXIF
165
- if "dpi" not in self .info and "exif" in self .info :
166
- try :
167
- exif = self .getexif ()
168
- resolution_unit = exif [0x0128 ]
169
- x_resolution = exif [0x011A ]
170
- try :
171
- dpi = float (x_resolution [0 ]) / x_resolution [1 ]
172
- except TypeError :
173
- dpi = x_resolution
174
- if math .isnan (dpi ):
175
- msg = "DPI is not a number"
176
- raise ValueError (msg )
177
- if resolution_unit == 3 : # cm
178
- # 1 dpcm = 2.54 dpi
179
- dpi *= 2.54
180
- self .info ["dpi" ] = dpi , dpi
181
- except (
182
- struct .error ,
183
- KeyError ,
184
- SyntaxError ,
185
- TypeError ,
186
- ValueError ,
187
- ZeroDivisionError ,
188
- ):
189
- # struct.error for truncated EXIF
190
- # KeyError for dpi not included
191
- # SyntaxError for invalid/unreadable EXIF
192
- # ValueError or TypeError for dpi being an invalid float
193
- # ZeroDivisionError for invalid dpi rational value
194
- self .info ["dpi" ] = 72 , 72
195
-
196
164
197
165
def COM (self : JpegImageFile , marker : int ) -> None :
198
166
#
@@ -411,6 +379,8 @@ def _open(self):
411
379
msg = "no marker found"
412
380
raise SyntaxError (msg )
413
381
382
+ self ._read_dpi_from_exif ()
383
+
414
384
def load_read (self , read_bytes : int ) -> bytes :
415
385
"""
416
386
internal: read more image data
@@ -499,6 +469,35 @@ def load_djpeg(self) -> None:
499
469
def _getexif (self ) -> dict [str , Any ] | None :
500
470
return _getexif (self )
501
471
472
+ def _read_dpi_from_exif (self ) -> None :
473
+ # If DPI isn't in JPEG header, fetch from EXIF
474
+ if "dpi" in self .info or "exif" not in self .info :
475
+ return
476
+ try :
477
+ exif = self .getexif ()
478
+ resolution_unit = exif [0x0128 ]
479
+ x_resolution = exif [0x011A ]
480
+ try :
481
+ dpi = float (x_resolution [0 ]) / x_resolution [1 ]
482
+ except TypeError :
483
+ dpi = x_resolution
484
+ if math .isnan (dpi ):
485
+ msg = "DPI is not a number"
486
+ raise ValueError (msg )
487
+ if resolution_unit == 3 : # cm
488
+ # 1 dpcm = 2.54 dpi
489
+ dpi *= 2.54
490
+ self .info ["dpi" ] = dpi , dpi
491
+ except (
492
+ struct .error , # truncated EXIF
493
+ KeyError , # dpi not included
494
+ SyntaxError , # invalid/unreadable EXIF
495
+ TypeError , # dpi is an invalid float
496
+ ValueError , # dpi is an invalid float
497
+ ZeroDivisionError , # invalid dpi rational value
498
+ ):
499
+ self .info ["dpi" ] = 72 , 72
500
+
502
501
def _getmp (self ):
503
502
return _getmp (self )
504
503
0 commit comments