• 表情包制作


    对着泊松融合的算法描述口胡了代码,没想到确实能看出效果

    存在问题:1.计算时用了整除,导致部分区域色调改变

         2.插入图片的边缘没有设成透明,出现一片糊

           3.本质上是口胡算法

    # -*- coding: cp936 -*-
    from PIL import Image
    
    back=Image.open("1.bmp")
    man=Image.open("2.bmp")
        
    pixel1=back.load()
    pixel2=man.load()
    
    n1=680
    m1=453
    n2=119
    m2=173
    begn=300
    begm=140
    
    div=[]
    for i in range(n1):
      div.append([])
      for j in range(m1):
        div[i].append([])
        for chan in range(3):
          div[i][j].append([])
    for i in range(1,n1-1):
      for j in range(1,m1-1):
        for chan in range(3):
          div[i][j][chan]=pixel1[i-1,j][chan]+pixel1[i,j-1][chan]+pixel1[i+1,j][chan]+pixel1[i,j+1][chan]-4*pixel1[i,j][chan]
    for i in range(1,n2-1):
      for j in range(1,m2-1):
        for chan in range(3):
          div[i+begn][j+begm][chan]=pixel2[i-1,j][chan]+pixel2[i,j-1][chan]+pixel2[i+1,j][chan]+pixel2[i,j+1][chan]-4*pixel2[i,j][chan]
    
    back.save("T1.bmp")
    
    for T in range(1,1001):
      im=Image.open("T%d.bmp"%T)
      pixel=im.load()
      for i in range(n1):
        for j in range(m1):
          if i>0 and i<(n1-1) and j>0 and j<(m1-1):
            im.putpixel((i,j),(-(div[i][j][0]-pixel[i-1,j][0]-pixel[i,j-1][0]-pixel[i+1,j][0]-pixel[i,j+1][0])/4,-(div[i][j][1]-pixel[i-1,j][1]-pixel[i,j-1][1]-pixel[i+1,j][1]-pixel[i,j+1][1])/4,-(div[i][j][2]-pixel[i-1,j][2]-pixel[i,j-1][2]-pixel[i+1,j][2]-pixel[i,j+1][2])/4))
          else:
            im.putpixel((i,j),(pixel[i,j][0],pixel[i,j][1],pixel[i,j][2]))
      im.save('T%d.bmp'%(T+1))

    大致效果

     -----------------------------------------------------------------------

    进行了一些迷之修改,去除了边缘空白与色调变化问题

    # -*- coding: cp936 -*-
    # 打开图片
    from PIL import Image
    
    back=Image.open("1.bmp")
    man=Image.open("2.bmp")
    mark=Image.open("3.bmp")
        
    pixel1=back.load()
    pixel2=man.load()
    pixel3=mark.load()
    
    n1=680
    m1=453
    n2=119
    m2=173
    begn=300
    begm=140
    
    div=[]
    for i in range(n1):
      div.append([])
      for j in range(m1):
        div[i].append([])
        for chan in range(3):
          div[i][j].append([])
    for i in range(1,n1-1):
      for j in range(1,m1-1):
        for chan in range(3):
          div[i][j][chan]=pixel1[i-1,j][chan]+pixel1[i,j-1][chan]+pixel1[i+1,j][chan]+pixel1[i,j+1][chan]-4*pixel1[i,j][chan]
    
    for i in range(1,n2-1):
      for j in range(1,m2-1):
        gray=0.3*pixel3[i,j][0]/256+0.59*pixel3[i,j][1]/256+0.11*pixel3[i,j][2]/256
        if gray<=0.5:
        #黑色是(0,0,0)...
          for chan in range(3):
            div[i+begn][j+begm][chan]=pixel2[i-1,j][chan]+pixel2[i,j-1][chan]+pixel2[i+1,j][chan]+pixel2[i,j+1][chan]-4*pixel2[i,j][chan]
        #else:
          #print "%d,%d,(%d,%d,%d)
    "%(i,j,pixel3[i,j][0],pixel3[i,j][1],pixel3[i,j][2])
        
    back.save("T1.bmp")
    col=[]
    tcol=[]
    for i in range(n1):
      col.append([])
      tcol.append([])
      for j in range(m1):
        col[i].append([])
        tcol[i].append([])
        for chan in range(3):
          col[i][j].append([])
          tcol[i][j].append([])
          col[i][j][chan]=pixel1[i,j][chan]
    
    for T in range(1,1001):
      for i in range(n1):
        for j in range(m1):
          for chan in range(3):
            if i>0 and i<(n1-1) and j>0 and j<(m1-1):
              tcol[i][j][chan]=1.0*(col[i-1][j][chan]+col[i][j-1][chan]+col[i+1][j][chan]+col[i][j+1][chan]-div[i][j][chan])/4.0    
            else:
              tcol[i][j][chan]=col[i][j][chan]
      for i in range(n1):
        for j in range(m1):
          for chan in range(3):
            col[i][j][chan]=tcol[i][j][chan]
          back.putpixel((i,j),(int(round(tcol[i][j][0])),int(round(tcol[i][j][1])),(int(round(tcol[i][j][2])))))
          
      back.save('T%d.bmp'%(T+1))

  • 相关阅读:
    模板
    洛谷
    Codeforces
    Codeforces
    Codeforces
    Codeforces
    洛谷
    洛谷
    洛谷
    NOIP 普及组 2016 海港
  • 原文地址:https://www.cnblogs.com/zhujiangning/p/11480804.html
Copyright © 2020-2023  润新知