Advertisement
mrAnderson33

КГ Лаба 2(1)

Mar 15th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. from PIL import Image as IMG, ImageDraw,ImageTk
  2. from tkinter.filedialog import askopenfilename
  3. from tkinter import *
  4. import random, math, copy
  5. import win32com.client, pythoncom, time
  6.  
  7. def Laplassian(image, c, mode, mode2):
  8.     im = copy.copy(image)
  9.     draw = ImageDraw.Draw(im)  
  10.     pix = image.load()
  11.  
  12.     for i in range(1, width-1):
  13.         for j in range(1, height-1):
  14.             f = pix[i, j][0]
  15.             # if (f<135 and not mode):
  16.             #     print('yeah')
  17.             xn = pix[i+1, j][0]
  18.             xp = pix[i-1, j][0]
  19.             yn = pix[i, j+1][0]
  20.             yp = pix[i, j-1][0]
  21.             xnd = pix[i+1, j-1][0]
  22.             xpd = pix[i-1, j+1][0]
  23.             ynd = pix[i+1, j+1][0]
  24.             ypd = pix[i-1, j-1][0]
  25.             if (mode):
  26.                 lap = -(xn + xp + yn + yp )+(4 * f)
  27.             else:
  28.                 lap = -(xn + xp + yn + yp + xnd + ynd + xpd + ypd) + (8 * f)
  29.             if (lap<0):
  30.                 lap = 0
  31.             if ( lap>255):
  32.                 lap = 255
  33.             g = int(f + (c * lap))
  34.             if (g < 0):
  35.                 g = 0
  36.             if (g > 255):
  37.                 g = 255
  38.             draw.point((i, j), (lap,lap,lap) if mode2 else (g,g,g))
  39.     return im
  40.  
  41.  
  42. image = IMG.open(askopenfilename())
  43.  
  44. split = image.filename.split("/")
  45. imagename = split[len(split)-1]
  46.  
  47. width = image.size[0]  
  48. height = image.size[1]
  49. pix = image.load()
  50.  
  51. # make grey image
  52. draw = ImageDraw.Draw(image)
  53. for i in range(width):
  54.     for j in range(height):
  55.         a,b,c = pix[i, j][:3]
  56.         # S = int(0.7*a + 0.2*b + 0.1*c)
  57.         S = int((0.2989 * a + 0.5870 * b + 0.1140 * c))
  58.         #S = int(a + b + c)//3
  59.         draw.point((i, j), (S, S, S))
  60.  
  61. # mask 4
  62. mask4 = Laplassian(image, 0, 1, 1)
  63. #  mask 8
  64. mask8 = Laplassian(image, 0, 0, 1)
  65. # c = 0.5 mask 4
  66. mask4half = Laplassian(image, 0.5, 1, 0)
  67. # c = 0.5 mask 8
  68. mask8half = Laplassian(image, 0.5, 0, 0)
  69. # c = 1 mask 8
  70. mask8one = Laplassian(image, 1, 0, 0)
  71.  
  72. images = [image,mask4,mask8,mask4half,mask8half,mask8one]
  73.  
  74. root = Tk()
  75. #root.bind("<Button>", button_click_exit_mainloop)
  76. canvas = Canvas(root,width=999,height=999)
  77. canvas.pack()
  78. shape = (2,3)
  79. xlen,ylen = 0,0
  80. imgs = [x.resize((530,300)) for x in images]
  81. xsize = sum(im.size[0] for im in imgs)
  82. ysize = sum(im.size[1] for im in imgs)
  83. root.geometry('%dx%d' % (xsize,ysize))
  84. tkpi = [ImageTk.PhotoImage(im,master=canvas) for im in imgs]
  85. lbl = Label(root,text='Вторая лаба Задание №1', justify=LEFT)
  86. lbl.place(relx=.4, rely=.75)
  87. lbl.config(font=('times', 20, 'bold'))
  88.  
  89. for i in range(shape[0]):
  90.     for j in range(shape[1]):
  91.         index = i*shape[1]+j
  92.         label_image = Label(root, image=tkpi[index])
  93.         label_image.place(x=xlen,y=ylen,width=imgs[index].size[0],height=imgs[index].size[1])
  94.         if j == shape[1]-1:
  95.             ylen = ylen + imgs[index].size[1]
  96.             xlen = 0
  97.         else:
  98.             xlen = xlen + imgs[index].size[0]
  99. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement