界面的大体外观其实和之前的没有太大区别。界面概况整体GUI界面如下图所示:用户在使用时可以选择将证件照更换为“白底”或“红底”,然后在正面上传照片后——end接口,然后end上的程序就会开始执行它应该有的操作。去除背景色首先,我们需要去除照片的背景色。这里使用第三方接口removebg。官方链接为:我们完成账号注册后,访问以下链接获取api_key:https://www.remove.bg/api#remove-background下面是对应的程序代码,如下:defremove_bg(self):api_keys="self-registeredapi_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.sizenew_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窗口中进行设置。图片显示出来,代码如下:defopenImage(self):globalimgNamepath#这里为了方便其他地方引用图片路径,设置为全局变量imgNamepath,imgType=QFileDialog.getOpenFileName(self.ui,"SelectPicture","D:\\","*.png;;*.jpg;;AllFiles(*)")#通过文件路径获取图片文件,并设置长宽图片的长宽作为标签控件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())#在标签控件上显示选中的图片item=QGraphicsPixmapItem(img)scene=QGraphicsScene()scene.addItem(item)self.ui.graphicsView.setScene(scene)self.ui.graphicsView.repaint()#显示选中图片的路径self.ui.lineEdit.setText(imgNamepath)最后来看一下整体效果
