我们看下生成的pdf效果
发布时间:2025-06-24 18:30:06 作者:北方职教升学中心 阅读量:258
如果您对测评结果有疑问,欢迎交流,更多大模型案例在公众号首发。
很惊喜,通义千问修改代码后,代码就运行成功。通义千问、UI大小适中,pdf内容图片一样大小。
我们看下生成的pdf效果。'''defselect_folder():folder_path =filedialog.askdirectory()iffolder_path:pdf_output_path =os.path.join(folder_path,"output.pdf")images_list =[os.path.join(folder_path,img)forimg inos.listdir(folder_path)ifimg.endswith(('.png','.jpg','.jpeg'))]# 按照文件名排序,确保顺序正确 images_list.sort()withopen(pdf_output_path,"wb")asf:pdf_bytes =img2pdf.convert(images_list)f.write(pdf_bytes)print(f"PDF文件已生成并保存至:{pdf_output_path}")defmain():root =tk.Tk()root.title("图片转PDF工具")browse_button =tk.Button(root,text="选择图片文件夹",command=select_folder)browse_button.pack(pady=10)root.mainloop()if__name__ =="__main__":main()
结论:通义千问代码编写成功。importos importtkinter astk fromtkinter importfiledialog importimg2pdf ''' 模型:通义千问需求:把一个文件夹中的图片转换成pdf文件,要求有一个界面,可以选择图片所在路径,然后点击运行,可以生产pdf文件,然后输出pdf所在路径。


第一次运行有错误,很正常,我们继续把错误发给kimi,让他修复。
fromtkinter importTk,filedialog,Label,Button,Entry,mainloop fromPIL importImage fromreportlab.pdfgen importcanvas fromreportlab.lib.pagesizes importletter importos defresize_image(image_path,max_width=None,max_height=None):""" Resize an image to fit within the given maximum width and height. Aspect ratio is preserved. """withImage.open(image_path)asimg:orig_width,orig_height =img.size ifmax_width isNoneandmax_height isNone:# No resizing needed if no max width or height is specified returnimg ifmax_width isNone:max_width =orig_width ifmax_height isNone:max_height =orig_height # Calculate the scaling factor to fit the image width_ratio =max_width /float(orig_width)height_ratio =max_height /float(orig_height)ratio =min(width_ratio,height_ratio)# Resize the image new_size =(int(orig_width *ratio),int(orig_height *ratio))resized_img =img.resize(new_size,Image.ANTIALIAS)returnresized_img defconvert_images_to_pdf(image_folder,output_pdf):images =[img forimg inos.listdir(image_folder)ifimg.lower().endswith((".png",".jpg",".jpeg",".tiff",".bmp",".gif"))]ifnotimages:print("No images found in the folder.")returnc =canvas.Canvas(output_pdf,pagesize=letter)height =letter[1]-72# 72 is the margin at the top forimg_name inimages:img_path =os.path.join(image_folder,img_name)# Resize the image to fit the page width (or a specified maximum size) resized_img =resize_image(img_path,max_width=letter[0]-72)# 72 is for the left and right margins # Calculate the new height based on the resized width to preserve aspect ratio new_height =int((resized_img.size[1]*(letter[0]-72)/resized_img.size[0])# Draw the resized image onto the canvas c.drawImage(resized_img,36,height -new_height,width=resized_img.size[0],height=new_height)# Update the current height for the next image height -=new_height +12# 12 is the gap between images ifheight <36:# Add a new page if needed c.showPage()height =letter[1]-72# Save the PDF and close the canvas c.save()print(f"PDF saved to: {output_pdf}")defselect_directory():directory =filedialog.askdirectory()entry_image_folder.delete(0,'end')entry_image_folder.insert(0,directory)# 创建GUI界面 root =Tk()root.title('Image to PDF Converter')# 图片文件夹输入 label_image_folder =Label(root,text='Select Image Folder:')label_image_folder.pack()entry_image_folder =Entry(root,width=50)entry_image_folder.pack()# 选择文件夹按钮 button_select_folder =Button(root,text='Select Folder',command=select_directory)button_select_folder.pack()# 转换按钮 button_convert =Button(root,text='Convert to PDF',command=lambda:convert_images_to_pdf(entry_image_folder.get(),'output.pdf'))button_convert.pack()# 运行GUI主循环 mainloop()
我想让他继续优化pdf内容,但是接下来再调试,也无法调试成功。


