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 9088383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:53:52+00:00 2026-06-16T21:53:52+00:00

I need to write a function that get an image and return a mirror

  • 0

I need to write a function that get an image and return a mirror image (the Y coordinate never change), without using any mirror or flip
module from PIL.
I have to write it myself.
I’m getting an error msg:

IndexError: image index out of range

what am I missing here?

thank you.

from os.path import exists
import tkMessageBox, tkFileDialog
from PIL import Image, ImageTk, ImageFilter

def flip(im):
    '''Flips a picutre horizontally, and returns a new image that is a mirror view of the original'''
    org=Image.open(im)
    new=Image.new("RGB",org.size)   
    for x in range(org.size[0]):
        a=org.size[0]
        for y in range(org.size[1]):
            pixel=org.getpixel((x,y))
            new.putpixel((a,y),pixel)
            a-=1
    new.save("new.bmp","bmp")
  • 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-06-16T21:53:53+00:00Added an answer on June 16, 2026 at 9:53 pm

    You’re mixing up 0-based and 1-based indexing. x ranges from 0 to org.size[0]-1. But a ranges from org.size[0] to 1, because you start with a=org.size[0] and use that, without first subtracting.

    Most likely the error comes from somewhere inside this line:

    new.putpixel((a,y),pixel)
    

    … on your very first call, when you try to write pixel (280, 0) in an image that only runs from (0-279, 0-279).

    So, instead of this:

    for x in range(org.size[0]):
        a=org.size[0]
        for y in range(org.size[1]):
            pixel=org.getpixel((x,y))
            new.putpixel((a,y),pixel)
            a-=1
    

    Do this:

    for x in range(org.size[0]):
        a=org.size[0]-1
        for y in range(org.size[1]):
            pixel=org.getpixel((x,y))
            new.putpixel((a,y),pixel)
            a-=1
    

    But once you fix this, you’re going to run into another problem. You set a to org.size[0] every time through the outer loop, instead of just the first time. And then you decrement a each time through the inner loop, instead of the outer loop. So, you’re going to end up copying each line in the original image to the diagonal running from (279,0) to (0,279).

    So, you need to do this:

    a=org.size[0]-1
    for x in range(org.size[0]):
        for y in range(org.size[1]):
            pixel=org.getpixel((x,y))
            new.putpixel((a,y),pixel)
        a-=1
    

    This kind of thing is exactly why you should try to avoid modifying indices manually like this. You never get it right on your first try. On your third try, you get something that looks right, but crashes as soon as you try the first image that wasn’t in your test suite. It’s better to calculate the values instead of counting them down. For example:

    for x in range(org.size[0]):
        flipped_x = org.size[0] - x - 1
        for y in range(org.size[1]):
            pixel=org.getpixel((x,y))
            new.putpixel((flipped_x,y),pixel)
    

    While we’re at it, if you can require/depend on PIL 1.1.6 or later, using the array returned by Image.load() is significantly simpler, and much more efficient, and often easier to debug:

    orgpixels, newpixels = org.load(), new.load()
    for x in range(org.size[0]):
        flipped_x = org.size[0] - x - 1
        for y in range(org.size[1]):
            pixel=orgpixels[x, y]
            newpixels[flipped_x, y] = pixel
    

    If you can’t rely on 1.1.6, you can use getdata to get a iterable sequence of pixels, use that to generate a new list or other sequence (which means you can use a list comprehension, map, even feed them into numpy via, e.g., np.array(org.getdata()).reshape(org.size)), and then use putdata to create the new image from the result.

    Of course getdata and putdata deal with a 1D sequence, while you want to treat it as a 2D sequence. Fortunately, the grouper function in the itertools docs—which you can copy and paste, or just pip install more-itertool—is usually exactly what you want:

    orgrows = more_itertools.grouper(org.size[0], org.getdata())
    newrows = [list(reversed(row)) for row in orgrows]
    new.putdata(newrows)
    

    One thing to watch out for is that Image.open(im) might not necessarily return you an image in RGB mode. If you just copy pixels from, say, an XYZ or P image to an RGB, you’ll end up with discolored garbage or just the red channel, respectively. You may want to print org.mode, and possibly print org.pixel((0, 0)) to make sure it actually has 3 channels (and they look like RGB)`.

    The easiest way around that is to convert org before you do anything else:

    org=Image.open(im).convert('RGB')
    

    Of course some formats either don’t have direct conversions or require an explicit matrix or color palette. If you get a ValueError, you’ll have to read up on converting your input type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write, in JavaScript (I am using jQuery), a function that is
I need to write a function that can take an if statement at runtime
I need to write a function that is passed an instance of a CharField.
I need to write a delegate function that can 'wrap' some while/try/catch code around
I need to write a little ruby function that does word wrapping. I have
Basically what I need to do is write a function that takes in a
I need a function that could generate a regex. For example if I write
I'm trying to write a function that will get me the attribute of a
I wrote a WCF service with a function that using absolute path to get
I am in need to write a function in postgeresql/postgis to update m-value in

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.