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 Overflow
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ paste
Share Python Code โ€“ a runnable Pastebin
A free Python-oriented pastebin service for sharing Python code snippets with anyone
Discussions

Is there a website where I can easily copy and paste python programs?
The problem I have with GITHUB is the file download, because I can't figure out how to get the code from the files. maybe learn how to do it? More on reddit.com
๐ŸŒ r/learnpython
4
0
May 27, 2022
image - "copy and paste" python program - Stack Overflow
I'm currently trying to write a program that reads a GIF file, displays its image on the screen, then allows the user to select a rectangular portion of the image to "copy and paste", AKA to then c... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Free recommended code sharing sites?
Iโ€™m fairly new to Python, I still have not finished my first Python tutorial. Sometimes itโ€™s easier to share longer code (50+ lines) on a code sharing website. Besides pastebin.com, what are recommended code sharing sites when asking questions on a forum about Python? More on discuss.python.org
๐ŸŒ discuss.python.org
7
0
February 16, 2024
What are the best sites to practice programming and Python?
Skip to main content More on reddit.com
๐ŸŒ r/learnpython
April 18, 2024
People also ask

Is the Python code generator free to use?
Yes. You can generate Python code for free without creating an account for up to 2 generations per day. For more generations and higher limits, sign in for free โ€” every account gets 5 free credits with support for up to 25,000 characters in your prompt per generation.
๐ŸŒ
codeconvert.ai
codeconvert.ai โ€บ home โ€บ ai code generator โ€บ python code generator
Free Python Code Generator - AI-Powered | CodeConvert AI
Do I need to sign up to use the Python code generator?
No. You can use the free Python code generator without signing up or creating an account for up to 2 generations per day. Just describe what you need, and click Generate. Sign in for free to get 5 free credits with higher limits.
๐ŸŒ
codeconvert.ai
codeconvert.ai โ€บ home โ€บ ai code generator โ€บ python code generator
Free Python Code Generator - AI-Powered | CodeConvert AI
What kind of Python code can this tool generate?
This tool can generate a wide range of Python code, from simple functions, algorithms, and data structures to complete programs with classes, error handling, and file I/O. It handles common Python patterns and idioms.
๐ŸŒ
codeconvert.ai
codeconvert.ai โ€บ home โ€บ ai code generator โ€บ python code generator
Free Python Code Generator - AI-Powered | CodeConvert AI
๐ŸŒ
CopyAssignment
copyassignment.com โ€บ python-games-code-copy-and-paste
Python Games Code | Copy and Paste โ€“ CopyAssignment
August 23, 2022 - Hello friends, today, we will see all the Python games code which you can easily copy and paste into your system.
๐ŸŒ
OneCompiler
onecompiler.com โ€บ python โ€บ 3wsj7ajyg
Python Online Compiler & Interpreter
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast.
Top answer
1 of 2
2

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])
2 of 2
0
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()
Find elsewhere
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ SimplePrograms
SimplePrograms - Python Wiki
The examples below will increase in number of lines of code and difficulty: ยท print ('Hello, world!')
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ web development โ€บ python
Explore These 20 Cool Python Scripts for Fun and Productivity! - Java Code Geeks
January 17, 2024 - Explore a collection of versatile Cool Python scripts, from web scraping to machine learning and web development.
๐ŸŒ
CodeConvert AI
codeconvert.ai โ€บ home โ€บ ai code generator โ€บ python code generator
Free Python Code Generator - AI-Powered | CodeConvert AI
Free AI Code Generator for writing Python code. Describe what you need, get working Python code instantly. No signup required.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Free recommended code sharing sites? - Python Help - Discussions on Python.org
February 16, 2024 - Iโ€™m fairly new to Python, I still have not finished my first Python tutorial. Sometimes itโ€™s easier to share longer code (50+ lines) on a code sharing website. Besides pastebin.com, what are recommended code sharing sites when asking questions on a forum about Python?
๐ŸŒ
Medium
medium.com โ€บ @zhongwei2049 โ€บ 8-delightful-python-scripts-to-brighten-your-day-5908c1761905
8 Delightful Python Scripts to Brighten Your Day | by Zhongwei | Medium
July 30, 2023 - 8 Delightful Python Scripts to Brighten Your Day As programmers, we regularly tackle coding challenges that demand sophisticated solutions beyond basic Python syntax. Copy-pasting snippets from โ€ฆ
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists_copy.asp
Python - Copy Lists
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
๐ŸŒ
Backlinkworks
blogs.backlinkworks.com โ€บ home โ€บ 10 insanely cool python codes youโ€™ll want to copy and paste right away!
10 Insanely Cool Python Codes You'll Want to Copy and Paste Right Away! - Topics on SEO & Backlinks
February 6, 2024 - Python is an incredibly versatile programming language that is widely used in various fields, including web development, data analysis, artificial intelligence, and more. With its simple and easy-to-read syntax, Python has become a favorite among both new and experienced developers. In this article, weโ€™ll explore 10 insanely cool Python codes that youโ€™ll want to copy and paste right away to enhance your projects.
๐ŸŒ
Online Python
online-python.com
Online Python - IDE, Editor, Compiler, Interpreter
Build, run, and share Python code online for free with the help of online-integrated python's development environment (IDE). It is one of the most efficient, dependable, and potent online compilers for the Python programming language. It is not necessary for you to bother about establishing a Python environment in your local.
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-14918.html
Copy & Paste Web Page
Is there a way to copy all text on a webpage and paste in a variable in Python 3.62 ? I am looking for a solution that does not use SendKeys("^a",0)/SendKeys("^c",0)
๐ŸŒ
GitHub
gist.github.com โ€บ sanchitgangwar โ€บ 2158089
Snakes Game using Python ยท GitHub
Snakes Game using Python. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
Pastecode
pastecode.io
Paste Code Online - PasteCode.io
PasteCode.io is a code pasting & sharing website, only intended for the collaboration of developers. You can paste code and share code online for free.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_examples.asp
Python Examples
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.