2017-01-27 48 views
0

我試圖製作一個popart圖像(顯示在下面),但我不知道如何調整圖像大小,然後將其分割爲1個大圖像上共享的9個較小圖像,保持與原始大小相同一。 這裏的腳本(這使得POPART是def Popart()腳本的一部分:PIL如何在1幅新圖像上調整大小,分割和共享圖像?

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() 
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

出於這種(我知道的顏色是不是很完美): chev.png

我也知道,我喜歡用簡單的z變量,我應該例如Original_Image使用。我只是想知道如何調整它,並把它分解,同時保持圖像的原始大小。

回答

0

我有9個不同的圖像,每個圖像都命名爲result0.png; result1.png; result2.png;...,所以我創建了一個包含這些圖像的列表,然後我用for循環一次打開它們,然後根據它們的位置調整它們的大小,創建的圖像。 這裏是代碼:

Img1=[None]*9 
    pixels1=[None]*9 
    Imaz=Image.new("RGB",(basewidth*3,hsize*3)) 
    pixels=Imaz.load() 
    for i in range(9): 
     Img1[i]=Image.open(''+dir_path+'\\PopArt\\Resized Images\\resized_image'+str(i)+'.png') 
     pixels1[i]=Img1[i].load() 


    for x in range(0,basewidth): 
     for y in range(0,hsize): 
      pixels[x,y]=pixels1[0][x,y] 
     for y in range(hsize,hsize*2): 
      pixels[x,y]=pixels1[1][x,y-hsize] 
     for y in range(hsize*2,hsize*3): 
      pixels[x,y]=pixels1[2][x,y-hsize*2] 

    for x in range(basewidth,basewidth*2): 
     for y in range(0,hsize): 
      pixels[x,y]=pixels1[3][x-basewidth,y] 
     for y in range(hsize,hsize*2): 
      pixels[x,y]=pixels1[4][x-basewidth,y-hsize] 
     for y in range(hsize*2,hsize*3): 
      pixels[x,y]=pixels1[5][x-basewidth,y-hsize*2] 

    for x in range(basewidth*2,basewidth*3): 
     for y in range(0,hsize): 
      pixels[x,y]=pixels1[6][x-basewidth*2,y] 
     for y in range(hsize,hsize*2): 
      pixels[x,y]=pixels1[7][x-basewidth*2,y-hsize] 
     for y in range(hsize*2,hsize*3): 
      pixels[x,y]=pixels1[8][x-basewidth*2,y-hsize*2] 
    Imaz = Imaz.resize((size[0] , size[1]), PIL.Image.ANTIALIAS) 
    Imaz.save(""+dir_path+"\\PopArt\\Result Image\\result.png") 
相關問題