Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:58:06+00:00 2026-05-26T03:58:06+00:00

I attempted today to create a Python code to invert the colors of a

  • 0

I attempted today to create a Python code to invert the colors of a .gif image using the Tkinter library. The code works and does exactly what I expected, but it takes about 50 seconds to run on a 3.4ghz processor. I’m having trouble seeing what I could change to optimize this. Basically, I loop through every pixel in the image , grab the color values, convert them to a list of integers (in order to manipulate them mathematically), invert each color value (new color value = 255 – old color value), convert them back to a string so that PhotoImage’s “put” method can process them and rewrite the image, and finally display the inverted image. I can’t think of what to change. I mean, looping through every single pixel must be a part of the slowness, but isn’t this process completely necessary?

from Tkinter import *
import tkMessageBox

class GUIFramework(Frame):
    def __init__(self, master=None):

    Frame.__init__(self, master)

    self.grid(padx=0, pady=0)
    self.btnDisplay = Button(self, text="Display!", command=self.Display)
    self.btnDisplay.grid(row=0, column=0)

    def Display(self):
        a = ''
        b = []
        self.imageDisplay = PhotoImage(file='C:\image.gif')
        for x in range(0, self.imageDisplay.width()):
            for y in range(0, self.imageDisplay.height()):
                value = self.imageDisplay.get(x,y)
                for i in value:
                    try:
                        c = int(i)
                        a += (i)
                    except:
                        b.append(int(a))
                        a = ''
                b.append(int(a))
                for i in range(0,3):
                    b[i] = (255 - b[i])
                self.imageDisplay.put('#%02x%02x%02x' %tuple(b), (x,y))
                b = []
                a = ''
        c = Canvas(self, width=700, height=700); c.pack()
        c.grid(padx=0,pady=0, column=0, row=0)
        c.create_image(0,0, image = self.imageDisplay, anchor = NW)

if __name__ == "__main__":
    guiFrame = GUIFramework()
    guiFrame.mainloop()

Thanks in advance for your help.
-Seth

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:58:07+00:00Added an answer on May 26, 2026 at 3:58 am

    Try this:

    def Display(self):
        self.imageDisplay = PhotoImage(file='C:\image.gif')
        for x in xrange(0, self.imageDisplay.width()):
            for y in xrange(0, self.imageDisplay.height()):
                raw = self.imageDisplay.get(x, y)
                rgb = tuple(255 - int(component) for component in raw.split())
                self.imageDisplay.put('#%02x%02x%02x' % rgb, (x, y))
        c = Canvas(self, width=700, height=700); c.pack()
        c.grid(padx=0,pady=0, column=0, row=0)
        c.create_image(0,0, image = self.imageDisplay, anchor = NW)
    

    Edit:
    New version (faster and with optimizations from Justin Peel).

    def Display(self):
        self.imageDisplay = PhotoImage(file='C:\image.gif')
    
        wrange = xrange(0, self.imageDisplay.width())
        hrange = xrange(0, self.imageDisplay.height())
        get = self.imageDisplay.get
        put = self.imageDisplay.put
    
        def convert_pixel(raw):
            return ('#%02x%02x%02x' %
                tuple(255 - int(component) for component in raw.split(' ')))
    
        for y in hrange:
            put('{' + ' '.join(convert_pixel(get(x, y)) for x in wrange) + '}', (0, y))
    
        c = Canvas(self, width=700, height=700);
        c.pack()
        c.grid(padx=0, pady=0, column=0, row=0)
        c.create_image(0, 0, image=self.imageDisplay, anchor=NW)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I did a math problem today and first attempted it in Python but after
I recently started learning 0MQ . Earlier today, I ran into a blog, Python
I wrote this code today, just out of experimentation, and I'm trying to figure
Today, tiny_tds suddenly does not accept more than one execute and returns: C:\>ruby test_use.rb
I attempted to open a solution today at my new job and it said
any one getting this error today? The site's security certificate has expired! You attempted
Okay this is a revised question from earlier today, I have included code to
Today I saw C# code that creates static dictionary and initializes it: public static
I am running OS X 10.6 and using the SVN Applications Versions, and today
Attempted translation of the above question from non-native English to English: This is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.