-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_images.py
More file actions
281 lines (237 loc) · 10.2 KB
/
extract_images.py
File metadata and controls
281 lines (237 loc) · 10.2 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
Extract all images from PPTX files in originalMaterials/ and create a catalog.
Outputs:
- images/ppt/<filename>/ — extracted images organized by source file
- images/image_catalog.md — markdown catalog mapping images to book chapters
"""
import os
import sys
from pathlib import Path
from collections import defaultdict
from pptx import Presentation
from pptx.util import Emu
from pptx.enum.shapes import MSO_SHAPE_TYPE
# ── Configuration ──────────────────────────────────────────────────────────
BASE_DIR = Path(r"C:\maidment_hidroGIS")
SRC_DIR = BASE_DIR / "originalMaterials"
OUT_DIR = BASE_DIR / "images" / "ppt"
CATALOG_PATH = BASE_DIR / "images" / "image_catalog.md"
CHAPTER_MAP = {
"Lecture12018.pptx": "Ch 1 – Introduction to GIS in Water Resources",
"MapProj.pptx": "Ch 2 – Geodesy and Map Projections",
"Lecture22018.pptx": "Ch 4 – GIS Software (ArcGIS Pro)",
"DataSources2018.pptx": "Ch 3 – Geospatial Data Sources",
"SpatialAnalysis.pptx": "Ch 5-6 – Raster Analysis and Map Algebra",
"DemWatershedDelineation.pptx": "Ch 9-11 – Terrain Analysis (DEM & Watersheds)",
"ExtendedTerrainAnalysis.pptx": "Ch 9-11 – Terrain Analysis (Extended)",
"HAND.pptx": "Ch 14 – Height Above Nearest Drainage (HAND)",
"NWMFlood.pptx": "Ch 16 – Flood Forecasting (National Water Model)",
"TFRS.pptx": "Ch 16 – Flood Response (Texas Flood Response System)",
"LIDAR.pptx": "Ch 13 – LiDAR Remote Sensing",
"Programming.pptx": "Ch 12 – Python Programming for GIS",
"HydroShare.pptx": "Ch 3 – Data Sharing (HydroShare)",
"Demography2018.pptx": "Ch 17 – Demography and GIS",
}
def emu_to_inches(emu_val):
"""Convert EMU to inches (1 inch = 914400 EMU)."""
if emu_val is None:
return None
return round(emu_val / 914400, 2)
def get_nearby_text(slide, shape):
"""Collect text from shapes near/overlapping the image on the same slide."""
texts = []
for s in slide.shapes:
if s.shape_id == shape.shape_id:
continue
if s.has_text_frame:
txt = s.text_frame.text.strip()
if txt:
texts.append(txt)
return texts
def content_type_to_ext(content_type):
"""Map image content-type to file extension."""
mapping = {
"image/png": ".png",
"image/jpeg": ".jpg",
"image/gif": ".gif",
"image/bmp": ".bmp",
"image/tiff": ".tiff",
"image/x-emf": ".emf",
"image/x-wmf": ".wmf",
"image/svg+xml": ".svg",
}
return mapping.get(content_type, ".bin")
def process_pptx(pptx_path):
"""Process a single PPTX file. Returns list of image records."""
records = []
fname = pptx_path.name
stem = pptx_path.stem
out_folder = OUT_DIR / stem
out_folder.mkdir(parents=True, exist_ok=True)
try:
prs = Presentation(str(pptx_path))
except Exception as e:
print(f" ERROR opening {fname}: {e}")
return records
img_counter = 0
for slide_idx, slide in enumerate(prs.slides, start=1):
for shape in slide.shapes:
is_image = False
image_blob = None
content_type = None
# Direct picture shape
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
is_image = True
try:
image_blob = shape.image.blob
content_type = shape.image.content_type
except Exception:
pass
# Placeholder with an image
elif shape.shape_type == MSO_SHAPE_TYPE.PLACEHOLDER:
try:
if hasattr(shape, "image"):
image_blob = shape.image.blob
content_type = shape.image.content_type
is_image = True
except Exception:
pass
# Group shapes — check children
elif shape.shape_type == MSO_SHAPE_TYPE.GROUP:
try:
for child in shape.shapes:
if child.shape_type == MSO_SHAPE_TYPE.PICTURE:
img_counter += 1
try:
blob = child.image.blob
ct = child.image.content_type
except Exception:
blob = None
ct = "unknown"
ext = content_type_to_ext(ct) if ct else ".bin"
out_name = f"slide{slide_idx:02d}_img{img_counter:03d}{ext}"
if blob:
out_path = out_folder / out_name
out_path.write_bytes(blob)
alt_text = ""
try:
alt_text = child.name or ""
except Exception:
pass
records.append({
"file": fname,
"slide": slide_idx,
"img_num": img_counter,
"out_name": out_name,
"width_in": emu_to_inches(child.width),
"height_in": emu_to_inches(child.height),
"left_in": emu_to_inches(child.left),
"top_in": emu_to_inches(child.top),
"content_type": ct or "unknown",
"alt_text": alt_text,
"slide_texts": [],
"saved": blob is not None,
})
except Exception:
pass
continue
if not is_image:
continue
img_counter += 1
ext = content_type_to_ext(content_type) if content_type else ".bin"
out_name = f"slide{slide_idx:02d}_img{img_counter:03d}{ext}"
if image_blob:
out_path = out_folder / out_name
out_path.write_bytes(image_blob)
# Alt text
alt_text = ""
try:
alt_text = shape.name or ""
except Exception:
pass
# Nearby text on same slide
slide_texts = get_nearby_text(slide, shape)
records.append({
"file": fname,
"slide": slide_idx,
"img_num": img_counter,
"out_name": out_name,
"width_in": emu_to_inches(shape.width),
"height_in": emu_to_inches(shape.height),
"left_in": emu_to_inches(shape.left),
"top_in": emu_to_inches(shape.top),
"content_type": content_type or "unknown",
"alt_text": alt_text,
"slide_texts": slide_texts,
"saved": image_blob is not None,
})
print(f" {fname}: {len(prs.slides)} slides, {img_counter} images extracted")
return records
def build_catalog(all_records):
"""Write image_catalog.md."""
# Group by source file
by_file = defaultdict(list)
for r in all_records:
by_file[r["file"]].append(r)
lines = []
lines.append("# Image Catalog — Extracted from Original PPTX Lectures")
lines.append("")
lines.append(f"Generated: 2026-03-25")
lines.append("")
lines.append(f"**Total images extracted: {len(all_records)}**")
lines.append("")
# Summary table
lines.append("## Summary by Source File")
lines.append("")
lines.append("| Source PPTX | Chapter | Slides with Images | Total Images |")
lines.append("|---|---|---|---|")
for fname in sorted(by_file.keys()):
recs = by_file[fname]
chapter = CHAPTER_MAP.get(fname, "Unmapped")
slides_with_imgs = len(set(r["slide"] for r in recs))
lines.append(f"| {fname} | {chapter} | {slides_with_imgs} | {len(recs)} |")
lines.append("")
# Detailed listing per file
lines.append("---")
lines.append("")
lines.append("## Detailed Image Listing")
lines.append("")
for fname in sorted(by_file.keys()):
recs = by_file[fname]
chapter = CHAPTER_MAP.get(fname, "Unmapped")
stem = Path(fname).stem
lines.append(f"### {fname}")
lines.append(f"**Chapter:** {chapter}")
lines.append(f"**Output folder:** `images/ppt/{stem}/`")
lines.append("")
lines.append("| # | Slide | Filename | Size (W x H in.) | Type | Shape Name / Alt Text | Slide Context |")
lines.append("|---|---|---|---|---|---|---|")
for i, r in enumerate(recs, 1):
w = r["width_in"] or "?"
h = r["height_in"] or "?"
ct = r["content_type"].replace("image/", "")
alt = r["alt_text"].replace("|", "/") if r["alt_text"] else ""
# Truncate slide texts to keep table readable
ctx = "; ".join(r["slide_texts"])
if len(ctx) > 120:
ctx = ctx[:117] + "..."
ctx = ctx.replace("|", "/").replace("\n", " ")
saved = "" if r["saved"] else " (not saved)"
lines.append(f"| {i} | {r['slide']} | `{r['out_name']}`{saved} | {w} x {h} | {ct} | {alt} | {ctx} |")
lines.append("")
CATALOG_PATH.parent.mkdir(parents=True, exist_ok=True)
CATALOG_PATH.write_text("\n".join(lines), encoding="utf-8")
print(f"\nCatalog written to {CATALOG_PATH}")
def main():
print("Scanning PPTX files in originalMaterials/\n")
OUT_DIR.mkdir(parents=True, exist_ok=True)
pptx_files = sorted(SRC_DIR.glob("*.pptx"))
print(f"Found {len(pptx_files)} PPTX files\n")
all_records = []
for pf in pptx_files:
records = process_pptx(pf)
all_records.extend(records)
print(f"\nTotal images extracted: {len(all_records)}")
build_catalog(all_records)
if __name__ == "__main__":
main()