forked from yuntaowu2000/trails-games-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfimg.py
25 lines (23 loc) · 1.06 KB
/
pdfimg.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
import fitz
import os
def pdf2pic(path, pic_path):
doc = fitz.open(path) # 打开pdf文件
imgcount = 0
for i in range(len(doc)):
imglist = doc.getPageImageList(i)
for j, img in enumerate(imglist):
xref = img[0]
pix = fitz.Pixmap(doc, xref) # make pixmap from image
new_name = "图片{}.png".format(imgcount) # 生成图片的名称
if pix.n - pix.alpha < 4: # can be saved as PNG
pix.writePNG(os.path.join(pic_path, new_name))
else: # CMYK: must convert first
pix0 = fitz.Pixmap(fitz.csRGB, pix)
pix0.writePNG(os.path.join(pic_path, new_name))
pix0 = None # free Pixmap resources
pix = None # free Pixmap resources
imgcount += 1
if __name__=='__main__':
path = r"D:\Downloads\那由多之轨迹图文流程攻略.pdf"
pic_path = r'C:\Users\yunta\Desktop\trails-game\tx_treasure\test'
pdf2pic(path, pic_path)