python - 将图像 (JPEG) 转换为 PDF
问题描述
我正在尝试将 JPEG 转换为 PDF。但是,当我运行代码并将 JPEG 的路径指定为输入和 PDF 时,此图片中出现此错误
起初我使用的是一个使用 GUI tkinter 的代码,但是有很多错误,所以我改成了这个代码。
编码:
import PIL.Image
import os
import re
# To sort files to stay at the same order
numbers = re.compile(r'(\d+)')
def numericalSort(value):
parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
in_file = ""
out_file = ""
def process():
in_file= input("In File: ")
out_file = input("Out File: ")
os.chdir(in_file)
lst = os.listdir()
lst = sorted(lst, key=numericalSort)
counter = 1
for i in lst:
print(i)
image1 = PIL.Image.open(i)
im1 = image1.convert('RGB')
out = out_file + "\\" + str(counter) + ".pdf"
im1.save(out)
counter += 1
for i in range(1,counter):
print(out_file+ "/"+str(i)+".pdf")
process()
def main():
def getInDir():
global in_file
in_file = filedialog.askdirectory()
myLabel = Label(root, text="Input File is: " + in_file)
myLabel.grid(row= 4, column = 2,padx=20, pady=10)
def getOutDir():
global out_file
out_file = filedialog.askdirectory()
myLabel = Label(root, text="Output File in: " + out_file)
myLabel.grid(row= 6, column = 2,padx=20, pady=10)
myInButton = Button(root, text = "Entre Input File", command = getInDir)
myInButton.grid(row = 0, column = 2,padx=20, pady=5)
myOutButton = Button(root, text = "Entre Output File", command = getOutDir)
myOutButton.grid(row = 2 , column = 2,padx=20, pady=5)
okButton = Button(root, text = "Convert", command = process)
okButton.grid(row = 8 , column = 2,padx=20, pady=5)
root.mainloop()
# main()
解决方案
推荐阅读
- hibernate - 带有额外列的 JPA 2.0 多对多 - 更新集合
- reactjs - 流量 | 还原 | 无法通过 StateProps 和 DispatchProps 连接函数
- java - 如何从具有特定首字母的字符串中打印出一个单词?
- python - 在 for 语句中使用 concurrent.futures
- python-3.x - 在 Python 中使用 unittest.mock 模拟 os.remove
- angular - Angular 8 - 在其他调用它的组件上调用共享组件两次
- r - (R,RStudio)Tidyverse 不会加载 - '没有这样的文件或目录'
- angular - Angular如何在http post中定义多个观察
- database - Oracle:Blob 列抛出不一致的数据类型
- php - Validate Unique - 传递给 Illuminate\Validation\Factory::make() 的 TypeError 参数 1 必须是数组类型,给定 null