-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmp_editing_utils.py
179 lines (162 loc) · 4.38 KB
/
xmp_editing_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import pathlib
import cv2
import numpy as np
import pyexiv2
from exiftool import ExifTool
from logzero import logger
from PIL import Image
from PIL import ImageDraw
from PIL import ImageStat
empty_xml = """<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 5.5.0">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:photoshop="http://ns.adobe.com/photoshop/1.0/"
xmlns:xmp="http://ns.adobe.com/xap/1.0/">
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>"""
def copy_xmp_temp(
from_file: pathlib.PosixPath,
to_file: pathlib.PosixPath,
et: ExifTool,
warn: bool = False,
):
# exiftool implementation. Does not contain much error handling
# will not raise an error if the file does not exist
if not from_file.is_file():
if warn:
logger.warning(f"File {from_file} not found")
else:
logger.debug(f"File {from_file} not found")
return
# run exiftool command on file to return xmp string
file_raw_xmp = et.execute(
*["-xmp", "-b", str(from_file.as_posix()), "-api", "LargeFileSupport=1"]
)
# strip null characters common in some languages
file_raw_xmp = file_raw_xmp.strip("\x00")
if file_raw_xmp == "":
# raise ValueError(f"Empty XMP retrieved for {from_file}")
logger.info(f"Empty XMP retrieved for {from_file}. Using empty XML string")
# logger.debug(f"Problem working on {from_file}: {e}")
file_raw_xmp = empty_xml
# write data to temp
with open(to_file, "w") as f:
f.write(file_raw_xmp)
# confirm the raw xmp can be opened - this requires the log level is not at 4 (muted)
with pyexiv2.Image(to_file.as_posix()) as img:
img.read_xmp()
def convert_to_binary(image, lower_threshold, upper_threshold):
"""
This function converts a image to a binary image
:param image: OpenCV image array
:return: Binary image as OpenCV image array
"""
original_imgcv2 = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
grayImage_imgcv2 = cv2.cvtColor(original_imgcv2, cv2.COLOR_BGR2GRAY)
(thresh, blackAndWhiteImage) = cv2.threshold(
grayImage_imgcv2, lower_threshold, upper_threshold, cv2.THRESH_BINARY
)
# cv2.imwrite("binary.jpg", blackAndWhiteImage)
image = Image.fromarray(
cv2.cvtColor(blackAndWhiteImage, cv2.COLOR_BGR2RGB)
) # convert from cv2 image file to pil image file
return image
def draw_cropline(im, bbox):
"""
This function draws the bounding box in the picture
:param im: PIL image
:param bbox: PIL bbox array
:return: PIL image with drawn bbox
"""
draw = ImageDraw.Draw(im)
draw.rectangle(((bbox[0], bbox[1]), (bbox[2], bbox[3])), outline="red", width=5)
return im
def get_control_value(im, bbox):
"""
This function gets the mean color value outside of the bounding box (bbox).
The closer to 0 the darker is the section that will be removed. If the value is
higher than a certain threshold this could indicate an wrong autocrop boundingbox
and non black pixels are cropped.
:param im: PIL Image
:param bbox: PIL bbox array
:return: mean color value (float)
"""
control_img = im
mask_layer = Image.new("L", control_img.size, 255)
draw = ImageDraw.Draw(mask_layer)
draw.rectangle(((bbox[0], bbox[1]), (bbox[2], bbox[3])), fill=0)
avg_list = ImageStat.Stat(control_img, mask=mask_layer).mean
avg = round(sum(avg_list) / 3, 2)
return avg
raw_files = [".CRW", ".CR2", ".CR3", ".DNG", ".RAW"]
other_files = [
# -- Regular formats
".3FR",
".ARI",
".ARW",
".BAY",
".BMQ",
".CAP",
".CINE",
# ".CR2",
# ".CR3",
# ".CRW",
".CS1",
".DC2",
".DCR",
# ".DNG",
".GPR",
".ERF",
".FFF",
".EXR",
".IA",
".IIQ",
".JPEG",
".JPG",
".K25",
".KC2",
".KDC",
".MDC",
".MEF",
".MOS",
".MRW",
".NEF",
".NRW",
".ORF",
".PEF",
".PFM",
".PNG",
".PXN",
".QTK",
".RAF",
# ".RAW",
".RDC",
".RW1",
".RW2",
".SR2",
".SRF",
".SRW",
".STI",
".TIF",
".TIFF",
".X3F",
# -- Extended Formats
".J2C",
".J2K",
".JP2",
".JPC",
".BMP",
".DCM",
".GIF",
".JNG",
".JPC",
".JP2",
".MIFF",
".MNG",
".PBM",
".PGM",
".PNM",
".PPM",
".WEBP",
]