如何解决嗯简单地解决了问题
我是 Python 的初学者,我现在正在尝试自己
解决方法
原因很明显,您看到的 img
对象仅在 open()
中定义/更新,因此图像将始终引用所选的原始图像而不是新编辑的图像,因此显示更改将其存储在一个新变量中,然后将其全球化。
另请注意,每次调用该函数时都会创建一个新的画布图像,这效率不高,因此在函数内部使用 itemconfig()
方法制作单个画布图像,然后每次更新它。
def open():
global img,tk_img
filename = fd.askopenfilename()
img = Image.open(filename)
tk_img = ImageTk.PhotoImage(img)
canvas.itemconfig('img',image=tk_img) # Update image
def image_rotate():
global tk_img,angle,img
angle += 45 # More pythonic to always use += rather than a = a + 10
img = img.rotate(angle)
tk_img = ImageTk.PhotoImage(img)
canvas.itemconfig('img',image=tk_img) # Update image
angle -= 45
def image_blur():
global tk_img,img
img = img.filter(ImageFilter.BLUR)
tk_img = ImageTk.PhotoImage(img)
canvas.itemconfig('img',image=tk_img) # Update image
canvas = tk.Canvas(window,width=500,height=500)
canvas.create_image(250,250,tag='img') # Create initial canvas object
canvas.pack()
另请查看我如何格式化您的代码以使其看起来更接近,请关注 PEP 8 -- Style Guide for Python Code 了解更多信息。
,您可以使用 opencv 包模糊图像
import cv2
img = cv2.imread('imagepath')
blurImg = cv2.blur(img,(10,10))
cv2.imshow('blurred image',blurImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
或者你可以查看opencv的文档来模糊图像。 你也可以使用 opencv 旋转图像
# importing cv2
import cv2
src = cv2.imread(path)
window_name = 'Image'
image = cv2.rotate(src,cv2.ROTATE_90_COUNTERCLOCKWISE)
# Displaying the image
cv2.imshow(window_name,image)
cv2.waitKey(0)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。