简体   繁体   中英

Python TrackBar convert the image to B&W

I want to make a TrackBar that translates the image into black and white. The application is on dextop, the image is read from a file. The location of the TrackBar is at the top, above image. Advance thanks.

import os
import cv2
import numpy
import skimage
import skimage.io as io
from tkinter import *
from PIL import ImageTk

# creating canvas and settings of color and brush's  size
canvas_width = 960
canvas_height = 1280
brush_size = 3
color = 'black'


# painting
def paint(event):
    global brush_size
    global color
    x1 = event.x - brush_size
    x2 = event.x + brush_size
    y1 = event.y - brush_size
    y2 = event.y + brush_size
    w.create_oval(x1,y1,x2,y2,fill=color,outline=color)

#razmer
def brush_size_change(new_size):
    global brush_size
    brush_size = new_size
#color change
def color_change(new_color):
    global color
    color = new_color

#reading image
root = Tk()
image = ImageTk.PhotoImage(file = "C:/Users/nikit/Desktop/Практика/lb3.jpg")

#creating
w = Canvas(root,width=canvas_width,
           height=canvas_height,
           bg='white')
#bind lkm
w.bind('<B1-Motion>', paint)
#creating buttons
red_btn = Button(text='Красный',width=10,
                 command=lambda: color_change('red'))

five_btn = Button(text='5',width=5,
                  command=lambda: brush_size_change(5))

#paste image
w.create_image(1, 1, image = image, anchor = NW)

w.grid(row=2,column=0,columnspan=7,
       padx=5,pady=5,sticky=E+W+S+N)
w.columnconfigure(6, weight=1)
w.rowconfigure(2,weight=1)

#paste buttons
red_btn.grid(row=0,column=2)
five_btn.grid(row=1,column=2)



#otobrajenie
root.mainloop()

#io.imshow(img)
#io.show()

So far, there is such a code, without a trackbar. According to the comments, I think it will be clear that this code makes it possible to draw, choose the color and size of the brush. There is a problem when I try to create a TrackBar, namely: I create it through the cv2.createTrackBar command, but for some reason it does not work

Convert your image to HSV/HSL colourspace, and use the slider to progressively reduce saturation. When you get to zero, the image will be greyscale.

Add your code and representative image to your question for better assistance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM