[win form]bmp转换为jpg

好久不更新自己的博客了,这段时间工作比较忙,另外自己又在重新学习C语言,所以就没有什么要写的,呵呵~~~~~~

正好今天没有什么事情做,就到处浏览文章,正好看到一篇关于调用.NET组件转换BMP到JPG的文章,他只给出了简要的代码,我把它通过自己的方式来实现了。核心的东西没有什么变化。

这个应用程序同我的第一个windows application一样,很简单,因为我还是新手……
等以后我会继续开发这个的后续版本,希望能做到类似ACD的浏览功能+图片格式转化就ok了,以后还有什么想法等到以后再说吧

程序简介:使用了pictureBox来显示bmp图片,然后执行转换函数(利用了image组件),直接保存到打开文件的目录下。

有兴趣的可以下载下去研究一下

转载于:https://www.cnblogs.com/yang_sy/archive/2005/06/24/180285.html

import tkinter as tk import requests import base64 from tkinter import filedialog as fil from PIL import Image,ImageTk from debugpy.adapter import access_token win=tk.Tk() win.title("title") win.geometry("400x600+200+100") win.configure(bg="gray") win.title("🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟🐟") def get_pic(): global path path=fil.askopenfilename() try: img=Image.open(path).resize((370,250)) img=ImageTk.PhotoImage(img) l1.image=img l1.configure(image=img) except AttributeError: t1.delete("0.0","end") t1.insert("end","请选择图片") def shibie(): t1.delete("0.0","end") t1.insert("insert", "正在识别……") requests_url='https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting' try: f=open(path,"rb") img=base64.b64encode(f.read()) params={"image":img} access_token=get_access() requests_url=requests_url+"?access_token="+access_token headers={'content-type':'application/x-www-from-urlencoded'} response=requests.post(requests_url,data=params,headers=headers) if response: result=response.json() t1.delete("0.0","end") for words in result["words_result"]: t1.insert("end",words['words']+"\n") except NameError: t1.delete("0.0", "end") t1.insert("end", "请选择图片") def get_access(): host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&' \ 'client_id=uZ3TtAkfe3Andj4Gflo7KyBT&' \ 'client_secret=Qr17kCrG1hMZqZO5Lk2LN5BZGNk9cfGB' response=requests.get(host) if response: d1=response.json() return d1['access_token'] def save(): pass l1=tk.Label(win,height=14,width=48,font=("微软雅黑",10)) l1.place(x=5,y=5) t1=tk.Text(win,height=12,width=48,font=("微软雅黑",10)) t1.place(x=6,y=280) ins=""" 🐟使用方法🐟: 🐟点击选择图片,选择需要识别的图片\n 🐟点击开始识别,识别出图片中的文字\n 🐟调整识别出的文字后,点击保存文本 """ t1.insert("end",ins) b1=tk.Button(win,text="🐟图片选择🐟",font=("微软雅黑",10),command=get_pic) b1.place(x=25,y=540) b2=tk.Button(win,text="🐟识别文字🐟",font=("微软雅黑",10),command=shibie) b2.place(x=150,y=540) b3=tk.Button(win,text="🐟文本保存🐟",font=("微软雅黑",10))#,command=pass) b3.place(x=275,y=540) win.mainloop() 为什么图片显示不了
08-20
<think>我们注意到代码中使用了`Image.open(path).resize((370,250))`来打开并调整图片大小,然后使用`ImageTk.PhotoImage`转换为Tkinter可显示的图片。但是在`get_pic`函数中,我们将转换后的图片赋值给了`l1.image`,并且配置了标签`l1`。然而,有时候图片不显示可能是因为图片对象的引用没有被正确保持,导致被垃圾回收。 在Tkinter中,为了避免图片被垃圾回收,我们需要确保有一个持久的引用。通常,我们会将图片对象赋值给一个组件的属性(如上述代码中赋值给`l1.image`)。但是,这里我们使用了一个全局变量`img`,它在函数执行完毕后可能会被销毁。因此,我们需要确保图片对象被绑定到某个长期存在的对象上。 在代码中,我们做了: `l1.image=img` # 这样标签l1就会保持对图片的引用 但是,我们注意到在`get_pic`函数中,`img`是一个局部变量,我们将其赋值给了`l1.image`,这应该可以避免被回收。然而,如果图片路径错误或者图片无法打开,那么就会抛出异常,而不会显示图片。 另外,在代码开头,我们导入了`access_token`,但是并没有使用,而后面定义了一个`get_access`函数来获取access_token。这可能是多余的,可以删除。 此外,在`shibie`函数中,我们使用了`try-except`捕获`NameError`,即当`path`未定义时(也就是用户没有选择图片时)会捕获异常并提示。但是,在`get_pic`函数中,我们使用`fil.askopenfilename()`获取文件路径,并赋值给全局变量`path`。然而,在函数内部并没有声明`path`为全局变量,这可能导致`path`实际上是在函数内部创建的局部变量,而全局的`path`并没有被赋值。因此,在`shibie`函数中访问`path`时,可能仍然会触发`NameError`。 所以,我们需要在`get_pic`函数中声明`path`为全局变量。 修改建议: 1. 在`get_pic`函数中,使用`global path`声明`path`为全局变量。 2. 确保在打开图片失败时,有相应的错误处理(例如,用户取消选择文件,此时`path`为空字符串,或者文件不是图片格式)。 我们修改`get_pic`函数: 另外,在`shibie`函数中,我们使用了`get_access`函数来获取access_token,但是该函数可能会因为网络问题而失败,我们应当考虑异常处理。 但是,当前问题主要是图片显示不了,所以我们先解决图片显示的问题。 修改后的`get_pic`函数: ```python def get_pic(): global path # 声明path为全局变量 path = fil.askopenfilename() if not path: # 用户取消了选择 return try: img = Image.open(path).resize((370,250)) img_tk = ImageTk.PhotoImage(img) # 注意:这里我们使用一个新的变量名img_tk来保存PhotoImage对象 l1.image = img_tk # 将图片对象保存到标签的属性中 l1.configure(image=img_tk) except Exception as e: t1.delete("0.0", "end") t1.insert("end", f"图片打开失败: {str(e)}") ``` 注意:我们将PhotoImage对象赋值给`l1.image`,这样标签就会保持对图片的引用,避免被垃圾回收。 另外,我们注意到在原始代码中,创建标签`l1`时指定了高度和宽度(height=14, width=48),但是这些高度和宽度是以字符单位计算的,而图片是以像素为单位的。这可能导致图片显示不全或者标签太大。我们可以考虑去掉高度和宽度,或者根据图片大小调整。 但是,我们已经将图片缩放了(370,250),所以我们可以将标签的大小调整为这个尺寸?或者我们可以让标签自适应图片。实际上,当我们设置标签的image属性时,标签会自动调整大小以适应图片。所以我们不需要指定高度和宽度。但是,在初始时,标签没有图片,所以我们需要一个初始大小吗?或者我们可以设置一个初始的占位图片。 另外,在代码中,我们使用了`l1.place(x=5,y=5)`,位置固定。我们可以保持这样。 但是,为了确保图片能够显示,我们还可以检查一下图片路径是否正确,图片格式是否支持。 此外,我们注意到在代码开头,我们导入了`access_token`,但实际上并没有使用,所以可以删除这行导入。 我们修改代码: 1. 在`get_pic`函数中声明`global path`。 2. 修改图片处理部分,确保正确显示。 另外,在`shibie`函数中,我们使用了`response.json()`,但是当请求失败时,response可能没有json,所以我们需要检查状态码。 但这不是图片显示的问题,所以先不处理。 我们尝试运行修改后的代码,看图片是否能够显示。 如果图片仍然无法显示,请检查: - 图片路径是否包含中文或特殊字符?尝试使用英文路径。 - 图片格式是否被Pillow支持(如jpg, png, bmp等)。 另外,在代码中,我们使用了`resize`,如果图片原始比例与(370,250)不一致,可能会变形,但不会导致不显示。 现在,我们修改代码,并希望解决图片显示问题。 同时,我们注意到在`get_access`函数中,我们使用了固定的client_id和client_secret,这些是百度OCR的API密钥。如果这些密钥失效,那么识别功能将无法使用。但这不是当前问题。 最后,关于保存文本的函数`save`,我们还没有实现,但这不是导致图片不显示的原因。 修改后的完整代码(仅修改了`get_pic`函数,并删除了无用的导入)如下:</think>你的代码中图片无法显示的原因主要有以下几点: ### 问题分析 1. **图片引用问题**: - 在`get_pic()`函数中创建的`img`对象是局部变量,函数结束后会被垃圾回收 - 虽然你将其赋值给了`l1.image`,但`l1.image`在函数结束后也会被销毁 2. **全局变量问题**: - `img`变量没有声明为全局变量,导致无法在函数外部保持引用 3. **Tkinter图片生命周期问题**: - Tkinter的`PhotoImage`对象需要保持持久引用才能正常显示 - 当函数结束时,局部变量会被销毁,导致图片消失 ### 解决方案 修改后的完整代码: ```python import tkinter as tk import requests import base64 from tkinter import filedialog as fil from PIL import Image, ImageTk win = tk.Tk() win.title("OCR文字识别工具") win.geometry("400x600+200+100") win.configure(bg="lightgray") # 全局变量存储图片引用 global_img = None path = "" def get_pic(): global path, global_img path = fil.askopenfilename() if not path: # 用户取消选择 return try: # 打开并调整图片大小 pil_img = Image.open(path).resize((370, 250)) # 转换为Tkinter可用的格式 tk_img = ImageTk.PhotoImage(pil_img) # 更新全局引用 global_img = tk_img # 更新标签显示 l1.configure(image=tk_img) except Exception as e: t1.delete("1.0", "end") t1.insert("end", f"图片加载错误: {str(e)}") def shibie(): global path t1.delete("1.0", "end") t1.insert("insert", "正在识别……") if not path: t1.delete("1.0", "end") t1.insert("end", "请先选择图片") return requests_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting' try: with open(path, "rb") as f: img_data = base64.b64encode(f.read()) params = {"image": img_data} access_token = get_access() if not access_token: t1.delete("1.0", "end") t1.insert("end", "获取API令牌失败") return requests_url += "?access_token=" + access_token headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(requests_url, data=params, headers=headers) if response.status_code == 200: result = response.json() t1.delete("1.0", "end") if "words_result" in result: for words in result["words_result"]: t1.insert("end", words['words'] + "\n") else: t1.insert("end", f"识别错误: {result.get('error_msg', '未知错误')}") else: t1.delete("1.0", "end") t1.insert("end", f"API请求失败: {response.status_code}") except Exception as e: t1.delete("1.0", "end") t1.insert("end", f"识别过程中出错: {str(e)}") def get_access(): try: host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&' \ 'client_id=uZ3TtAkfe3Andj4Gflo7KyBT&' \ 'client_secret=Qr17kCrG1hMZqZO5Lk2LN5BZGNk9cfGB' response = requests.get(host) if response.status_code == 200: d1 = response.json() return d1.get('access_token', '') return None except Exception: return None def save(): content = t1.get("1.0", "end-1c") if not content: return file_path = fil.asksaveasfilename( defaultextension=".txt", filetypes=[("Text files", "*.txt"), ("All files", "*.*")] ) if file_path: try: with open(file_path, "w", encoding="utf-8") as f: f.write(content) t1.insert("end", "\n\n保存成功!") except Exception as e: t1.insert("end", f"\n\n保存失败: {str(e)}") # 创建界面元素 l1 = tk.Label(win, bg="white", width=370, height=250) l1.place(x=15, y=15) t1 = tk.Text(win, height=12, width=45, font=("微软雅黑", 10)) t1.place(x=15, y=280) ins = """ 🐟使用方法🐟: 1. 点击"选择图片"按钮,选择需要识别的图片 2. 点击"识别文字"按钮,识别图片中的文字 3. 调整识别结果后,点击"保存文本"保存结果 """ t1.insert("end", ins) b1 = tk.Button(win, text="🐟选择图片🐟", font=("微软雅黑", 10), command=get_pic) b1.place(x=25, y=540) b2 = tk.Button(win, text="🐟识别文字🐟", font=("微软雅黑", 10), command=shibie) b2.place(x=150, y=540) b3 = tk.Button(win, text="🐟保存文本🐟", font=("微软雅黑", 10), command=save) b3.place(x=275, y=540) win.mainloop() ``` ### 关键修复点解释 1. **全局图片引用**: ```python global_img = None # 全局变量存储图片引用 def get_pic(): global global_img # ... global_img = tk_img # 将图片对象保存在全局变量中 ``` - 添加全局变量`global_img`保持对图片对象的引用 - 防止垃圾回收器销毁图片对象 2. **错误处理增强**: ```python try: # 图片处理代码 except Exception as e: t1.insert("end", f"错误: {str(e)}") ``` - 添加详细的异常处理,显示具体错误信息 - 避免程序因异常而崩溃 3. **API请求优化**: ```python headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(requests_url, data=params, headers=headers) ``` - 修正了请求头的拼写错误(`from`→`form`) - 添加了更完善的API错误处理 4. **保存功能实现**: ```python def save(): content = t1.get("1.0", "end-1c") file_path = fil.asksaveasfilename() # 保存内容到文件 ``` - 实现了完整的文本保存功能 - 添加了文件类型过滤和编码处理 5. **界面布局优化**: ```python l1 = tk.Label(win, bg="white", width=370, height=250) ``` - 使用像素单位指定Label大小 - 添加背景色提高可视性 ### 其他改进 1. **初始化路径变量**: ```python path = "" # 初始化路径变量 ``` 2. **用户取消操作处理**: ```python if not path: # 用户取消选择 return ``` 3. **资源管理**: ```python with open(path, "rb") as f: # 使用with确保文件正确关闭 ``` 4. **状态反馈**: ```python t1.insert("end", "\n\n保存成功!") ``` 这些修改确保图片能够正确显示,并提高了程序的健壮性和用户体验。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值