python 图片转pdf_python1-图片转成pdf

把图片转成pdf

需要先把图片(.jpg or png)放到 image这个文件夹里

然后使用

from PIL import Image

import os

def rea(path, pdf_name):

## change all png into jpg & delete the .png files

names=os.listdir(path)

for name in names:

img=Image.open(path+'/'+name)

name=name.split(".")

if name[-1] == "png":

name[-1] = "jpg"

name_jpg = str.join(".", name)

r,g,b,a=img.split()

img=Image.merge("RGB",(r,g,b))

to_save_path = path + '/'+name_jpg

img.save(to_save_path)

os.remove(path+'/'+name[0]+'.png')

else:

continue

## add jpg and jpeg to

file_list = os.listdir(path)

pic_name = []

im_list = []

for x in file_list:

if "jpg" in x or 'jpeg' in x:

pic_name.append(x)

pic_name.sort() #sorted

new_pic = []

for x in pic_name:

if "jpg" in x:

new_pic.append(x)

im1 = Image.open(os.path.join(path, new_pic[0]))

new_pic.pop(0)

for i in new_pic:

img = Image.open(os.path.join(path, i))

# im_list.append(Image.open(i))

if img.mode == "RGBA":

r,g,b,a=img.split()

img=Image.merge("RGB",(r,g,b))

img = img.convert('RGB')

im_list.append(img)

else:

im_list.append(img)

im1.save(pdf_name, "PDF", resolution=100.0, save_all=True, append_images=im_list)

print("输出文件名称:", pdf_name)

if __name__ == '__main__':

# input the name for pdf like xxx.pdf

pdf_name = 'image2pdf.pdf'

mypath='image'

if ".pdf" in pdf_name:

rea(mypath, pdf_name=pdf_name)

else:

rea(mypath, pdf_name="{}.pdf".format(pdf_name))

相关资源:Python高级编程.pdf
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页