[Tutor] More image manipulation

D. Hartley denise.hartley at gmail.com
Tue Jun 7 21:05:47 CEST 2005

Hello, everyone!

If someone has a few seconds, I am getting some weird errors and
Python won't tell me why.  What I am trying to do is take a middle
chunk out of a line of an image, place that at the beginning of the
line, and slide the rest over to the end, like so:





I'm doing this for each horizontal line in a 640x480 picture, and then
I want to take all those newlines and make a new image.  So I made a
list of all the pixel values (this is the one whose mode is "P," that
strange thing I emailed about before, so instead of a tuple of RGB
values, it just gives me one number for each pixel).  To create this
list, I did:

This is what I did:

def findlist():
    newlist = []
    for i in range(480):
        line = fromlist[:640]
        del fromlist[:640]
        x = line.index(195)
        y = x + 5
        z = line[x:y]
        del line[x:y]
        for i in z:
        for i in line:
    return newlist

So this worked exactly as I figured it would.  It returned me a list,
len 307200 (hooray!), and I looked at the first handful of 640-long
chunks and indeed, each now starts with five 195's (the middle chunk I
am removing from each line and re-placing at the beginning of each new

So now I have a list 307200 pixels long of pixel values, and I want to
put it into a new image.  I then did the following:

def makenewpic():
    newim = Image.new("P",(640,480))
    a = findlist()
    result = newim.putdata(a)
    return result

Now two weird things happen.  

A). If I try to run b = makenewpic()  (so that i'd have a b i could do
b.show() on), I get this error:

Traceback (most recent call last):
  File "<pyshell#53>", line 1, in ?
    b = makenewpic()
  File "Z:\misc\16.py", line 50, in makenewpic
    a = findlist()
  File "Z:\misc\16.py", line 37, in findlist
    x = line.index(195)
ValueError: list.index(x): x not in list

This is annoying enough in and of itself, besides, I am about 99% sure
that there IS a set of five 195's on every single line, and the error
does not seem to be valid *because*:

B). If I run the steps in makenewpic one by one in the interpreter, it
doesnt give me any "x not in list" error, it lets me do the result =
newim.putdata(a) just fine.  But then when I type result.show() it
tells me 'NoneType' object has no attribute 'show'.  At first I
thought it was because when I was creating the blank newim, I was
using mode "RGB" and adding a list of "P" pixel values to it. But I
fixed it so that my newim is also mode "P" and it still tells me that
type(result) = None.

Again, this is probably some little typo or dumb newbie error that I'm
missing because of looking at this thing four million times and
getting frustrated. I'm hoping a fresh set of eyes will help?

Thank you guys for your patience!


More information about the Tutor mailing list