Hey, guys! Newbie here. =) I'm trying to return an image that contains n^2 full size copies of my original image tiled, that makes an n-by-n square. I've been working on this for the past couple days and I am stuck. It's giving me an "image index out of range" error and I'm not even sure my code will tile anything. Help?

(Oh, and if my code doesn't show up tabbed in the correct spots, my orginal code has appropriate indentation. That I'm sure of.)

import Image

def tile(img):
     w,h=img.size
     x=Image.new("RGB", (w*2, h*2))
     m=0
     n=0
    for r in range (n^2):
          if r==0:
               m==0
         if r==1:
               m=w
        for i in range(w):
            if r==0:
                n==0
            if r==0:
                n==h
            for j in range (h):
                p=img.getpixel((i,j))
                x.putpixel((m, n), p)
            n=n+1
            m=m+1
    return x
def main():
    filename=raw_input ("Gimme a picture.")
    n=raw_input ("How many times would you like it tiled?")
    img=Image.open(filename)
    tiled=tile(img)
    tiled.show()
main()

Recommended Answers

All 7 Replies

Welcome to the forums.

EDIT: OP Updated post with Code tags

Also, when asking about a specific error you get, it would help if we could see the entire traceback (which helps to identify exactly what line the error occured on). Seeing as you have an indexing error, we can narrow down where the error occurred, however it's hard to pin-point exactly what the problem is.

Can you please repost with the entirety of the traceback that you get?

EDIT 2: Also note that in Python ^ is the bit-wise XOR operator. To use "power of" 2 use **

This is what I'm getting:

Traceback (most recent call last):
File "C:\Python26\Com. Sci Homework\Tiles (29.7).py", line 32, in <module>
main()
File "C:\Python26\Com. Sci Homework\Tiles (29.7).py", line 30, in main
tiled=tile(img)
File "C:\Python26\Com. Sci Homework\Tiles (29.7).py", line 22, in tile
x.putpixel((m, n), p)
File "C:\Python26\lib\site-packages\PIL\Image.py", line 1233, in putpixel
return self.im.putpixel(xy, value)
IndexError: image index out of range

Looking at your code you have quite a nightmare of errors in there and on top of that the all important indentations are all screwed up.

!) use the customary 4 space indentations, avoid tabs!!!!

2) you set n = 0 before you use it in range()

3) you ask for n but never pass it on to tile()

4) m==0 should be m = 0

5) range (n^2) should most likely be range(n) or range(n**2)

There are a few more, but I have to stop.

BTW, PIL has the paste() function that will make the whole thing one lot easier.

1)I mentioned before that I do have the indentations in the appropriate spots in my original code--I should have edited the code posted here afterward. Also--I am new at this. Actually, transpose the word "suck" over "am new". lol
2) Good spot, thanks. That'll be removed.
3) What do you mean I ask for it and then it's not passed on to tile? 'n' is used in your next suggestion? Maybe I'm not understanding your statement?
4)Python yelled at me when m=0, but i'll try it again. (Side note, is that just a cosmetic thing or does it actually do something different than it should?)
5) Good spot. Should have known that.

What would I be pasting though? I'm struggling making duplicates of the thing and I don't know if I'm just thinking too hard about this--how do I make the whole image larger? Like, if my image was 100x300, how do I get it to make it 200x600 then?

Thanks for your ideas. =)

If you use m==0 it will give you False unless m is zero.

3) What do you mean I ask for it and then it's not passed on to tile? 'n' is used in your next suggestion? Maybe I'm not understanding your statement?

This is the section he's referring to:

def main():
    filename=raw_input ("Gimme a picture.")
    n=raw_input ("How many times would you like it tiled?")
    img=Image.open(filename)
    tiled=tile(img)
    tiled.show()

You store the user's input into the object n ; however after that you never use it. Inside your tile() function you do create a new n, however even if you removed the n = 0 step, you'd get an error because n does not exist in the tile function, only in your main function. What you would want to do is pass it to the tile function... something along the lines of tile(img, n) .

If you use m==0 it will give you False unless m is zero.

Just to further explain the difference between = and == :

>>> m = 0
>>> m
0
>>> m == 0
True
>>> m = 5
>>> m
5
>>> m == 0
False
>>>

As you can see, a single ( = ) is for assignment. A double ( == ) is for comparison a.k.a. "is equal to", and returns true or false.

Thanks! I get it now. I'll shift stuff around today.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.