【发布时间】:2015-07-08 05:09:18
【问题描述】:
我目前正在尝试编写一个程序来读取 GIF 文件,在屏幕上显示其图像,然后允许用户选择图像的矩形部分进行“复制和粘贴”,也就是复制图像在矩形内,然后将结果另存为新的 GIF 文件。我已经走了很远,但是每次我想我已经弄清楚时,似乎都会弹出一个新错误!
# CopyAndPaste.py
# This program is designed to read a GIF file, display its image on the screen, allows the user to
# select a rectangular portion of the image to copy and paste, and then save the result as a new GIF file
import tkinter
from tkinter import *
import base64
root = Tk()
def action(canvas):
canvas.bind("<Button-1>", xaxis)
canvas.bind("<ButtonRelease-1>", yaxis)
canvas.bind("<ButtonRelease-1>", box)
def xaxis(event):
global x1, y1
x1, y1 = (event.x - 1), (event.y - 1)
print (x1, y1)
def yaxis(event):
global x2, y2
x2, y2 = (event.x + 1), (event.y + 1)
print (x2, y2)
def box(event):
photo = PhotoImage(file="picture.gif")
yaxis(event)
canvas.create_rectangle(x1,y1,x2,y2)
for x in range(x1, x2):
for y in range(y1, y2):
r,g,b = getRGB(photo, x, y)
newImage(r, g, b, x, y)
def getRGB(photo, x, y):
value = photo.get(x, y)
return tuple(map(int, value.split(" ")))
def newImage(r, g, b, x, y):
picture = PhotoImage(width=x, height=y)
picture.put("#%02x%02x%02x" % (r,g,b), (x,y))
picture.write('new_image.gif', format='gif')
canvas = Canvas(width=500, height=250)
canvas.pack(expand=YES, fill=BOTH)
photo = PhotoImage(file="picture.gif")
canvas.create_image(0, 0, image=photo, anchor=NW)
canvas.config(cursor='cross')
action(canvas)
root.mainloop()
【问题讨论】:
-
您遇到的错误是什么?
-
请尝试拆分您的问题并获得更详细的信息。目前您要求的是一个完整的工具,它不在本页的主题范围内,让人们难以帮助
标签: python image canvas tkinter gif