利用Python实现多种风格的照片处理

作者:欣一 时间:2021-05-04 13:26:27 

前言

在上一篇教程当中呢,小编向大家展示了如何通过一键点击实现将头像变成动漫风的实践,无非是在制作的UI界面当中,在用户上传了照片之后,后端的脚本在接收到照片之后,借助对抗生成神经网络来生成具有动漫风格的头像。

用Python制作可视化GUI界面,一键实现将头像转成动漫风!

利用Python实现多种风格的照片处理

今天小编想在上述成果的基础之上,添加上将“修复老照片”的功能,曾经在抖音上面也是吸引了不少的流量,内容就是发布一张老照片修复后的效果呈现,毕竟老照片容易引起人的共鸣,甚至有不少人通过修复老照片,找到失散多年的亲人。

利用Python实现多种风格的照片处理

照片修复的具体实操

那么我们就在已经完成的UI界面上面再添加几个按钮,如下图所示,分别是“动漫风格”、“老照片修复”以及“素描风格”

利用Python实现多种风格的照片处理

当我们点击“动漫风格”时,程序会针对先前的步骤将上传的图片变成动漫风格的头像,当我们点击“老照片修复”时,会针对上传的照片进行修复,对应的代码是Github上面一个名叫DeOldify的项目,在Github上面获得了15.2K的小星星,可谓是非常的火爆,该作者给我们提供了一个多语言版本的接口,调用这个接口我们可以快速的使用该项目的能力,为老照片上色,代码如下

import requests
r = requests.post(
   "https://api.deepai.org/api/colorizer",
   files={
       'image': open('自己本地图片的路径', 'rb'),
   },
   headers={'api-key': '........'}
)
output_url = r.json()["output_url"]

同时我们也需要将图片保存在本地,代码如下

def deoldify_action(self):
   r = requests.post(
       "https://api.deepai.org/api/colorizer",
       files={
           'image': open(imgNamepath, 'rb'),
       },
       headers={'api-key': 'fe26be2a-b66e-4cfc-8f4d-514f683e9812'}
   )
   output_url = r.json()["output_url"]
   ## 将图片保存在本地
   response = requests.get(output_url, stream=True)
   image_name = imgNamepath.split(".")[0]
   try:
       with open(image_name + '_oldified.png', 'wb') as logFile:
           for chunk in response:
               logFile.write(chunk)
           logFile.close()
           print("Download done!")
   except Exception as e:
       print("Download log error!")

image_name = imgNamepath.split(".")[0]
   imgShow = QtGui.QPixmap(image_name + '_oldified' + ".png").scaled(self.ui.label_5.size(), aspectMode=Qt.KeepAspectRatio)
   ## 将变换过之后的照片显示在界面上
   self.ui.label_5.setFixedSize(imgShow.width(), imgShow.height())
   self.ui.label_5.setScaledContents(True)
   self.ui.label_5.setPixmap(imgShow)
   print(f"image saved: {image_name}")

最后出来的效果如下图所示

利用Python实现多种风格的照片处理

将照片风格素描化的具体实操

而当我们点击“素描风格”的单选框之后,便开始将上传的图片变成素描风格,代码如下

def startAction_sumiao(self):
   img = cv2.imread(imgNamepath)
   gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
   inverted_gray_image = 255 - gray_image
   blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19, 19), 0)
   image_name = imgNamepath.split(".")[0]
   inverted_blurred_image = 255 - blurred_inverted_gray_image
   sketck = cv2.divide(gray_image, inverted_blurred_image, scale=256.0)
   ## 图像保存在本地
   cv2.imwrite(image_name + '_sumiao.png', sketck)
   ## 将变换过之后的照片显示在界面上
   imgShow = QtGui.QPixmap(image_name + '_sumiao' + ".png").scaled(self.ui.label_5.size(), aspectMode=Qt.KeepAspectRatio)
   self.ui.label_5.setFixedSize(imgShow.width(), imgShow.height())
   self.ui.label_5.setScaledContents(True)
   self.ui.label_5.setPixmap(imgShow)

我们来看一下具体的效果,如下图所示

利用Python实现多种风格的照片处理

来源:https://mp.weixin.qq.com/s/6dmbq_dIld4uNdLChPY7wA

标签:Python,照片,处理
0
投稿

猜你喜欢

  • 全国哀悼日 建议站点换素装(附代码)

    2008-05-19 12:05:00
  • Python通过OpenCV的findContours获取轮廓并切割实例

    2022-12-11 22:33:05
  • python目标检测yolo1 yolo2 yolo3和SSD网络结构对比

    2022-05-24 16:16:39
  • SQL Server修改标识列方法 如自增列的批量化修改

    2012-06-06 19:42:35
  • 关于Django显示时间你应该知道的一些问题

    2023-10-23 06:26:21
  • python使用多线程+socket实现端口扫描

    2023-04-14 19:21:14
  • python 进阶学习之python装饰器小结

    2023-05-12 07:13:42
  • Windows下使Python2.x版本的解释器与3.x共存的方法

    2021-03-14 22:22:17
  • 用Python抢过年的火车票附源码

    2021-11-12 00:01:42
  • Python常用断言函数实例汇总

    2023-07-04 18:15:23
  • Python字典中items()函数案例详解

    2021-02-23 04:47:31
  • Python subprocess模块学习总结

    2022-04-29 02:17:40
  • Python爬虫小练习之爬取并分析腾讯视频m3u8格式

    2022-07-03 08:44:32
  • 趣用文化元素

    2009-09-03 11:53:00
  • Python性能优化技巧

    2021-06-29 12:48:32
  • 详解new function(){}和function(){}()

    2008-02-28 12:28:00
  • python中如何使用朴素贝叶斯算法

    2023-02-26 08:51:39
  • Python MongoDB 插入数据时已存在则不执行,不存在则插入的解决方法

    2022-11-16 20:48:41
  • 使用Python做定时任务及时了解互联网动态

    2021-07-08 17:54:16
  • ASP运行出错:缺少对象: xmlDoc.documentElement错误解决方法

    2012-11-30 20:40:52
  • asp之家 网络编程 m.aspxhome.com