Re: strange behavior with image.set_data()

101 views
Skip to first unread message

Nathan

unread,
Apr 25, 2013, 5:28:05 PM4/25/13
to pyglet...@googlegroups.com
I figured it out!  Okay, I know this is 6 months later and you've probably either figured out the problem or dumped pyglet, but hey, I've been busy for the last 6 months!

Anyway, you set your format to 'RGB', and you pass that into all the functions, but the new image you create defaults to 'RGBA'.  So all you have to do to fix the problem is set the format on newImage to 'RGB' after you create it.

newImage.format = FORMAT


Or here's the fix in patch format:

--- p1.py 2013-04-25 15:26:08.000000000 -0600
+++ p2.py 2013-04-25 15:26:11.000000000 -0600
@@ -12,6 +12,7 @@
 imageData = list(imageData)

 newImage = pyglet.image.create(WIDTH, HEIGHT)
+newImage.format = FORMAT
 data = len(FORMAT) * WIDTH * HEIGHT * [chr(0)]

 def getPixel(x, y):


That was fun to debug.  I got into all sorts of internals and kept verifying things were working.  What finally tipped me off was the ratio of your problem.  You would get 450 out of 600 pixels correct, which is exactly 3 out of 4.  It matched up nicely with the len(format) that was being used all over the place internally.

~ Nathan


On Wed, Oct 17, 2012 at 7:10 AM, Brian Will <brian.th...@gmail.com> wrote:
I'm trying to set up a window where:

1) an image file is displayed in the left half of the window
2) I can read RGB values of individual pixels in that image
3) a second image, created pixel-by-pixel in memory, is displayed in the right half of the window

The main purpose here is to play with and demonstrate basic image scaling and other kinds of image manipulation. Performance is no concern here, just simplicity.

The errant behavior I'm getting is that, in the image I produce for the right side of the window, some number of its leftmost columns are displaying duplicated on the right side, e.g. no matter what I write to column x=500, a copy of column x=50 shows at x=500.

Tell me if I'm doing something wrong here or if maybe I've stumbled upon a bug. This is on Windows with Python 2.7 and pyglet 1.2a1:

import pyglet

WIDTH = 600
HEIGHT = 600
FORMAT = 'RGB'
FILE = 'The_Scream.jpg'  # whatever image you use, keep it within the WIDTH and HEIGHT, else you'll get index errors with the code below

image = pyglet.image.load(FILE)
imageData = image.get_data(FORMAT, image.width * len(FORMAT))
imageData = list(imageData)

newImage = pyglet.image.create(WIDTH, HEIGHT)
data = len(FORMAT) * WIDTH * HEIGHT * [chr(0)]

def getPixel(x, y):
    ''' assumes RGB
        returns tuple of (r, g, b)'''
    pixelIdx = x + (y * image.width)
    byteIdx = pixelIdx * len(FORMAT)
    return (ord(imageData[byteIdx]), ord(imageData[byteIdx + 1]), ord(imageData[byteIdx + 2]))
    
def setPixel(x, y, color):
    ''' color is tuple of (R, G, B) as integers'''
    pixelIdx = x + (y * WIDTH)
    byteIdx = pixelIdx * len(FORMAT)
    r, g, b = color
    data[byteIdx] = chr(r)
    data[byteIdx + 1] = chr(g)
    data[byteIdx + 2] = chr(b)


# get and set pixels here ###########################

# no matter what I draw here, every column starting at about x=450 is a repeat of the columns starting from x=0, 
# e.g. column x=500 is the same as whatever I draw in column x=50


for y in range(200, HEIGHT):
    for x in range(200, WIDTH):
        setPixel(x, y, (255, 0, 0))

for y in range(image.height):
    for x in range(image.width):
        pixel = getPixel(x, y)
        setPixel(x, y, pixel)

for x in range(0, WIDTH, 50):
    setPixel(x, 500, (0, 255, 0))


for y in range(HEIGHT):
    setPixel(500, y, (255, 255, 0))



# render images ############################

data = ''.join(data)
newImage.set_data(FORMAT, WIDTH * len(FORMAT), data)

win = pyglet.window.Window(WIDTH * 2, HEIGHT, caption='image render')

@win.event
def on_draw():
    image.blit(0, 0)
    newImage.blit(WIDTH, 0)

pyglet.app.run()


--
You received this message because you are subscribed to the Google Groups "pyglet-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pyglet-users/-/eBE_x4fs4hYJ.
To post to this group, send email to pyglet...@googlegroups.com.
To unsubscribe from this group, send email to pyglet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyglet-users?hl=en.

Reply all
Reply to author
Forward
0 new messages