简体   繁体   English

如何使用 python tkinter canvas 获得更好质量的图像?

[英]How can I get a better quality image with python tkinter canvas?

In the python tkinter library, I am trying to make a simple program that is supposed to allow you to take a screenshot and then blur out certain details.在 python tkinter 库中,我试图制作一个简单的程序,它应该允许您截取屏幕截图然后模糊某些细节。 However the quality of the final image does not look desirable.然而,最终图像的质量看起来并不理想。 it's all pixelated都是像素化的

Also, if you are wondering how to blur out parts of your image, all you got to do is click around.此外,如果您想知道如何模糊图像的某些部分,您所要做的就是四处点击。

I also do plan on making threading for saving of images, since there is freezing of the entire graphical user interface.我还计划制作线程以保存图像,因为整个图形用户界面都冻结了。

from tkinter import *
import pyautogui
import threading
import keyboard
import time
from PIL import ImageTk, Image, EpsImagePlugin

class screenshotwindow:
    def __init__(self, master):
        self.master = master
        self.screenshot = None
        self.cords = [None,None]
        self.clicks = 0
        self.master.focus_force()
        self.buttons()
        self.blurtwindow()
    def buttons(self):
        self.clear = Button(master=self.master, command=self.clear, text="Clear all blurring rectangles.")
        self.imgsave = Button(master=self.master, command=self.save, text="Save")
        self.clear.pack(side=BOTTOM)
        self.imgsave.pack(side=BOTTOM)
    def save(self):
        self.canv.postscript(file="test.jpg")
        EpsImagePlugin.gs_windows_binary = r"C:\Program Files\gs\gs9.53.3\bin\gswin64c"
        img = Image.open("test.jpg")
        img.format = "PNG"
        resize = img.resize((int(self.screenshot.size[0] * 1.3),int(self.screenshot.size[1] * 1.3)), Image.LANCZOS)
        resize.save("test.png", "png")
    def clear(self):
        self.canv.delete("all")
        self.canv.create_image(0, 0, image=self.image, anchor=NW)
    def cord(self,event):
        self.clicks += 1
        if self.clicks == 1:
            self.cords[0] = (event.x, event.y)
        if self.clicks == 2:
            self.clicks = 0
            self.cords[1] = (event.x, event.y)
            self.t = self.canv.create_rectangle(self.cords[0][0],self.cords[0][1], self.cords[1][0], self.cords[1][1], fill="black")
    def blurtwindow(self):
        self.screenshot = pyautogui.screenshot()
        self.screenshot.save("screenshot.png")
        self.canv = Canvas(self.master, width=(int(self.screenshot.size[0] / 1.3)), height=int((self.screenshot.size[1] / 1.3)), bg="white")
        self.canv.pack(fill=BOTH)
        self.resized_image = self.screenshot.resize((int(self.screenshot.size[0] / 1.3),int(self.screenshot.size[1] / 1.3)), Image.LANCZOS)
        self.image = ImageTk.PhotoImage(master=self.canv, image=self.resized_image)
        self.canv.create_image(0,0,image=self.image,anchor=NW)
        self.canv.bind("<Button-1>", self.cord)

root = Tk()
screenshotwindow(root)
root2.mainloop()

As I understand it you are doing the following:据我了解,您正在执行以下操作:

  1. Take a screenshot截图
  2. resize the photo for Tk interface.调整 Tk 界面的照片大小。
  3. determine blurring确定模糊
  4. combine blurring with photo将模糊与照片相结合
  5. repeat重复

It appears you are iteratively degrading the quality by using screenshots.看来您正在使用屏幕截图反复降低质量。 You would probably get better performance by making a specific modification to the original image file instead of constantly processing the image through a screenshot/resize.通过对原始图像文件进行特定修改而不是通过屏幕截图/调整大小不断处理图像,您可能会获得更好的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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