大家好,我是Lex 喜欢欺负超人那个Lex
建议大家收藏哦,以后帮小姐姐P自拍、证件照、调尺寸、背景、抠图,直接10行代码搞定,瞬间高大上。
事情是这样的
晚上,正在聚精会神写代码
突然,收到隔壁小姐姐给我发来的消息

还有一张证件自拍照
而且是可以放在结婚证上的那种哦

  
就是 之前帮过她几次忙
难道要以身相许 去一起办证

原来是照片尺寸不合适
让我帮她修图。还要什么蓝底、红底各种背景的

虽然有些失落
还是,默默的撸出了我39米长的python大刀

先上效果
1、尺寸长宽调整为:295x413
2、背景色调为蓝底 和 红底各一张
3、还要一张透明背景的证件照。
原图↓↓↓

啪啪啪一顿操作,效果如下↓↓↓

环境准备
此处,我们需要用到两个python模块:pillow和removebg
pillow模块:用于调整照片的像素大小。
removebg模块:用于抠图,调整背景。
  
   - 
    
     
    
    
     
      #安装python模块
     
    
- 
    
     
    
    
     
      pip install pillow
     
    
- 
    
     
    
    
     
      pip install removebg
     
    

证件照尺寸调整
先来调整尺寸吧,调好了,再来调整背景颜色。
诗诗小姐姐说,她考试要求的照片尺寸:295x413
  
   - 
    
     
    
    
     
      from PIL 
      import Image
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      old_img = 
      'C:/Users/lex/desktop/img/诗诗.png'
     
    
- 
    
     
    
    
     
      new_img = 
      'C:/Users/lex/desktop/img/诗诗-new.png'
     
    
- 
    
     
    
    
     
      img = Image.open(old_img)
     
    
- 
    
     
    
    
     
      #读取照片尺寸
     
    
- 
    
     
    
    
     
      (x,y) = img.size
     
    
- 
    
     
    
    
     
      #重新设置照片尺寸
     
    
- 
    
     
    
    
     
      x_s = 
      295 
      #宽
     
    
- 
    
     
    
    
     
      y_s = 
      413 
      #高
     
    
- 
    
     
    
    
     
      out = img.resize((x_s,y_s),Image.ANTIALIAS) 
      #resize image with high-quality
     
    
- 
    
     
    
    
     
      out.save(new_img)
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      print (
      '原始照片尺寸(宽x高): ',x,
      "x",y)
     
    
- 
    
     
    
    
     
      print (
      '调整后照片尺寸:(宽x高) ',x_s,
      "x",y_s)
     
    
啪啪啪一顿操作,照片尺寸调好了
如下图 ↓↓↓
证件照背景调整
1、通过removebg模块的方法,我们可以把人像抠图出来。
2、我们通过颜色背景来定义三个背景颜色
  
   - 
    
     
    
    
     
      BACKGROUND_COLOR = {
     
    
- 
    
     
    
    
     
          'RED': (255, 0, 0, 255),
     
    
- 
    
     
    
    
     
          'BLUE': (67, 142, 219, 255),
     
    
- 
    
     
    
    
     
          'WHITE': (255, 255, 255, 255)
     
    
- 
    
     
    
    
     
      }
     
    
3、将抠出来的无背景的图片 粘贴到我们自己画的背景板上
  
   - 
    
     
    
    
     
      #老照片路径、新照片路径、无背景照片路径、颜色
     
    
- 
    
     
    
    
     
      def get_img_bg(old_img_path,new_img_path,no_bg_img_path,color):
     
    
- 
    
     
    
    
         
      #去掉背景图,提取照片
     
    
- 
    
     
    
    
     
          rmbg.remove_background_from_img_file(old_img_path)
     
    
- 
    
     
    
    
     
          foreground = Image.open(no_bg_img_path)
     
    
- 
    
     
    
    
     
          background = Image.new(
      'RGBA', foreground.size, BACKGROUND_COLOR[color])  
      # 背景图,大小同前景图
     
    
- 
    
     
    
    
     
          background.paste(foreground, mask=foreground)
     
    
- 
    
     
    
    
     
          background.save(new_img_path)
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      if __name__ == 
      '__main__':
     
    
