-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathtest_extractimage.py
57 lines (45 loc) · 1.72 KB
/
test_extractimage.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
"""
Extract images from a PDF file, confirm number of images found.
"""
import os
import pymupdf
scriptdir = os.path.abspath(os.path.dirname(__file__))
filename = os.path.join(scriptdir, "resources", "joined.pdf")
known_image_count = 21
def test_extract_image():
doc = pymupdf.open(filename)
image_count = 1
for xref in range(1, doc.xref_length() - 1):
if doc.xref_get_key(xref, "Subtype")[1] != "/Image":
continue
img = doc.extract_image(xref)
if isinstance(img, dict):
image_count += 1
assert image_count == known_image_count # this number is know about the file
def test_2348():
pdf_path = f'{scriptdir}/test_2348.pdf'
document = pymupdf.open()
page = document.new_page(width=500, height=842)
rect = pymupdf.Rect(20, 20, 480, 820)
page.insert_image(rect, filename=f'{scriptdir}/resources/nur-ruhig.jpg')
page = document.new_page(width=500, height=842)
page.insert_image(rect, filename=f'{scriptdir}/resources/img-transparent.png')
document.ez_save(pdf_path)
document.close()
document = pymupdf.open(pdf_path)
page = document[0]
imlist = page.get_images()
image = document.extract_image(imlist[0][0])
jpeg_extension = image['ext']
page = document[1]
imlist = page.get_images()
image = document.extract_image(imlist[0][0])
png_extension = image['ext']
print(f'jpeg_extension={jpeg_extension!r} png_extension={png_extension!r}')
assert jpeg_extension == 'jpeg'
assert png_extension == 'png'
def test_delete_image():
doc = pymupdf.open(os.path.abspath(f'{__file__}/../../tests/resources/test_delete_image.pdf'))
page = doc[0]
xref = page.get_images()[0][0]
page.delete_image(xref)