The main problem with your code is that you create a new PhotoImage for each pixel! Instead, create the PhotoImage once and just add the pixels in the double-for-loop.
def box(event):
yaxis(event)
canvas.create_rectangle(x1, y1, x2, y2)
picture = PhotoImage(width=(x2-x1), height=(y2-y1))
for x in range(x1, x2):
for y in range(y1, y2):
r, g, b = photo.get(x, y)
picture.put("#%02x%02x%02x" % (r, g, b), (x-x1, y-y1))
picture.write('new_image.gif', format='gif')
Also, the line tuple(map(int, value.split(" "))) in your getRGB function is wrong, as value is already the tuple you want to create, not a string.1) As you can see, I just 'inlined' that part directly into the box function. Another problem was that you wrote the copied pixels to x and y, but you have to write them to x-x1 and y-y1 instead.
Update 1: 1) It seems like the return value of PhotoImage.get depends on the version of Python/Tkinter you are using. In some versions, it returns a tuple, like (41, 68, 151), and in others, a string, like u'41 68 151'.
Update 2: As pointed out by @Oblivion, you can in fact just use the from_coords parameter of PhotoImage.write to specify the region of the picture to be saved to file. With this, the box function can be simplified as
def box(event):
yaxis(event)
canvas.create_rectangle(x1, y1, x2, y2)
photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
Answer from tobias_k on Stack OverflowThe main problem with your code is that you create a new PhotoImage for each pixel! Instead, create the PhotoImage once and just add the pixels in the double-for-loop.
def box(event):
yaxis(event)
canvas.create_rectangle(x1, y1, x2, y2)
picture = PhotoImage(width=(x2-x1), height=(y2-y1))
for x in range(x1, x2):
for y in range(y1, y2):
r, g, b = photo.get(x, y)
picture.put("#%02x%02x%02x" % (r, g, b), (x-x1, y-y1))
picture.write('new_image.gif', format='gif')
Also, the line tuple(map(int, value.split(" "))) in your getRGB function is wrong, as value is already the tuple you want to create, not a string.1) As you can see, I just 'inlined' that part directly into the box function. Another problem was that you wrote the copied pixels to x and y, but you have to write them to x-x1 and y-y1 instead.
Update 1: 1) It seems like the return value of PhotoImage.get depends on the version of Python/Tkinter you are using. In some versions, it returns a tuple, like (41, 68, 151), and in others, a string, like u'41 68 151'.
Update 2: As pointed out by @Oblivion, you can in fact just use the from_coords parameter of PhotoImage.write to specify the region of the picture to be saved to file. With this, the box function can be simplified as
def box(event):
yaxis(event)
canvas.create_rectangle(x1, y1, x2, y2)
photo.write('new_image.gif', format='gif', from_coords=[x1, y1, x2, y2])
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):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
canvas.create_rectangle(x1, y1, x2, y2)
new_photo = copy_photo(photo, x1, y1, x2, y2)
new_photo.write('new_image.gif', format='gif')
def copy_photo(photo, x1, y1, x2, y2):
new_photo = PhotoImage(width=photo.width(), height=photo.height())
for x in range(photo.width()):
for y in range(photo.height()):
if x1 <= x < x2 and y1 <= y < y2:
r,g,b = getRGB(photo, x, y)
new_photo.put("#%02x%02x%02x" % (r,g,b), (x,y))
else:
new_photo.put(photo.get(x, y), (x,y))
return new_photo
def getRGB(photo, x, y):
value = photo.get(x, y)
return tuple(map(int, value.split(" ")))
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)
enter code here
canvas.mainloop()
Share the code you're most proud of!
Cool scripts that help you with your daily life
Copy and Paste Files - Command
What are some Python scripts have u made for fun and daily life?
Videos
Well all know that sense of awesome when you write a neat, concise chunk of code that does something really cool. Take the code in the sidebar for example:
def fibonacci():
a, b = 0, 1
while 1:
yield a
a, b = b, a + bIt's just a thought, but if you have a moment why not share your favorite snippet of code, along with a brief explanation? It must be under 10 lines, but anything else goes. Post away!
What are the most interesting scripts you have made for fun or to automate daily tasks?