Blackened everything
This commit is contained in:
parent
e213647217
commit
5966ce29be
57 changed files with 1938 additions and 1414 deletions
|
|
@ -31,16 +31,20 @@ def dump(exif_dict_original):
|
|||
else:
|
||||
zeroth_ifd = {}
|
||||
|
||||
if (("Exif" in exif_dict) and len(exif_dict["Exif"]) or
|
||||
("Interop" in exif_dict) and len(exif_dict["Interop"]) ):
|
||||
if (
|
||||
("Exif" in exif_dict)
|
||||
and len(exif_dict["Exif"])
|
||||
or ("Interop" in exif_dict)
|
||||
and len(exif_dict["Interop"])
|
||||
):
|
||||
zeroth_ifd[ImageIFD.ExifTag] = 1
|
||||
exif_is = True
|
||||
exif_ifd = exif_dict["Exif"]
|
||||
if ("Interop" in exif_dict) and len(exif_dict["Interop"]):
|
||||
exif_ifd[ExifIFD. InteroperabilityTag] = 1
|
||||
exif_ifd[ExifIFD.InteroperabilityTag] = 1
|
||||
interop_is = True
|
||||
interop_ifd = exif_dict["Interop"]
|
||||
elif ExifIFD. InteroperabilityTag in exif_ifd:
|
||||
elif ExifIFD.InteroperabilityTag in exif_ifd:
|
||||
exif_ifd.pop(ExifIFD.InteroperabilityTag)
|
||||
elif ImageIFD.ExifTag in zeroth_ifd:
|
||||
zeroth_ifd.pop(ImageIFD.ExifTag)
|
||||
|
|
@ -52,17 +56,20 @@ def dump(exif_dict_original):
|
|||
elif ImageIFD.GPSTag in zeroth_ifd:
|
||||
zeroth_ifd.pop(ImageIFD.GPSTag)
|
||||
|
||||
if (("1st" in exif_dict) and
|
||||
("thumbnail" in exif_dict) and
|
||||
(exif_dict["thumbnail"] is not None)):
|
||||
if (
|
||||
("1st" in exif_dict)
|
||||
and ("thumbnail" in exif_dict)
|
||||
and (exif_dict["thumbnail"] is not None)
|
||||
):
|
||||
first_is = True
|
||||
exif_dict["1st"][ImageIFD.JPEGInterchangeFormat] = 1
|
||||
exif_dict["1st"][ImageIFD.JPEGInterchangeFormatLength] = 1
|
||||
first_ifd = exif_dict["1st"]
|
||||
|
||||
zeroth_set = _dict_to_bytes(zeroth_ifd, "0th", 0)
|
||||
zeroth_length = (len(zeroth_set[0]) + exif_is * 12 + gps_is * 12 + 4 +
|
||||
len(zeroth_set[1]))
|
||||
zeroth_length = (
|
||||
len(zeroth_set[0]) + exif_is * 12 + gps_is * 12 + 4 + len(zeroth_set[1])
|
||||
)
|
||||
|
||||
if exif_is:
|
||||
exif_set = _dict_to_bytes(exif_ifd, "Exif", zeroth_length)
|
||||
|
|
@ -115,8 +122,7 @@ def dump(exif_dict_original):
|
|||
else:
|
||||
gps_pointer = b""
|
||||
if interop_is:
|
||||
pointer_value = (TIFF_HEADER_LENGTH +
|
||||
zeroth_length + exif_length + gps_length)
|
||||
pointer_value = TIFF_HEADER_LENGTH + zeroth_length + exif_length + gps_length
|
||||
pointer_str = struct.pack(">I", pointer_value)
|
||||
key = ExifIFD.InteroperabilityTag
|
||||
key_str = struct.pack(">H", key)
|
||||
|
|
@ -126,33 +132,46 @@ def dump(exif_dict_original):
|
|||
else:
|
||||
interop_pointer = b""
|
||||
if first_is:
|
||||
pointer_value = (TIFF_HEADER_LENGTH + zeroth_length +
|
||||
exif_length + gps_length + interop_length)
|
||||
pointer_value = (
|
||||
TIFF_HEADER_LENGTH
|
||||
+ zeroth_length
|
||||
+ exif_length
|
||||
+ gps_length
|
||||
+ interop_length
|
||||
)
|
||||
first_ifd_pointer = struct.pack(">L", pointer_value)
|
||||
thumbnail_pointer = (pointer_value + len(first_set[0]) + 24 +
|
||||
4 + len(first_set[1]))
|
||||
thumbnail_p_bytes = (b"\x02\x01\x00\x04\x00\x00\x00\x01" +
|
||||
struct.pack(">L", thumbnail_pointer))
|
||||
thumbnail_length_bytes = (b"\x02\x02\x00\x04\x00\x00\x00\x01" +
|
||||
struct.pack(">L", len(thumbnail)))
|
||||
first_bytes = (first_set[0] + thumbnail_p_bytes +
|
||||
thumbnail_length_bytes + b"\x00\x00\x00\x00" +
|
||||
first_set[1] + thumbnail)
|
||||
thumbnail_pointer = (
|
||||
pointer_value + len(first_set[0]) + 24 + 4 + len(first_set[1])
|
||||
)
|
||||
thumbnail_p_bytes = b"\x02\x01\x00\x04\x00\x00\x00\x01" + struct.pack(
|
||||
">L", thumbnail_pointer
|
||||
)
|
||||
thumbnail_length_bytes = b"\x02\x02\x00\x04\x00\x00\x00\x01" + struct.pack(
|
||||
">L", len(thumbnail)
|
||||
)
|
||||
first_bytes = (
|
||||
first_set[0]
|
||||
+ thumbnail_p_bytes
|
||||
+ thumbnail_length_bytes
|
||||
+ b"\x00\x00\x00\x00"
|
||||
+ first_set[1]
|
||||
+ thumbnail
|
||||
)
|
||||
else:
|
||||
first_ifd_pointer = b"\x00\x00\x00\x00"
|
||||
|
||||
zeroth_bytes = (zeroth_set[0] + exif_pointer + gps_pointer +
|
||||
first_ifd_pointer + zeroth_set[1])
|
||||
zeroth_bytes = (
|
||||
zeroth_set[0] + exif_pointer + gps_pointer + first_ifd_pointer + zeroth_set[1]
|
||||
)
|
||||
if exif_is:
|
||||
exif_bytes = exif_set[0] + interop_pointer + exif_set[1]
|
||||
|
||||
return (header + zeroth_bytes + exif_bytes + gps_bytes +
|
||||
interop_bytes + first_bytes)
|
||||
return header + zeroth_bytes + exif_bytes + gps_bytes + interop_bytes + first_bytes
|
||||
|
||||
|
||||
def _get_thumbnail(jpeg):
|
||||
segments = split_into_segments(jpeg)
|
||||
while (b"\xff\xe0" <= segments[1][0:2] <= b"\xff\xef"):
|
||||
while b"\xff\xe0" <= segments[1][0:2] <= b"\xff\xef":
|
||||
segments.pop(1)
|
||||
thumbnail = b"".join(segments)
|
||||
return thumbnail
|
||||
|
|
@ -161,24 +180,31 @@ def _get_thumbnail(jpeg):
|
|||
def _pack_byte(*args):
|
||||
return struct.pack("B" * len(args), *args)
|
||||
|
||||
|
||||
def _pack_signed_byte(*args):
|
||||
return struct.pack("b" * len(args), *args)
|
||||
|
||||
|
||||
def _pack_short(*args):
|
||||
return struct.pack(">" + "H" * len(args), *args)
|
||||
|
||||
|
||||
def _pack_signed_short(*args):
|
||||
return struct.pack(">" + "h" * len(args), *args)
|
||||
|
||||
|
||||
def _pack_long(*args):
|
||||
return struct.pack(">" + "L" * len(args), *args)
|
||||
|
||||
|
||||
def _pack_slong(*args):
|
||||
return struct.pack(">" + "l" * len(args), *args)
|
||||
|
||||
|
||||
def _pack_float(*args):
|
||||
return struct.pack(">" + "f" * len(args), *args)
|
||||
|
||||
|
||||
def _pack_double(*args):
|
||||
return struct.pack(">" + "d" * len(args), *args)
|
||||
|
||||
|
|
@ -190,16 +216,14 @@ def _value_to_bytes(raw_value, value_type, offset):
|
|||
if value_type == TYPES.Byte:
|
||||
length = len(raw_value)
|
||||
if length <= 4:
|
||||
value_str = (_pack_byte(*raw_value) +
|
||||
b"\x00" * (4 - length))
|
||||
value_str = _pack_byte(*raw_value) + b"\x00" * (4 - length)
|
||||
else:
|
||||
value_str = struct.pack(">I", offset)
|
||||
four_bytes_over = _pack_byte(*raw_value)
|
||||
elif value_type == TYPES.Short:
|
||||
length = len(raw_value)
|
||||
if length <= 2:
|
||||
value_str = (_pack_short(*raw_value) +
|
||||
b"\x00\x00" * (2 - length))
|
||||
value_str = _pack_short(*raw_value) + b"\x00\x00" * (2 - length)
|
||||
else:
|
||||
value_str = struct.pack(">I", offset)
|
||||
four_bytes_over = _pack_short(*raw_value)
|
||||
|
|
@ -241,8 +265,7 @@ def _value_to_bytes(raw_value, value_type, offset):
|
|||
new_value = b""
|
||||
for n, val in enumerate(raw_value):
|
||||
num, den = val
|
||||
new_value += (struct.pack(">L", num) +
|
||||
struct.pack(">L", den))
|
||||
new_value += struct.pack(">L", num) + struct.pack(">L", den)
|
||||
value_str = struct.pack(">I", offset)
|
||||
four_bytes_over = new_value
|
||||
elif value_type == TYPES.SRational:
|
||||
|
|
@ -255,8 +278,7 @@ def _value_to_bytes(raw_value, value_type, offset):
|
|||
new_value = b""
|
||||
for n, val in enumerate(raw_value):
|
||||
num, den = val
|
||||
new_value += (struct.pack(">l", num) +
|
||||
struct.pack(">l", den))
|
||||
new_value += struct.pack(">l", num) + struct.pack(">l", den)
|
||||
value_str = struct.pack(">I", offset)
|
||||
four_bytes_over = new_value
|
||||
elif value_type == TYPES.Undefined:
|
||||
|
|
@ -272,19 +294,17 @@ def _value_to_bytes(raw_value, value_type, offset):
|
|||
value_str = raw_value + b"\x00" * (4 - length)
|
||||
except TypeError:
|
||||
raise ValueError("Got invalid type to convert.")
|
||||
elif value_type == TYPES.SByte: # Signed Byte
|
||||
elif value_type == TYPES.SByte: # Signed Byte
|
||||
length = len(raw_value)
|
||||
if length <= 4:
|
||||
value_str = (_pack_signed_byte(*raw_value) +
|
||||
b"\x00" * (4 - length))
|
||||
value_str = _pack_signed_byte(*raw_value) + b"\x00" * (4 - length)
|
||||
else:
|
||||
value_str = struct.pack(">I", offset)
|
||||
four_bytes_over = _pack_signed_byte(*raw_value)
|
||||
elif value_type == TYPES.SShort: # Signed Short
|
||||
elif value_type == TYPES.SShort: # Signed Short
|
||||
length = len(raw_value)
|
||||
if length <= 2:
|
||||
value_str = (_pack_signed_short(*raw_value) +
|
||||
b"\x00\x00" * (2 - length))
|
||||
value_str = _pack_signed_short(*raw_value) + b"\x00\x00" * (2 - length)
|
||||
else:
|
||||
value_str = struct.pack(">I", offset)
|
||||
four_bytes_over = _pack_signed_short(*raw_value)
|
||||
|
|
@ -295,7 +315,7 @@ def _value_to_bytes(raw_value, value_type, offset):
|
|||
else:
|
||||
value_str = struct.pack(">I", offset)
|
||||
four_bytes_over = _pack_float(*raw_value)
|
||||
elif value_type == TYPES.DFloat: # Double
|
||||
elif value_type == TYPES.DFloat: # Double
|
||||
length = len(raw_value)
|
||||
value_str = struct.pack(">I", offset)
|
||||
four_bytes_over = _pack_double(*raw_value)
|
||||
|
|
@ -303,6 +323,7 @@ def _value_to_bytes(raw_value, value_type, offset):
|
|||
length_str = struct.pack(">I", length)
|
||||
return length_str, value_str, four_bytes_over
|
||||
|
||||
|
||||
def _dict_to_bytes(ifd_dict, ifd, ifd_offset):
|
||||
tag_count = len(ifd_dict)
|
||||
entry_header = struct.pack(">H", tag_count)
|
||||
|
|
@ -318,7 +339,10 @@ def _dict_to_bytes(ifd_dict, ifd, ifd_offset):
|
|||
continue
|
||||
elif (ifd == "Exif") and (key == ExifIFD.InteroperabilityTag):
|
||||
continue
|
||||
elif (ifd == "1st") and (key in (ImageIFD.JPEGInterchangeFormat, ImageIFD.JPEGInterchangeFormatLength)):
|
||||
elif (ifd == "1st") and (
|
||||
key
|
||||
in (ImageIFD.JPEGInterchangeFormat, ImageIFD.JPEGInterchangeFormatLength)
|
||||
):
|
||||
continue
|
||||
|
||||
raw_value = ifd_dict[key]
|
||||
|
|
@ -332,13 +356,13 @@ def _dict_to_bytes(ifd_dict, ifd, ifd_offset):
|
|||
offset = TIFF_HEADER_LENGTH + entries_length + ifd_offset + len(values)
|
||||
|
||||
try:
|
||||
length_str, value_str, four_bytes_over = _value_to_bytes(raw_value,
|
||||
value_type,
|
||||
offset)
|
||||
length_str, value_str, four_bytes_over = _value_to_bytes(
|
||||
raw_value, value_type, offset
|
||||
)
|
||||
except ValueError:
|
||||
raise ValueError(
|
||||
'"dump" got wrong type of exif value.\n' +
|
||||
'{0} in {1} IFD. Got as {2}.'.format(key, ifd, type(ifd_dict[key]))
|
||||
'"dump" got wrong type of exif value.\n'
|
||||
+ "{0} in {1} IFD. Got as {2}.".format(key, ifd, type(ifd_dict[key]))
|
||||
)
|
||||
|
||||
entries += key_str + type_str + length_str + value_str
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue