用 Python 制作可视化 GUI 界面,一键实现证件照背景颜色的替换
GUI
界面来方便大家使用。关于界面的大致模样其实和先前的相差不大,大家应该都看过上一篇的内容界面大体的样子

去除掉背景颜色
removebg
,官方链接是:
api_key
:https://www.remove.bg/api#remove-background
def remove_bg(self):
api_keys = "自己注册的api_key"
rmbg = RemoveBg(api_keys, "error.log")
rmbg.remove_background_from_img_file(imgNamepath)
添加上我们想要的颜色
no_bg_image = Image.open(in_path)
x, y = no_bg_image.size
new_image = Image.new('RGBA', no_bg_image.size, color="red")
new_image.paste(no_bg_image, (0, 0, x, y), no_bg_image)
new_image.save(output_path)
GUI
界面中用到的显示图片的控件是graphicsView
组件,我们在点击“选择图片”的按钮之后,在上传图片之后,需要在graphicsView
窗口当中将图片显示出来,代码如下def openImage(self):
global imgNamepath # 这里为了方便别的地方引用图片路径,将其设置为全局变量
imgNamepath, imgType = QFileDialog.getOpenFileName(self.ui, "选择图片", "D:\\", "*.png;;*.jpg;;All Files(*)")
# 通过文件路径获取图片文件,并设置图片长宽为label控件的长、宽
img = QtGui.QPixmap(imgNamepath).scaled(self.ui.graphicsView.size(), aspectMode=Qt.KeepAspectRatioByExpanding)
print("img: ", img.width(), img.height())
self.ui.graphicsView.setFixedSize(img.width(), img.height())
# 在label控件上显示选择的图片
item = QGraphicsPixmapItem(img)
scene = QGraphicsScene()
scene.addItem(item)
self.ui.graphicsView.setScene(scene)
self.ui.graphicsView.repaint()
# 显示所选图片的路径
self.ui.lineEdit.setText(imgNamepath)

往期回顾 分享
点收藏
点点赞
点在看
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/

随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 热爱与坚守 7904342
- 2 中核集团重大突破:成功产出第一桶铀 7808612
- 3 第二代居民身份证迎换证高峰 7714311
- 4 这份防中暑秘籍请收好 7616855
- 5 十分钟很短但这是她30年的痛苦 7520098
- 6 三次判刑仍提拔的局长曾枪击村民 7428139
- 7 净网:来看1起侵犯公民个人信息案 7327598
- 8 00后大学舍友 最熟悉的陌生人 7236297
- 9 村民捞手机索要1500元未果又扔水里 7141053
- 10 邓紫棋小说首日销售额破4000万 7043266