- 
    
     
    
    
     
          get_img_bg(
      'C:/Users/pacer/Desktop/img/诗诗.png',
      'C:/Users/pacer/desktop/img/诗诗_red.png',
      'C:/Users/pacer/desktop/img/诗诗.png_no_bg.png',
      'RED')
     
    
- 
    
     
    
    
     
          get_img_bg(
      'C:/Users/pacer/Desktop/img/诗诗.png',
      'C:/Users/pacer/desktop/img/诗诗_blue.png',
      'C:/Users/pacer/desktop/img/诗诗.png_no_bg.png',
      'BLUE')
     
    
啪啪啪代码一顿执行,所有照片都拿到了
各种背景颜色图片
原图、透明背景、蓝色背景、红色背景图片全部生成。

完整代码
  
   - 
    
     
    
    
     
      from os 
      import name
     
    
- 
    
     
    
    
     
      from PIL 
      import Image
     
    
- 
    
     
    
    
     
      from removebg 
      import RemoveBg
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      BACKGROUND_COLOR = {
     
    
- 
    
     
    
    
         
      'RED': (
      255, 
      0, 
      0, 
      255),
     
    
- 
    
     
    
    
         
      'BLUE': (
      67, 
      142, 
      219, 
      255),
     
    
- 
    
     
    
    
         
      'WHITE': (
      255, 
      255, 
      255, 
      255)
     
    
- 
    
     
    
    
     
      }
     
    
- 
    
     
    
    
     
      #调用removebg模块
     
    
- 
    
     
    
    
     
      rmbg = RemoveBg(
      'ogNLSpKn7VFeeamhVn7yaEhu', 
      'error.log')
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      #生成红色背景证件照
     
    
- 
    
     
    
    
     
      #老照片路径、新照片路径、无背景照片路径、颜色
     
    
- 
    
     
    
    
     
      def get_img_bg(old_img_path,new_img_path,no_bg_img_path,color):
     
    
- 
    
     
    
    
         
      #去掉背景图,提取照片
     
    
- 
    
     
    
    
     
          rmbg.remove_background_from_img_file(old_img_path)
     
    
- 
    
     
    
    
     
          foreground = Image.open(no_bg_img_path)
     
    
- 
    
     
    
    
     
          background = Image.new(
      'RGBA', foreground.size, BACKGROUND_COLOR[color])
     
    
- 
    
     
    
    
     
          background.paste(foreground, mask=foreground)
     
    
- 
    
     
    
    
         
      #background.show()
     
    
- 
    
     
    
    
     
          background.save(new_img_path)
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      if __name__ == 
      '__main__':
     
    
- 
    
     
    
    
     
          get_img_bg(
      'C:/Users/pacer/Desktop/img/诗诗.png',
      'C:/Users/pacer/desktop/img/诗诗_red.png',
      'C:/Users/pacer/desktop/img/诗诗.png_no_bg.png',
      'RED')
     
    
- 
    
     
    
    
     
          get_img_bg(
      'C:/Users/pacer/Desktop/img/诗诗.png',
      'C:/Users/pacer/desktop/img/诗诗_blue.png',
      'C:/Users/pacer/desktop/img/诗诗.png_no_bg.png',
      'BLUE')
     
    
结尾
啪啪啪 敲了半个小时代码 之后,
我把P好的证件照,发给了小姐姐
这次,反响很强烈


推荐阅读
python及安全系列
【渗透测试】python你TM太皮了——区区30行代码就能记录键盘的一举一动
【渗透实战】女神相册密码忘记了,我只用Python写了20行代码~~~
【渗透测试】密码暴力破解工具——九头蛇(hydra)使用详解及实战
【渗透学习】Web安全渗透详细教程+学习线路+详细笔记【全网最全+建议收藏】
【渗透案例】如何用ssh工具连接前台小姐姐的“小米手机”——雷总看了直呼内行!!!
【渗透测试】密码暴力破解工具——九头蛇(hydra)使用详解及实战
pygame系列文章
一起来学pygame吧 游戏开发30例(三)——射击外星人小游戏
一起来学pygame吧 游戏开发30例(四)——俄罗斯方块小游戏
一起来学pygame吧 游戏开发30例(五)——消消乐 小游戏
一起来学pygame吧 游戏开发30例(六)——高山滑雪 小游戏
转载:https://blog.csdn.net/weixin_42350212/article/details/116936268
 
					