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()
Everytime I copy and paste more than one line of code, python stops responding.
Python Scripts Copy Paste makes some error
How to paste Python code in Stack Overflow without DIY - Meta Stack Overflow
Massive Python Example Script - General and Gameplay Programming | GameDev.net Forums - GameDev.net
Videos
I'm trying to learn Python by taking some codes for games like Snake. I want to copy and paste the code into Python, change some things up a bit in order to learn how everything works, and then begin making my own sets of code. But everytime I copy and paste more than one line of code into, Python launches Snake, and stops responding and I'm forced to cloe it. Is there anyway around this?
Here is a link to a video showing what happens: https://youtu.be/LsoJG4LHY4A