Skip to content

Commit 13dcab0

Browse files
committed
Replaced DEBUG with logging
1 parent 97280a0 commit 13dcab0

1 file changed

Lines changed: 54 additions & 79 deletions

File tree

src/PIL/TiffImagePlugin.py

Lines changed: 54 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#
4141
import io
4242
import itertools
43+
import logging
4344
import os
4445
import struct
4546
import warnings
@@ -51,7 +52,7 @@
5152
from ._binary import i8, o8
5253
from .TiffTags import TYPES
5354

54-
DEBUG = False # Needs to be merged with the new logging approach.
55+
logger = logging.getLogger(__name__)
5556

5657
# Set these to true to force use of libtiff for reading or writing.
5758
READ_LIBTIFF = False
@@ -734,29 +735,21 @@ def load(self, fp):
734735
try:
735736
for i in range(self._unpack("H", self._ensure_read(fp, 2))[0]):
736737
tag, typ, count, data = self._unpack("HHL4s", self._ensure_read(fp, 12))
737-
if DEBUG:
738-
tagname = TiffTags.lookup(tag).name
739-
typname = TYPES.get(typ, "unknown")
740-
print(
741-
"tag: %s (%d) - type: %s (%d)" % (tagname, tag, typname, typ),
742-
end=" ",
743-
)
738+
739+
tagname = TiffTags.lookup(tag).name
740+
typname = TYPES.get(typ, "unknown")
741+
msg = "tag: %s (%d) - type: %s (%d)" % (tagname, tag, typname, typ)
744742

745743
try:
746744
unit_size, handler = self._load_dispatch[typ]
747745
except KeyError:
748-
if DEBUG:
749-
print("- unsupported type", typ)
746+
logger.debug(msg + " - unsupported type {}".format(typ))
750747
continue # ignore unsupported type
751748
size = count * unit_size
752749
if size > 4:
753750
here = fp.tell()
754751
(offset,) = self._unpack("L", data)
755-
if DEBUG:
756-
print(
757-
"Tag Location: {} - Data Location: {}".format(here, offset),
758-
end=" ",
759-
)
752+
msg += " Tag Location: {} - Data Location: {}".format(here, offset)
760753
fp.seek(offset)
761754
data = ImageFile._safe_read(fp, size)
762755
fp.seek(here)
@@ -769,19 +762,20 @@ def load(self, fp):
769762
"Expecting to read %d bytes but only got %d."
770763
" Skipping tag %s" % (size, len(data), tag)
771764
)
765+
logger.debug(msg)
772766
continue
773767

774768
if not data:
769+
logger.debug(msg)
775770
continue
776771

777772
self._tagdata[tag] = data
778773
self.tagtype[tag] = typ
779774

780-
if DEBUG:
781-
if size > 32:
782-
print("- value: <table: %d bytes>" % size)
783-
else:
784-
print("- value:", self[tag])
775+
msg += " - value: " + (
776+
"<table: %d bytes>" % size if size > 32 else str(data)
777+
)
778+
logger.debug(msg)
785779

786780
(self.next,) = self._unpack("L", self._ensure_read(fp, 4))
787781
except OSError as msg:
@@ -802,21 +796,17 @@ def tobytes(self, offset=0):
802796
if tag == STRIPOFFSETS:
803797
stripoffsets = len(entries)
804798
typ = self.tagtype.get(tag)
805-
if DEBUG:
806-
print("Tag {}, Type: {}, Value: {}".format(tag, typ, value))
799+
logger.debug("Tag {}, Type: {}, Value: {}".format(tag, typ, value))
807800
values = value if isinstance(value, tuple) else (value,)
808801
data = self._write_dispatch[typ](self, *values)
809-
if DEBUG:
810-
tagname = TiffTags.lookup(tag).name
811-
typname = TYPES.get(typ, "unknown")
812-
print(
813-
"save: %s (%d) - type: %s (%d)" % (tagname, tag, typname, typ),
814-
end=" ",
815-
)
816-
if len(data) >= 16:
817-
print("- value: <table: %d bytes>" % len(data))
818-
else:
819-
print("- value:", values)
802+
803+
tagname = TiffTags.lookup(tag).name
804+
typname = TYPES.get(typ, "unknown")
805+
msg = "save: %s (%d) - type: %s (%d)" % (tagname, tag, typname, typ)
806+
msg += " - value: " + (
807+
"<table: %d bytes>" % len(data) if len(data) >= 16 else str(values)
808+
)
809+
logger.debug(msg)
820810