有一个错误,让他修复,修复后成功。
importos importtkinter astk fromtkinter importfiledialog importimg2pdf ''' 模型:通义千问需求:把一个文件夹中的图片转换成pdf文件,要求有一个界面,可以选择图片所在路径,然后点击运行,可以生产pdf文件,然后输出pdf所在路径。


第一次运行有错误,很正常,我们继续把错误发给kimi,让他修复。
fromtkinter importTk,filedialog,Label,Button,Entry,mainloop fromPIL importImage fromreportlab.pdfgen importcanvas fromreportlab.lib.pagesizes importletter importos defresize_image(image_path,max_width=None,max_height=None):""" Resize an image to fit within the given maximum width and height. Aspect ratio is preserved. """withImage.open(image_path)asimg:orig_width,orig_height =img.size ifmax_width isNoneandmax_height isNone:# No resizing needed if no max width or height is specified returnimg ifmax_width isNone:max_width =orig_width ifmax_height isNone:max_height =orig_height # Calculate the scaling factor to fit the image width_ratio =max_width /float(orig_width)height_ratio =max_height /float(orig_height)ratio =min(width_ratio,height_ratio)# Resize the image new_size =(int(orig_width *ratio),int(orig_height *ratio))resized_img =img.resize(new_size,Image.ANTIALIAS)returnresized_img defconvert_images_to_pdf(image_folder,output_pdf):images =[img forimg inos.listdir(image_folder)ifimg.lower().endswith((".png",".jpg",".jpeg",".tiff",".bmp",".gif"))]ifnotimages:print("No images found in the folder.")returnc =canvas.Canvas(output_pdf,pagesize=letter)height =letter[1]-72# 72 is the margin at the top forimg_name inimages:img_path =os.path.join(image_folder,img_name)# Resize the image to fit the page width (or a specified maximum size) resized_img =resize_image(img_path,max_width=letter[0]-72)# 72 is for the left and right margins # Calculate the new height based on the resized width to preserve aspect ratio new_height =int((resized_img.size[1]*(letter[0]-72)/resized_img.size[0])# Draw the resized image onto the canvas c.drawImage(resized_img,36,height -new_height,width=resized_img.size[0],height=new_height)# Update the current height for the next image height -=new_height +12# 12 is the gap between images ifheight <36:# Add a new page if needed c.showPage()height =letter[1]-72# Save the PDF and close the canvas c.save()print(f"PDF saved to: {output_pdf}")defselect_directory():directory =filedialog.askdirectory()entry_image_folder.delete(0,'end')entry_image_folder.insert(0,directory)# 创建GUI界面 root =Tk()root.title('Image to PDF Converter')# 图片文件夹输入 label_image_folder =Label(root,text='Select Image Folder:')label_image_folder.pack()entry_image_folder =Entry(root,width=50)entry_image_folder.pack()# 选择文件夹按钮 button_select_folder =Button(root,text='Select Folder',command=select_directory)button_select_folder.pack()# 转换按钮 button_convert =Button(root,text='Convert to PDF',command=lambda:convert_images_to_pdf(entry_image_folder.get(),'output.pdf'))button_convert.pack()# 运行GUI主循环 mainloop()
我想让他继续优化pdf内容,但是接下来再调试,也无法调试成功。


有一个错误,让他修复,修复后成功。
上面还有一个小问题,是通义千问给的UI并没有“运行”按钮,如果需要,可以继续让他添加按钮,但是已经很好了。
算了,原谅它的无知,我手动添加括号试试。
按照代码质量排序:通义千问> kimi / 文心一言 。
kimi仿佛进入了错误死循环,始终无法修复错误,我认为他可能上下文理解错乱,所以重新描述了自己的需求,让他重新作答。
最终UI效果:
最终PDF内容:
最终代码:
通义千问给的代码更少,选择的库img2pdf
比较合适。
🤖AI改变生活:最近都在说月之暗面的kimi的各项能力吊打国内其他大模型,今天我们真实感受下 kimi、
按照编码纠错效率排序:通义千问 > 文心一言 > kimi。
最终我放弃和他的对话。结果依然无法使用。测评结果让人震惊!
kimi
kimi编程过程
我们先看一下热捧的月之暗面的kimi模型。
通义千问
通义千问编码过程
无一例外第一次都会报错。放弃吧~
结论:文心一言代码编写失败。


而且错误变得很离谱,连包都没有导入。文心一言的根据需求写代码的能力。
最终代码
文心一言和kimi给的代码非常多,使用canvas
库,复杂度更高,更难以调试。
又经过多轮对话,我发现他始终无法修复自己的错误。
最终代码:
kimi的代码越改越复杂,使用canvas
更复杂,有大部分错误发生在canvas绘制和字体选择上,没编程基础,都无法调试。请保证相关的包都导入,如果用到字体,使用宋体常规。
总结
按照编码运行结果排序:通义千问 > 文心一言 > kimi。
文心一言后面就和kimi一样,进入了错误循环。
文心一言
文心一言编码过程
文心一言给的这段代码连编译都错误,很明显下面代码中少了一个右括号
img =img.resize((int(img_width *0.75),int(img_height *0.75)),Image.ANTIALIAS
经过我的提示,它好像明白了,但是给的修正后的代码依然没有添加右括号🙄。
大概又经过4次调试,代码终于调通,能够生成pdf,但是文件内容不完整。
importos fromtkinter importTk,Button,Label,Entry,filedialog,messagebox fromPIL importImage fromreportlab.pdfbase importpdfmetrics fromreportlab.pdfbase.ttfonts importTTFont fromreportlab.pdfgen importcanvas fromreportlab.lib.pagesizes importA4 # 注册字体 defregister_font(font_name,font_path):try:pdfmetrics.registerFont(TTFont(font_name,font_path))exceptIOError ase:print("Error registering font:",e)# 合并图片到PDF defmerge_images_to_pdf(folder_path,output_pdf_path):register_font('SimSun',r'C:\Windows\Fonts\simsun.ttc')# 确保提供正确的字体文件路径 pdf =canvas.Canvas(output_pdf_path,pagesize=A4)current_page =1page_height =pdf._pagesize[1]images =[os.path.join(folder_path,f)forf inos.listdir(folder_path)iff.lower().endswith(('.png','.jpg','.jpeg','.bmp'))]forimg_path inimages:img =Image.open(img_path).convert('RGB')# 确保图像为RGB模式 width,height =img.size ifheight >page_height:# 如果图片高度大于页面高度,需要创建新页面 pdf.showPage()current_page +=1page_height =pdf._pagesize[1]pdf.drawImage(img,width,height,x=(A4[0]-width)/2,y=page_height -height)pdf.save()messagebox.showinfo("Success","PDF Created Successfully!")# 选择文件夹 defselect_folder(entry):folder_path =filedialog.askdirectory()entry.delete(0,'end')entry.insert(0,folder_path)# GUI应用程序的主函数 defmain():root =Tk()root.title("Image to PDF Converter")Label(root,text="Select the folder containing images:").pack()folder_path_entry =Entry(root,width=50)folder_path_entry.pack()Button(root,text="Browse",command=lambda:select_folder(folder_path_entry)).pack()Button(root,text="Run",command=lambda:merge_images_to_pdf(folder_path_entry.get(),"merged.pdf")).pack()root.mainloop()if__name__ =="__main__":main()
结论:kimi代码编写失败。
#AIGC #kimi #通义千问 #文心一言 #大模型

pdf内容是出来了,我们再让他优化下程序,将UI调大,pdf中图片调成一样大小。