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()
Copy and Paste Files - Command
100% complete noob trying to copy/paste code
Copying and pasting code directly into the Python interpreter - Stack Overflow
Complete noob, need copy/paste code
Videos
[SOLVED]
Yo. Please use the most dumbed down language, because I've literally never done this stuff, Idk how python works. And at this moment, I'm not exactly trying to. Im just trying to copy/paate a command. I am simply trying to copy/paste code from a guide and it keeps saying I have a syntax error. I think I figured out that you dont actually type the $, but I dont even know if that's correct. Either way, I continue to get a syntax error with the arrow pointing to seemingly random letters.
Please help.
Edit: This is the simple command given to download a HTTP library for Python called "Requests":
$ python -m pip install requests
Edit2: Thanks to social_nerdtastic for answering. I just had to use cmd. I had a feeling it was something simple and fundamental that I just didn't know
You can usually easily and safely do copy-pasting with IPython, through the commands %cpaste (manually ending code with --) and %paste (execute code immediately). This is very handy for testing code that you copy from web pages, for instance, or from your editor: these commands even strip leading prompts (like In[1] and ...) for you.
IPython also has a %run command that runs a program and leaves you in a Python shell with all the variables that were defined in the program, so that you can play with them.
In order to get help on these functions: %cpaste?, etc.
You can simply convert all tabs to spaces and remove ALL empty lines. So you will be able to paste any code to python console (e.g.: python2.6)