821811
# count is sum of lengths for string and arbitrary data
822812
if typ in [TiffTags.BYTE, TiffTags.ASCII, TiffTags.UNDEFINED]:
@@ -840,8 +830,9 @@ def tobytes(self, offset=0):
840830

841831
# pass 2: write entries to file
842832
for tag, typ, count, value, data in entries:
843-
if DEBUG:
844-
print(tag, typ, count, repr(value), repr(data))
833+
logger.debug(
834+
"{} {} {} {} {}".format(tag, typ, count, repr(value), repr(data))
835+
)
845836
result += self._pack("HHL4s", tag, typ, count, value)
846837

847838
# -- overwrite here for multi-page --
@@ -997,10 +988,9 @@ def _open(self):
997988
self._frame_pos = []
998989
self._n_frames = None
999990

1000-
if DEBUG:
1001-
print("*** TiffImageFile._open ***")
1002-
print("- __first:", self.__first)
1003-
print("- ifh: ", ifh)
991+
logger.debug("*** TiffImageFile._open ***")
992+
logger.debug("- __first: {}".format(self.__first))
993+
logger.debug("- ifh: {}".format(ifh))
1004994

1005995
# and load the first frame
1006996
self._seek(0)
@@ -1035,18 +1025,16 @@ def _seek(self, frame):
10351025
while len(self._frame_pos) <= frame:
10361026
if not self.__next:
10371027
raise EOFError("no more images in TIFF file")
1038-
if DEBUG:
1039-
print(
1040-
"Seeking to frame %s, on frame %s, __next %s, location: %s"
1041-
% (frame, self.__frame, self.__next, self.fp.tell())
1042-
)
1028+
logger.debug(
1029+
"Seeking to frame %s, on frame %s, __next %s, location: %s"
1030+
% (frame, self.__frame, self.__next, self.fp.tell())
1031+
)
10431032
# reset buffered io handle in case fp
10441033
# was passed to libtiff, invalidating the buffer
10451034
self.fp.tell()
10461035
self.fp.seek(self.__next)
10471036
self._frame_pos.append(self.__next)
1048-
if DEBUG:
1049-
print("Loading tags, location: %s" % self.fp.tell())
1037+
logger.debug("Loading tags, location: %s" % self.fp.tell())
10501038
self.tag_v2.load(self.fp)
10511039
self.__next = self.tag_v2.next
10521040
if self.__next == 0:
@@ -1149,21 +1137,18 @@ def _load_libtiff(self):
11491137
# Rearranging for supporting byteio items, since they have a fileno
11501138
# that returns an OSError if there's no underlying fp. Easier to
11511139
# deal with here by reordering.
1152-
if DEBUG:
1153-
print("have getvalue. just sending in a string from getvalue")
1140+
logger.debug("have getvalue. just sending in a string from getvalue")
11541141
n, err = decoder.decode(self.fp.getvalue())
11551142
elif fp:
11561143
# we've got a actual file on disk, pass in the fp.
1157-
if DEBUG:
1158-
print("have fileno, calling fileno version of the decoder.")
1144+
logger.debug("have fileno, calling fileno version of the decoder.")
11591145
if not close_self_fp:
11601146
self.fp.seek(0)
11611147
# 4 bytes, otherwise the trace might error out
11621148
n, err = decoder.decode(b"fpfp")
11631149
else:
11641150
# we have something else.
1165-
if DEBUG:
1166-
print("don't have fileno or getvalue. just reading")
1151+
logger.debug("don't have fileno or getvalue. just reading")
11671152
self.fp.seek(0)
11681153
# UNDONE -- so much for that buffer size thing.
11691154
n, err = decoder.decode(self.fp.read())
@@ -1203,21 +1188,19 @@ def _setup(self):
12031188

12041189
fillorder = self.tag_v2.get(FILLORDER, 1)
12051190

