我有一個帶有空白文本框(通常由手工填寫)的表單(以.png格式)。我想填充文本框。爲此,我使用tkinter在屏幕上顯示窗體,然後使用鼠標(使用箭頭鍵進行更精細的定位)來獲取框的像素座標,然後使用PIL將文本寫入到該窗體中框。下面是一個工作示例。調整tkinter畫布和PIL圖像上的像素座標(Python)
我的主要問題是我努力對準tkinter畫布中的像素座標和PIL圖像中的像素座標。
一些額外的背景。圖像高分辨率,大約4961×7016像素。我的屏幕分辨率是1920 x 1080.我在寫文本時遇到了問題,我需要這樣寫,並且如果我縮放圖像以使其完全適合我的屏幕,則會獲得更大的成功。我只能假設這是因爲我屏幕像素與圖片像素混淆(當我將圖像放入屏幕以對齊這兩個圖像時解決了這一問題 - 但瞭解這裏的差異以及如何在不縮放的情況下執行此任務將成爲最也有幫助)。
但我也有麻煩tkinter畫布上的像素座標與PIL圖片調和。例如,下面的代碼被設計爲將(x,y)像素座標和頁面相對性(頁面上的x%,頁面上的y%)寫入框中(其原因是因爲它是另一個過程)。一個例子是:(346,481)>> {49.856,51.018}
但是如果(使用0.14的縮放因子)我點擊圖片底部的很低,我得到(209,986)>> {30.115,-0.407}。相對性應該介於0和100%之間,所以不應該是負數,我不能在PIL生成的.png文件中看到這個。
如果我使用0.125的比例因子,我可以將文本寫入tkinter畫布框中,但文本顯示比保存到驅動器的PIL .png文件低很多(即在框外) 。所以這兩個系統之間顯然不起作用。
如何協調PIL和tkinter像素座標?另外,我有四個獨立的函數來處理更好的關鍵調整。理想的情況是這些將是一個函數,但我無法讓箭頭按鈕(ETC)到如果elif的塊中工作(例如,我嘗試這樣做,左,右等一些衍生品)
def mouseMovement(event):
moveSpeed = 1
try:
int(event.char)
moveSpeed = max(1, int(event.char)*5)
return True
except ValueError:
return False
x, y = pyautogui.position()
if event.char == '<Left>':
pyautogui.moveTo(x-moveSpeed, y)
elif event.char == '<Right>':
pyautogui.moveTo(x+moveSpeed, y)
root.bind('<Key>' , mouseMovement)
任何幫助不勝感激!
幾乎工作下面的例子:
from tkinter import *
from PIL import Image, ImageDraw, ImageFont, ImageTk
import pyautogui
# Form
formName = '2013+ MCS4'
# PIL image'
formImage = Image.open(formName+'.png')
wForm, hForm = formImage.size
scale = 0.14
formImage = formImage.resize((int(scale*wForm), int(scale*hForm)), Image.ANTIALIAS)
draw = ImageDraw.Draw(formImage)
font = ImageFont.truetype('arial.ttf', 10)
textColor = (255, 40, 40)
# tkinter canvas
def colorConversion(RGB):
def hexadecimalScale(RGB):
hexadecimalSystem = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F')
return str(hexadecimalSystem[RGB//16]) + str(hexadecimalSystem[RGB%16])
return '#' + hexadecimalScale(RGB[0]) + hexadecimalScale(RGB[1]) + hexadecimalScale(RGB[2])
fontCanvas = 'arial 7'
textColorCanvas = colorConversion(textColor)
# generate canvas
if __name__ == '__main__':
root = Tk()
# set up tkinter canvas with scrollbars
frame = Frame(root, bd=2, relief=SUNKEN)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
xscroll = Scrollbar(frame, orient=HORIZONTAL)
xscroll.grid(row=1, column=0, sticky=E+W)
yscroll = Scrollbar(frame)
yscroll.grid(row=0, column=1, sticky=N+S)
canvas = Canvas(frame, width=int(scale*wForm), height=int(scale*hForm), bd=0, xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
xscroll.config(command=canvas.xview)
yscroll.config(command=canvas.yview)
frame.pack(fill=BOTH,expand=1)
# add image
#img = PhotoImage(file=formName+'.png')
img = ImageTk.PhotoImage(formImage)
canvas.create_image(0,0,image=img,anchor="nw")
canvas.config(scrollregion=canvas.bbox(ALL))
wForm = img.width()
hForm = img.height()
# finer mouse movements
moveSpeed = 1
def setMoveSpeed(event):
global moveSpeed
try:
int(event.char)
moveSpeed = max(1, int(event.char)*5)
return moveSpeed
except ValueError:
return False
def moveMouseLeft(event):
x, y = pyautogui.position()
pyautogui.moveTo(x-moveSpeed, y)
def moveMouseRight(event):
x, y = pyautogui.position()
pyautogui.moveTo(x+moveSpeed, y)
def moveMouseUp(event):
x, y = pyautogui.position()
pyautogui.moveTo(x, y-moveSpeed)
def moveMouseDown(event):
x, y = pyautogui.position()
pyautogui.moveTo(x, y+moveSpeed)
root.bind('<Key>' , setMoveSpeed)
root.bind('<Left>' , moveMouseLeft)
root.bind('<Right>', moveMouseRight)
root.bind('<Up>' , moveMouseUp)
root.bind('<Down>' , moveMouseDown)
# print coordinates
def printCoordinates(event):
x = event.x # minor adjustments to correct for differences in tkinter vs PIL methods (investigate further)
y = event.y # minor adjustments to correct for differences in tkinter vs PIL methods (investigate further)
canvas.create_text(x, y-5, fill= textColorCanvas, font= fontCanvas, anchor= 'sw',
text= '{'+str(round(x/wForm*100,3))+', '+str(round((1-y/hForm)*100,3))+'}')
draw.text((x, y-5), '{'+str(round(x/wForm*100,3))+', '+str(round((1-y/hForm)*100,3))+'}' , fill=textColor, font=font)
print('('+str(x)+', '+str(y)+') >> {'+str(round(x/wForm*100,3))+', '+str(round((1-y/hForm)*100,3))+'}')
root.bind('<Return>', printCoordinates)
root.mainloop()
formImage.save('coordinates - '+formName+'.png')