Skip to content

Commit 3a141b0

Browse files
committed
uses PIL to calculate image size
1 parent 9caffec commit 3a141b0

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

icevision/imports.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys, os, re, shutil, typing, itertools, operator, math, warnings, json, random
22
import functools, io, cv2, mimetypes, torch, torchvision, dataclasses, zipfile, pickle
3+
import PIL
34
from copy import copy, deepcopy
45

56
from pdb import set_trace

icevision/utils/imageio.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88
]
99

1010
from icevision.imports import *
11-
import PIL, imagesize
11+
from PIL import ExifTags
1212

1313
ImgSize = namedtuple("ImgSize", "width,height")
1414

15+
# get exif tag
16+
for _EXIF_ORIENTATION_TAG in ExifTags.TAGS.keys():
17+
if PIL.ExifTags.TAGS[_EXIF_ORIENTATION_TAG] == "Orientation":
18+
break
19+
1520

1621
def open_img(fn, gray=False):
1722
color = "L" if gray else "RGB"
@@ -27,14 +32,26 @@ def get_image_size(filepath: Union[str, Path]) -> Tuple[int, int]:
2732
Returns image (width, height)
2833
"""
2934
logger.warning("get_image_size is deprecated, use get_img_size instead")
30-
return imagesize.get(filepath)
35+
image_size = get_img_size(filepath=filepath)
36+
return image_size.width, image_size.height
3137

3238

3339
def get_img_size(filepath: Union[str, Path]) -> ImgSize:
3440
"""
3541
Returns image (width, height)
3642
"""
37-
return ImgSize(*imagesize.get(filepath))
43+
with PIL.Image.open(filepath) as image:
44+
image_size = image.size
45+
46+
try:
47+
exif = image._getexif()
48+
if exif is not None and exif[_EXIF_ORIENTATION_TAG] in [6, 8]:
49+
image_size = image_size[::-1]
50+
except (AttributeError, KeyError, IndexError):
51+
# cases: image don't have getexif
52+
pass
53+
54+
return ImgSize(*image_size)
3855

3956

4057
def show_img(img, ax=None, show: bool = False, **kwargs):

setup.cfg

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ install_requires =
3535
pycocotools>=2.0.2<3
3636
requests >=2.24.0<3
3737
loguru >=0.5.3
38-
imagesize >= 1.2.0
3938
pillow > 8.0.0
4039
importlib-metadata>=1;python_version<"3.8"
4140

tests/utils/test_imageio.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"fn,expected",
77
[
88
("voc/JPEGImages/2007_000063.jpg", (375, 500, 3)),
9-
("images2/flies.jpeg", (2592, 3888, 3)),
9+
("images2/flies.jpeg", (3888, 2592, 3)),
1010
],
1111
)
1212
def test_open_img(samples_source, fn, expected):
@@ -18,7 +18,7 @@ def test_open_img(samples_source, fn, expected):
1818
"fn,expected",
1919
[
2020
("voc/JPEGImages/2007_000063.jpg", (500, 375)),
21-
("images2/flies.jpeg", (3888, 2592)),
21+
("images2/flies.jpeg", (2592, 3888)),
2222
],
2323
)
2424
def test_get_image_size(samples_source, fn, expected):

0 commit comments

Comments
 (0)