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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en.