2017-01-26 27 views
1

我正在處理圖像處理程序,它在哪裏創建popart圖像。問題是,我得到了一些「入侵者」像素,其中藍色區域在這裏和那裏都有一些綠色像素。我想讓這些像素消失,但我似乎無法找到這樣做的方法。如果您還有另一種製作popart圖片的方式,請隨時提出。這裏是我的腳本:PIL刪除不同的像素

from tkinter import* 
import tkinter as Tkinter 
from tkinter import filedialog, DISABLED 
import os 
import ntpath 
from PIL import Image 

def EchelleDeGris(): 
    Ima2=Image.new("RGB",(z[0],z[1])) 
    px=Ima1.load() 
    px1=Ima2.load() 
    for x in range(z[0]): 
     for y in range(z[1]): 
      p=px[x,y] 
      o=int((p[0]+p[1]+p[2])/3) 
      px1[x,y]=(o,o,o) 
    Ima2.save("ImageMod.png") 
    im2 = PhotoImage(file="ImageMod.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(row=0, column=3, rowspan =6) 

def SupprimerImage(): 
    I2 = Tkinter.Label(main, image=imt) 
    I2.grid(row=0, column=3, rowspan =6) 

def Luminosite(): 
    Ima2=Image.new("RGB",(z[0],z[1])) 
    px=Ima1.load() 
    px1=Ima2.load() 
    for x in range(z[0]): 
     for y in range(z[1]): 
      p=px[x,y] 
      px1[x,y]=(p[0]+S1.get(),p[1]+S1.get(),p[2]+S1.get()) 
    Ima2.save("ImageMod.png") 
    im2 = PhotoImage(file="ImageMod.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(row=0, column=3, rowspan =6) 

def AnnulerModifications(): 
    I2 = Tkinter.Label(main, image=im1) 
    I2.grid(row=0, column=3, rowspan =6) 

def PopArt(): 
    Ima2=Image.new("RGB",(z[0],z[1])) 
    px=Ima1.load() 
    px1=Ima2.load() 
    for x in range(z[0]): 
     for y in range(z[1]): 
      p=px[x,y] 
      if type(p)==int: 
       p=(p,p,p) 
      o=int((p[0]+p[1]+p[2])/3) 
      if o<85: 
       px1[x,y]=(0,0,255) 
      elif 85<o<170: 
       px1[x,y]=(0,255,0) 
      elif o>170: 
       px1[x,y]=(255,0,0) 
    Ima2.save("ImageMod.png") 
    im2 = PhotoImage(file="ImageMod.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(row=0, column=3, rowspan =6) 






main=Tk() 

main.withdraw() 
currdir = os.getcwd() 
a = filedialog.askopenfilename() 
main.deiconify() 

main.configure(background="#a1dbcd") 
main.title("Photoshop Version.Megzari") 

Ima1=Image.open(a) 
z=Ima1.size 
nux=Image.new("RGB",(z[0],z[1])) 
nuxy=nux.load() 
for x in range(z[0]): 
    for y in range(z[1]): 
     nuxy[x,y]=(255,255,255) 
nux.save("Blank.png") 








if z>(400,400): 
    im2 = Tkinter.PhotoImage(file="ImageBlank.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(padx=20, pady=20, row=0, column=1, rowspan =6) 
    T1 = Tkinter.Label(main, image=im2) 
    T1.grid(padx=20, pady=20, row=0, column=3, rowspan =6) 
    B1 = Tkinter.Button(main, text ="Echelle de gris", command = EchelleDeGris, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B1.grid(padx=20, pady=20, row=0, column=2) 
    B3 = Tkinter.Button(main, text ="Appliquer Luminosité", command = Luminosite, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B3.grid(padx=20, pady=20, row=2, column=2) 
    S1 = Scale(main, from_=0, to=254, orient=HORIZONTAL, fg="#a1dbcd", bg="#383a39", state=DISABLED, length = 200) 
    S1.grid(row=1, column=2) 
    B2 = Tkinter.Button(main, text ="Supprimer Image", command = SupprimerImage, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B2.grid(padx=20, pady=20, row=4, column=2) 
    B3 = Tkinter.Button(main, text ="Annuler Modifications", command = AnnulerModifications, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B3.grid(padx=20, pady=20, row=3, column=2) 
    B4 = Tkinter.Button(main, text ="Pop Art", command = PopArt, fg="#a1dbcd", bg="#383a39", state=DISABLED) 
    B4.grid(padx=20, pady=20, row=5, column=2) 
elif z<(400,400): 
    im1 = Tkinter.PhotoImage(file=a) 
    I1 = Tkinter.Label(main, image=im1) 
    I1.grid(padx=20, pady=20, row=0, column=1, rowspan =6) 
    imt = Tkinter.PhotoImage(file="Blank.png") 
    T1 = Tkinter.Label(main, image=imt) 
    T1.grid(padx=20, pady=20, row=0, column=3, rowspan =6) 
    B1 = Tkinter.Button(main, text ="Echelle de gris", command = EchelleDeGris, fg="#a1dbcd", bg="#383a39", state=NORMAL) 
    B1.grid(padx=20, pady=20, row=0, column=2) 
    B3 = Tkinter.Button(main, text ="Appliquer Luminosité", command = Luminosite, fg="#a1dbcd", bg="#383a39") 
    B3.grid(padx=20, pady=20, row=2, column=2) 
    S1 = Scale(main, from_=0, to=254, orient=HORIZONTAL, fg="#a1dbcd", bg="#383a39", length = 200) 
    S1.grid(row=1, column=2) 
    B2 = Tkinter.Button(main, text ="Supprimer Image", command = SupprimerImage, fg="#a1dbcd", bg="#383a39") 
    B2.grid(padx=20, pady=20, row=4, column=2) 
    B3 = Tkinter.Button(main, text ="Annuler Modifications", command = AnnulerModifications, fg="#a1dbcd", bg="#383a39") 
    B3.grid(padx=20, pady=20, row=3, column=2) 
    B4 = Tkinter.Button(main, text ="Pop Art", command = PopArt, fg="#a1dbcd", bg="#383a39") 
    B4.grid(padx=20, pady=20, row=5, column=2) 

s=S1.get() 




main.mainloop() 

只是爲了幫助,這裏是使POPART圖像的程序的一部分:

def PopArt(): 
    Ima2=Image.new("RGB",(z[0],z[1])) 
    px=Ima1.load() 
    px1=Ima2.load() 
    for x in range(z[0]): 
     for y in range(z[1]): 
      p=px[x,y] 
      if type(p)==int: 
       p=(p,p,p) 
      o=int((p[0]+p[1]+p[2])/3) 
      if o<85: 
       px1[x,y]=(0,0,255) 
      elif 85<o<170: 
       px1[x,y]=(0,255,0) 
      elif o>170: 
       px1[x,y]=(255,0,0) 
    Ima2.save("ImageMod.png") 
    im2 = PhotoImage(file="ImageMod.png") 
    main.image = im2 
    I2 = Tkinter.Label(main, image=im2) 
    I2.grid(row=0, column=3, rowspan =6) 

這裏是圖像及其結果:

face.png chev.png

face.png modified chev.png modified

如果需要什麼就問我始終可用

編輯:這裏是代碼,我在評論中談到的卡,你只需將其卡在POPART函數的末尾:

n=0 
j=0 
q=0 

for x in range(1,z[0]):  
    for y in range(1,z[1]): 
     for t in range(-1,1): #These two loops change the pixel "coordinates" 
      for w in range(-1,1): 
       v=px1[x+t,y+w] 
       if v[0]==255: 
        n=n+1 
       elif v[1]==255: #These are the counters 
        j=j+1 
       elif v[2]==255: 
        q=q+1 

     Max=n  #And this checks the dominant color 
     if j>Max: 
      Max=j 
     elif q>Max: 
      Max=q 
     elif j>q: 
      Max=j 

     if n==Max: 
      px2[x,y]=(255,0,0) #And here I assign the color depending on the surrounding colors 
     elif j==Max: 
      px2[x,y]=(0,255,0) 
     elif q==Max: 
      px2[x,y]=(0,0,255) 
     n=0 
     j=0 
     q=0 
Ima3.save("ImageMod.png") 

而這就是我得到:

face.png modified

我知道我犯了一個錯誤,當我開始使用簡單的名稱變量z,我應該用Original_Image之類的東西。該程序有太多變量來改變它們,但是在我未來的項目中,我不會犯這個錯誤。

+0

你可以添加你如何算法的工作進行了簡要概述? 'EchelleDeGris'建議*灰度*,對吧? – usr2564301

+1

@RadLexus是的,我用法語'EchelleDeGris()'寫的確實意味着_grayscale_,'SupprimerImage()'去除圖像,'Luminosite()'根據比例值改變圖像的亮度,'AnnulerModifications()'種類取消添加到圖像中的效果,並且'PopArt()'很明顯;) –

回答

2

起初,我認爲你在你的程序中沒有幫助的命名。關於法語函數名稱不僅是 ,它主要是關於諸如「z」的短變量名稱。

但是,如果我是正確的,我看到你知道如何讀取和寫入二維矩陣中的每個像素的RGB代碼。所以你只需要爲每個像素分析八個相鄰像素 - 如果一個像素擁有不止一半的不同顏色的相鄰像素 - 根據最流行的相鄰顏色對其進行着色。重複,直到所有像素都具有相同顏色的最受歡迎的鄰居。

當然,這只是一個簡單的邏輯,可能會有所改進,但我希望我已經展示了這個想法。

Upd:

我寫了一些代碼來實現我的想法。工作非常緩慢,但結果似乎是正確的。

from PIL import Image 
from collections import Counter 


def get_pixel(pixels, x, y): 
    try: 
     return pixels[x, y] 
    except IndexError: 
     return None 


def get_neighbors(pixels, x,): 
    neighbors = list() 
    neighbors.append(get_pixel(pixels, x, y - 1)) 
    neighbors.append(get_pixel(pixels, x, y + 1)) 
    neighbors.append(get_pixel(pixels, x - 1, y)) 
    neighbors.append(get_pixel(pixels, x + 1, y)) 
    neighbors.append(get_pixel(pixels, x - 1, y - 1)) 
    neighbors.append(get_pixel(pixels, x - 1, y + 1)) 
    neighbors.append(get_pixel(pixels, x + 1, y - 1)) 
    neighbors.append(get_pixel(pixels, x + 1, y + 1)) 
    return neighbors 


def filter_art(pixels, size): 
    indexes = dict() 
    for x in range(size[0]): 
     for y in range(size[1]): 
      color = get_pixel(pixels, x, y) 
      neighbors = get_neighbors(pixels, x, y) 
      new_color = Counter(neighbors).most_common()[0][0] 
      if new_color is not None: 
       indexes[x, y] = new_color 
    for x, y in indexes: 
     pixels[x, y] = indexes[x, y] 


def pop_art(path_orig, path_mod, coef): # coef is integer value, meant how deep filtering would be (for example, coef=4) 
    image_orig = Image.open(path_orig) 
    size = image_orig.size 
    image_mod = Image.new("RGB",(size[0],size[1])) 
    pixels_orig = image_orig.load() 
    pixels_mod = image_mod.load() 
    for x in range(size[0]): 
     for y in range(size[1]): 
      p = pixels_orig[x, y] 
      if isinstance(p, int): # this should be done using PIL palletes and converting to exact pallete at first, 
       # but now I omit this for my quick test 
       rgb = (p,p,p) 
      elif isinstance(p, tuple) and len(p) in (3, 4): 
       rgb = p[:3] 
      else: 
       raise TypeError('Unknown pallete') 
      average_color = sum(rgb)/3 
      if average_color <= 85: 
       pixels_mod[x, y] = (255, 0, 0) # you also need care about guarantee correct PIL pallete format here (omitted) 
      elif 85 < average_color <= 170: 
       pixels_mod[x, y] = (0, 255, 0) 
      elif pixels_mod[x, y] > 170: 
       pixels_mod[x, y] = (0, 0, 255) 
    for _ in range(coef): 
     filter_art(pixels_mod, size) 
    image_mod.save(path_mod) 


if __name__ == '__main__': 
    pop_art('obama.png', 'result.png', coef=4) 

enter image description here

+0

我已經試過了,它給了我可怕的結果。我期望它能夠去除'入侵者'像素,但它可以讓它們變得更大。我爲它周圍的八個像素(3x3的正方形)製作了一個計數器,並對顏色進行了計數(我擁有像(0,0,255)這樣的絕對值)然後檢查主色,並將其分配給原始顏色,但不是。 –

+0

我寫了一些代碼來實現我的想法,但是我做了一個深度係數來在合理的時間內實現正確的圖形,你可能在你的某個地方有一個錯誤儘管如此,我的代碼的性能還是太差,需要大幅改進,我在代碼中添加了代碼和結果圖片 –

+0

另一個想法是make算法,它可以找到面積比某個係數小的固體區域,肯定它應該在像素網格中進行一次迭代,你實現快速優化算法的任務並不是很微不足道,所以你需要努力思考,或者找到一個用於相同任務的應用程序(有些像這樣的應該肯定已經是exi st,但我認爲這個想法是學習如何)。 –