[英]How to show image with lines plotted over it on tkinter canvas window?
This program reads an image which is in location C:/Square.png and lines are plotted over it.该程序读取位置 C:/Square.png 中的图像并在其上绘制线条。 The plot title is also defined.还定义了情节标题。 I want to show this whole image in tkinter window using canvas.我想使用画布在 tkinter 窗口中显示整个图像。 How do I do it?我该怎么做?
This is the image.这是图像。 The name has to be changed and we can run the code.名称必须更改,我们才能运行代码。 https://imgur.com/RkV02yY https://imgur.com/RkV02yY
import math
import matplotlib.pyplot as plt
def plot_output(opt_w, opt_h, n_x, n_y):
y_start, y_end = 100, 425
x_start, x_end = 25, 400
img = plt.imread("C:/Square.png") #Please change the location
fig, ax = plt.subplots(figsize=(10, 10))
plt.axis('off')
ax.imshow(img)
x_interval = (x_end - x_start)/n_x*2
h_x = range(x_start, x_end, 5)
for i in range(0,int(n_y)):
if i != 0:
ax.plot(h_x, [y_start + (y_end-y_start)/n_y*i]*len(h_x), '--', linewidth=5, color='firebrick')
plt.title(str(int(n_x*n_y)) + ' ABCD\n'+'TYUI:'+str(opt_w)+', Yummy:'+str(opt_h))
def get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a):
x = 1
if max_w < PL:
x = math.ceil(PL / max_w)
cost_rest = cost_m * PL * PH * (max_t + min_t) / 2 + cost_a * PH * x
cost_y = float("inf")
y = None
if min_h == 0:
min_h = 1
for i in range(math.ceil(PH / max_h), math.floor(PH / min_h)+1):
tmp_cost = cost_m * PL * PH * (max_t - min_t) / 2 / i + cost_a * PL * i
if tmp_cost < cost_y:
cost_y = tmp_cost
y = i
opt_w, opt_h, opt_cost = PL/x, PH/y, cost_rest + cost_y
plot_output(opt_w, opt_h, x, y)
return opt_w, opt_h, opt_cost
PL=30
PH=10
min_t=0.1
max_t=0.3
cost_m=0.1
cost_a=0.1
min_w=0.5
min_h=0.5
max_w=4
max_h=3
get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a)
It creates image, lines and text (title) on canvas它在画布上创建图像、线条和文本(标题)
import math
import tkinter as tk
from PIL import ImageTk
def plot_output(opt_w, opt_h, n_x, n_y):
global img # solution for PhotoImage bug
y_start, y_end = 100, 425
x_start, x_end = 25, 400
margin_top = 35 # place for title
# --- image ---
#img = ImageTk.PhotoImage(file="image.jpg") #Please change the location
canvas.create_image(0, margin_top, image=img, anchor='nw')
canvas['width'] = img.width()
canvas['height'] = img.height() + margin_top
# --- lines ---
y_step = (y_end-y_start)/n_y
for i in range(1, n_y):
y = y_start + y_step*i
y += margin_top # place for title
canvas.create_line(x_start, y, x_end, y, fill='red', width=7, dash=(25, 10))
#for y in range(y_start+y_step, y_end-1, y_step):
# y += margin_top # place for title
# canvas.create_line(x_start, y, x_end, y, fill='red', width=7, dash=(25, 10))
# --- title ---
text = '{} ABCD\nTYUI:{}, Yummy:{}'.format(int(n_x*n_y), opt_w, opt_h)
x = img.width()//2
y = margin_top # bottom of title because `create_text` uses `anchor='s'`
canvas.create_text(x, y, text=text, anchor='s', justify='center')
def get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a):
x = 1
if max_w < PL:
x = math.ceil(PL / max_w)
cost_rest = cost_m * PL * PH * (max_t + min_t) / 2 + cost_a * PH * x
cost_y = float("inf")
y = None
if min_h == 0:
min_h = 1
for i in range(math.ceil(PH / max_h), math.floor(PH / min_h)+1):
tmp_cost = cost_m * PL * PH * (max_t - min_t) / 2 / i + cost_a * PL * i
if tmp_cost < cost_y:
cost_y = tmp_cost
y = i
opt_w, opt_h, opt_cost = PL/x, PH/y, cost_rest + cost_y
plot_output(opt_w, opt_h, x, y)
return opt_w, opt_h, opt_cost
# --- main ---
PL = 30
PH = 10
min_t = 0.1
max_t = 0.3
cost_m = 0.1
cost_a = 0.1
min_w = 0.5
min_h = 0.5
max_w = 4
max_h = 3
root = tk.Tk()
canvas = tk.Canvas(root, width=1000, height=1000)
canvas.pack()
get_get(min_w, min_h, max_w, max_h, PL, PH, min_t, max_t, cost_m, cost_a)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.