1206-
if DEBUG:
1207-
print("*** Summary ***")
1208-
print("- compression:", self._compression)
1209-
print("- photometric_interpretation:", photo)
1210-
print("- planar_configuration:", self._planar_configuration)
1211-
print("- fill_order:", fillorder)
1212-
print("- YCbCr subsampling:", self.tag.get(530))
1191+
logger.debug("*** Summary ***")
1192+
logger.debug("- compression: {}".format(self._compression))
1193+
logger.debug("- photometric_interpretation: {}".format(photo))
1194+
logger.debug("- planar_configuration: {}".format(self._planar_configuration))
1195+
logger.debug("- fill_order: {}".format(fillorder))
1196+
logger.debug("- YCbCr subsampling: {}".format(self.tag.get(530)))
12131197

12141198
# size
12151199
xsize = int(self.tag_v2.get(IMAGEWIDTH))
12161200
ysize = int(self.tag_v2.get(IMAGELENGTH))
12171201
self._size = xsize, ysize
12181202

1219-
if DEBUG:
1220-
print("- size:", self.size)
1203+
logger.debug("- size: {}".format(self.size))
12211204

12221205
sampleFormat = self.tag_v2.get(SAMPLEFORMAT, (1,))
12231206
if len(sampleFormat) > 1 and max(sampleFormat) == min(sampleFormat) == 1:
@@ -1251,18 +1234,15 @@ def _setup(self):
12511234
bps_tuple,
12521235
extra_tuple,
12531236
)
1254-
if DEBUG:
1255-
print("format key:", key)
1237+
logger.debug("format key: {}".format(key))
12561238
try:
12571239
self.mode, rawmode = OPEN_INFO[key]
12581240
except KeyError:
1259-
if DEBUG:
1260-
print("- unsupported format")
1241+
logger.debug("- unsupported format")
12611242
raise SyntaxError("unknown pixel mode")
12621243

1263-
if DEBUG:
1264-
print("- raw mode:", rawmode)
1265-
print("- pil mode:", self.mode)
1244+
logger.debug("- raw mode: {}".format(rawmode))
1245+
logger.debug("- pil mode: {}".format(self.mode))
12661246

12671247
self.info["compression"] = self._compression
12681248

@@ -1303,8 +1283,7 @@ def _setup(self):
13031283
if fillorder == 2:
13041284
# Replace fillorder with fillorder=1
13051285
key = key[:3] + (1,) + key[4:]
1306-
if DEBUG:
1307-
print("format key:", key)
1286+
logger.debug("format key: {}".format(key))
13081287
# this should always work, since all the
13091288
# fillorder==2 modes have a corresponding
13101289
# fillorder=1 mode
@@ -1366,8 +1345,7 @@ def _setup(self):
13661345
x = y = 0
13671346
layer += 1
13681347
else:
1369-
if DEBUG:
1370-
print("- unsupported data organization")
1348+
logger.debug("- unsupported data organization")
13711349
raise SyntaxError("unknown data organization")
13721350

13731351
# Fix up info.
@@ -1447,8 +1425,7 @@ def _save(im, fp, filename):
14471425

14481426
# write any arbitrary tags passed in as an ImageFileDirectory
14491427
info = im.encoderinfo.get("tiffinfo", {})
1450-
if DEBUG:
1451-
print("Tiffinfo Keys: %s" % list(info))
1428+
logger.debug("Tiffinfo Keys: %s" % list(info))
14521429
if isinstance(info, ImageFileDirectory_v1):
14531430
info = info.to_v2()
14541431
for key in info:
@@ -1533,9 +1510,8 @@ def _save(im, fp, filename):
15331510
)
15341511
ifd[JPEGQUALITY] = quality
15351512

1536-
if DEBUG:
1537-
print("Saving using libtiff encoder")
1538-
print("Items: %s" % sorted(ifd.items()))
1513+
logger.debug("Saving using libtiff encoder")
1514+
logger.debug("Items: %s" % sorted(ifd.items()))
15391515
_fp = 0
15401516
if hasattr(fp, "fileno"):
15411517
try:
@@ -1597,8 +1573,7 @@ def _save(im, fp, filename):
15971573
else:
15981574
atts[tag] = value
15991575

1600-
if DEBUG:
1601-
print("Converted items: %s" % sorted(atts.items()))
1576+
logger.debug("Converted items: %s" % sorted(atts.items()))
16021577

16031578
# libtiff always expects the bytes in native order.
16041579
# we're storing image byte order. So, if the rawmode

0 commit comments

Comments
 (0)