一、前言:
将上传的图片无损放大2倍、4倍、8倍
二、利用Flet写个前端页面:
三、前端页面代码:
page.title = "图片无所放大"page.vertical_alignment = "center"page.window_width = 500 # window's width is 200 pxpage.window_height = 700 # window's height is 200 pxpage.window_resizable = False # window is not resizablepage.window_center()page.update()t = flet.Text(value="图片无损放大软件", color="red", size=27, width=300, height=50,weight=flet.FontWeight.W_500, text_align="center")image = flet.Image(src="https://pic.imgdb.cn/item/65af57e8871b83018affa6db.jpg", width=270, height=270,fit=flet.ImageFit.CONTAIN)def radiogroup_changed(e):t1.value = f"你选择的放大倍数是: {e.control.value}"enlarge_number.value = 0if "2" in e.control.value:enlarge_number.value = 2elif "4" in e.control.value:enlarge_number.value = 4elif "8" in e.control.value:enlarge_number.value = 8page.update()def get_directory_result1(e: FilePickerResultEvent):directory_path1.value = e.files[0].path if e.files else "Cancelled!"image.src = directory_path1.value if directory_path1.value else " "# directory_path1.value = e.path if e.path else "Cancelled!"directory_path1.update()page.update()get_directory_dialog1 = FilePicker(on_result=get_directory_result1)# # Open directory dialog# def get_directory_result2(e: FilePickerResultEvent):# directory_path2.value = e.path if e.path else "Cancelled!"# directory_path2.update()## get_directory_dialog2 = FilePicker(on_result=get_directory_result2)# directory_path2 = Text()# Open directory dialogdef get_directory_result3(e: FilePickerResultEvent):directory_path3.value = e.path if e.path else "Cancelled!"directory_path3.update()get_directory_dialog3 = FilePicker(on_result=get_directory_result3)directory_path3 = Text()# hide all dialogs in overlaypage.overlay.extend([get_directory_dialog1, get_directory_dialog3])def button_clicked(e):if directory_path1.value:save_path = os.path.dirname(directory_path1.value) + "\save." + directory_path1.value.split(".")[-1]b.disabled = Truetry:enlarge_image(directory_path1.value, enlarge_number, save_path)# get_img_txt_ret(directory_path1.value, joint_path, directory_path3.value, t1, page, True)t1.value = "图片无所放大已完成。。。。"except Exception as e:t1.value = f"有点问题:{e}"print(e)else:t1.value = "请选择上传与保存文件路径。。。"page.update()t1 = Text()directory_path1 = Text()directory_path3 = Text()enlarge_number = Text()b = flet.ElevatedButton(text="确认提交", on_click=button_clicked, width=100, height=30)page.add(Row([t], alignment="center"),Row([FilledTonalButton("上传图片",icon=icons.FOLDER_OPEN,on_click=lambda _: get_directory_dialog1.pick_files(allow_multiple=True),disabled=page.web,width=180, height=40),directory_path1,],alignment="center"),Row([image],alignment="center"),Row([flet.ElevatedButton(text='放大倍数'),flet.RadioGroup(content=flet.Row([flet.Radio(value="2倍", label="2倍"),flet.Radio(value="4倍", label="4倍"),flet.Radio(value="8倍", label="8倍")]), on_change=radiogroup_changed)],alignment="center"),Row([FilledTonalButton("选择保存图片路径",icon=icons.FOLDER_OPEN,on_click=lambda _: get_directory_dialog3.get_directory_path(),disabled=page.web,width=200, height=40),directory_path3,],alignment="center"),Row([b], alignment="center"),Row([t1], alignment="center"),)
四、Python处理后端图片代码:
"""放大图片"""img = Image.open(image_path)# JPEG格式图片转换为RGB格式if "png" or "PNG" not in image_path:img = img.convert("RGB")# 获取图片尺寸image_h, image_w = img.height, img.widthprint(image_h, image_w)num = int(enlarge_number.value)# 进行无损放大new_size = (image_h * num, image_w * num)resized_img = img.resize(new_size, resample=Image.BICUBIC)# 保存图片resized_img.save(save_path)
五、利用auto-py-to-exe制作exe软件
具体教程参考:
python打包exe实用工具auto-py-to-exe的操作方法
六、页面示例:
七、下载地址:
蓝奏云:https://wwbs.lanpv.com/iTilS1m09wbg