supporting ICCSBased colors
This commit is contained in:
1 parent
b491cd2181
commit
cf3c89bf25
4 files changed
+64
-36
No files matched your search
@@ -33,7 +33,7 @@ def get_color(col: PDFGraphicStateColor):
|
||||
return f'gray({color_val})'
|
||||
elif color_type == "custom":
|
||||
# Todo: convert these to more friendly color types
|
||||
return f"custom({col.custom['type']})"
|
||||
return f"{col.custom['type']}({col.custom['defintion']})"
|
||||
return ""
|
||||
|
||||
FIRST_CAP_RE = re.compile('(.)([A-Z][a-z]+)')
|
||||
|
||||
@@ -351,6 +351,24 @@ class PDFPageInterpreter(object):
|
||||
#self.do_cs(LITERAL_DEVICE_CMYK)
|
||||
return
|
||||
|
||||
def __process_ICCBased(self, n: int, components: list):
|
||||
if n == 1:
|
||||
return {
|
||||
"type": "gray",
|
||||
"definition": components
|
||||
}
|
||||
elif n == 3:
|
||||
return {
|
||||
"type": "rgb",
|
||||
"definition": components
|
||||
}
|
||||
elif n == 4:
|
||||
return {
|
||||
"type": "cmyk",
|
||||
"definition": components
|
||||
}
|
||||
raise PDFInterpreterError("Unsupported ICCBased color")
|
||||
|
||||
# setcolor
|
||||
def __process_scn(self):
|
||||
if self.ncs:
|
||||
@@ -359,8 +377,14 @@ class PDFPageInterpreter(object):
|
||||
raise PDFInterpreterError('No colorspace specified!')
|
||||
components = self.pop(n)
|
||||
obj_id = components[0]
|
||||
if self.ncs.name == "ICCBased":
|
||||
return self.__process_ICCBased(n, components)
|
||||
if self.ncs.name not in self.resources.keys():
|
||||
raise PDFInterpreterError("Color space not in resources")
|
||||
log.warning("Color space not in resources")
|
||||
return {
|
||||
"type": self.ncs.name,
|
||||
"defintion": f"{n}|({','.join(map(str, components))})"
|
||||
}
|
||||
if obj_id.name not in self.resources[self.ncs.name].keys():
|
||||
raise PDFInterpreterError("Invalid Color reference")
|
||||
|
||||
@@ -374,11 +398,27 @@ class PDFPageInterpreter(object):
|
||||
"defintion": color_data
|
||||
}
|
||||
def do_SCN(self):
|
||||
self.graphicstate.scolor.custom = self.__process_scn()
|
||||
processed_col = self.__process_scn()
|
||||
if processed_col['type'] == 'rgb':
|
||||
self.graphicstate.scolor.rgb = processed_col['definition']
|
||||
elif processed_col['type'] == 'gray':
|
||||
self.graphicstate.scolor.gray = processed_col['definition'][0]
|
||||
elif processed_col['type'] == 'cmyk':
|
||||
self.graphicstate.scolor.cmyk = processed_col['definition']
|
||||
else:
|
||||
self.graphicstate.scolor.custom = processed_col
|
||||
return
|
||||
|
||||
def do_scn(self):
|
||||
self.graphicstate.ncolor.custom = self.__process_scn()
|
||||
processed_col = self.__process_scn()
|
||||
if processed_col['type'] == 'rgb':
|
||||
self.graphicstate.ncolor.rgb = processed_col['definition']
|
||||
elif processed_col['type'] == 'gray':
|
||||
self.graphicstate.ncolor.gray = processed_col['definition'][0]
|
||||
elif processed_col['type'] == 'cmyk':
|
||||
self.graphicstate.ncolor.cmyk = processed_col['definition']
|
||||
else:
|
||||
self.graphicstate.ncolor.custom = processed_col
|
||||
return
|
||||
|
||||
def do_SC(self):
|
||||
|
||||
+19
-31
@@ -8,7 +8,7 @@ from xml.etree import ElementTree
|
||||
def rel_path(*arg: List[str]) -> str:
|
||||
return os.path.join(os.path.dirname(__file__), *arg)
|
||||
|
||||
EXT = 'xml'
|
||||
EXT = 'html'
|
||||
INPUT_FOLDER = rel_path("./pdf")
|
||||
OUTPUT_FOLDER = rel_path(f"./output/{EXT}")
|
||||
|
||||
@@ -17,34 +17,22 @@ if not os.path.exists(OUTPUT_FOLDER):
|
||||
|
||||
files = os.listdir(INPUT_FOLDER)
|
||||
|
||||
input_file_path = '/media/ari/sd/projects/School/Schulich/acp/pdf-extraction/data/input/Supplemental_User_Agreement_Settlement_Custodian.pdf'
|
||||
output_file_path = os.path.join(OUTPUT_FOLDER, 'demo.xml')
|
||||
TOTAL = len(files)
|
||||
for idx, file_name in enumerate(files):
|
||||
file_name_no_ext = re.sub(r'\.pdf$',"", file_name)
|
||||
input_file_path = os.path.join(INPUT_FOLDER, file_name)
|
||||
output_file_path = os.path.join(OUTPUT_FOLDER, file_name_no_ext + "." + EXT)
|
||||
|
||||
with open(input_file_path, 'rb') as input_file:
|
||||
with open(output_file_path, 'wb') as output_file:
|
||||
convert_file(
|
||||
input_file=input_file,
|
||||
output_file=output_file,
|
||||
out_type=EXT,
|
||||
debug=True,
|
||||
)
|
||||
|
||||
# TOTAL = len(files)
|
||||
# for idx, file_name in enumerate(files):
|
||||
# file_name_no_ext = re.sub(r'\.pdf$',"", file_name)
|
||||
# input_file_path = os.path.join(INPUT_FOLDER, file_name)
|
||||
# output_file_path = os.path.join(OUTPUT_FOLDER, file_name_no_ext + "." + EXT)
|
||||
|
||||
# if len(file_name) > len(file_name_no_ext):
|
||||
# print(f"processing {file_name} {idx}/{TOTAL}")
|
||||
# with open(input_file_path, 'rb') as input_file:
|
||||
# with open(output_file_path, 'wb') as output_file:
|
||||
# convert_file(
|
||||
# input_file=input_file,
|
||||
# output_file=output_file,
|
||||
# out_type=EXT,
|
||||
# debug=True,
|
||||
# )
|
||||
# print(f" > testing format {file_name} {idx}/{TOTAL}")
|
||||
# ElementTree.parse(output_file_path)
|
||||
# print(f"processing {idx+1}/{TOTAL}")
|
||||
if len(file_name) > len(file_name_no_ext):
|
||||
print(f"processing {file_name} {idx}/{TOTAL}")
|
||||
with open(input_file_path, 'rb') as input_file:
|
||||
with open(output_file_path, 'wb') as output_file:
|
||||
convert_file(
|
||||
input_file=input_file,
|
||||
output_file=output_file,
|
||||
out_type=EXT,
|
||||
debug=True,
|
||||
)
|
||||
print(f" > testing format {file_name} {idx}/{TOTAL}")
|
||||
ElementTree.parse(output_file_path)
|
||||
print(f"processing {idx+1}/{TOTAL}")
|
||||
Reference in new issue
Block a user