Message from discussion
strange behavior with image.set_data()
Received: by 10.42.33.9 with SMTP id g9mr10432729icd.25.1350512678748;
Wed, 17 Oct 2012 15:24:38 -0700 (PDT)
X-BeenThere: pyglet-users@googlegroups.com
Received: by 10.50.208.37 with SMTP id mb5ls3438184igc.2.gmail; Wed, 17 Oct
2012 15:24:37 -0700 (PDT)
Received: by 10.42.33.9 with SMTP id g9mr10432703icd.25.1350512677477;
Wed, 17 Oct 2012 15:24:37 -0700 (PDT)
Received: by 10.50.0.146 with SMTP id 18msige;
Wed, 17 Oct 2012 06:10:48 -0700 (PDT)
Received: by 10.68.212.72 with SMTP id ni8mr2404460pbc.14.1350479448700;
Wed, 17 Oct 2012 06:10:48 -0700 (PDT)
Date: Wed, 17 Oct 2012 06:10:47 -0700 (PDT)
From: Brian Will <brian.thomas.w...@gmail.com>
To: pyglet-users@googlegroups.com
Message-Id: <d52f7489-fb16-4db2-87da-170c8986f6a7@googlegroups.com>
Subject: strange behavior with image.set_data()
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_6_14244464.1350479447678"
------=_Part_6_14244464.1350479447678
Content-Type: multipart/alternative;
boundary="----=_Part_7_29404428.1350479447678"
------=_Part_7_29404428.1350479447678
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
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()
------=_Part_7_29404428.1350479447678
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<div>I'm trying to set up a window where:<br></div><div><br></div><div>1) a=
n image file is displayed in the left half of the window</div><div>2) I can=
read RGB values of individual pixels in that image</div><div>3) a second i=
mage, created pixel-by-pixel in memory, is displayed in the right half of t=
he window</div><div><br></div><div>The main purpose here is to play with an=
d demonstrate basic image scaling and other kinds of image manipulatio=
n. Performance is no concern here, just simplicity.</div><div><br></div><di=
v>The errant behavior I'm getting is that, in the image I produce for the r=
ight 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=3D50=
0, a copy of column x=3D50 shows at x=3D500.</div><div><br></div><div>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:</div><div><font face=
=3D"courier new, monospace"><br></font></div><span style=3D"font-family: 'c=
ourier new', monospace; ">import pyglet</span><br><font face=3D"courier new=
, monospace"><br></font><span style=3D"font-family: 'courier new', monospac=
e; ">WIDTH =3D 600</span><br><span style=3D"font-family: 'courier new', mon=
ospace; ">HEIGHT =3D 600</span><br><span style=3D"font-family: 'courier new=
', monospace; ">FORMAT =3D 'RGB'</span><br><span style=3D"font-family: 'cou=
rier new', monospace; ">FILE =3D 'The_Scream.jpg' # whatever image yo=
u use, keep it within the WIDTH and HEIGHT, else you'll get index errors wi=
th the code below</span><br><font face=3D"courier new, monospace"><br></fon=
t><span style=3D"font-family: 'courier new', monospace; ">image =3D pyglet.=
image.load(FILE)</span><br><span style=3D"font-family: 'courier new', monos=
pace; ">imageData =3D image.get_data(FORMAT, image.width * len(FORMAT))</sp=
an><br><span style=3D"font-family: 'courier new', monospace; ">imageData =
=3D list(imageData)</span><br><font face=3D"courier new, monospace"><br></f=
ont><span style=3D"font-family: 'courier new', monospace; ">newImage =3D py=
glet.image.create(WIDTH, HEIGHT)</span><br><span style=3D"font-family: 'cou=
rier new', monospace; ">data =3D len(FORMAT) * WIDTH * HEIGHT * [chr(0)]</s=
pan><br><font face=3D"courier new, monospace"><br></font><span style=3D"fon=
t-family: 'courier new', monospace; ">def getPixel(x, y):</span><br><span s=
tyle=3D"font-family: 'courier new', monospace; "> ''' assumes =
RGB</span><br><span style=3D"font-family: 'courier new', monospace; "> =
; returns tuple of (r, g, b)'''</span><br><span style=
=3D"font-family: 'courier new', monospace; "> pixelIdx =3D x +=
(y * image.width)</span><br><span style=3D"font-family: 'courier new', mon=
ospace; "> byteIdx =3D pixelIdx * len(FORMAT)</span><br><span =
style=3D"font-family: 'courier new', monospace; "> return (ord=
(imageData[byteIdx]), ord(imageData[byteIdx + 1]), ord(imageData[byteIdx + =
2]))</span><br><span style=3D"font-family: 'courier new', monospace; ">&nbs=
p; </span><br><span style=3D"font-family: 'courier new', monosp=
ace; ">def setPixel(x, y, color):</span><br><span style=3D"font-family: 'co=
urier new', monospace; "> ''' color is tuple of (R, G, B) as i=
ntegers'''</span><br><span style=3D"font-family: 'courier new', monospace; =
"> pixelIdx =3D x + (y * WIDTH)</span><br><span style=3D"font-=
family: 'courier new', monospace; "> byteIdx =3D pixelIdx * le=
n(FORMAT)</span><br><span style=3D"font-family: 'courier new', monospace; "=
> r, g, b =3D color</span><br><span style=3D"font-family: 'cou=
rier new', monospace; "> data[byteIdx] =3D chr(r)</span><br><s=
pan style=3D"font-family: 'courier new', monospace; "> data[by=
teIdx + 1] =3D chr(g)</span><br><span style=3D"font-family: 'courier new', =
monospace; "> data[byteIdx + 2] =3D chr(b)</span><br><font fac=
e=3D"courier new, monospace"><br><br></font><span style=3D"font-family: 'co=
urier new', monospace; "># get and set pixels here ########################=
###</span><br><font face=3D"courier new, monospace"><br></font><span style=
=3D"font-family: 'courier new', monospace; "># no matter what I draw here, =
every column starting at about x=3D450 is a repeat of the columns starting =
from x=3D0, </span><br><span style=3D"font-family: 'courier new', mono=
space; "># e.g. column x=3D500 is the same as whatever I draw in column x=
=3D50</span><br><font face=3D"courier new, monospace"><br><br></font><span =
style=3D"font-family: 'courier new', monospace; ">for y in range(200, HEIGH=
T):</span><br><span style=3D"font-family: 'courier new', monospace; "> =
; for x in range(200, WIDTH):</span><br><span style=3D"font-family: =
'courier new', monospace; "> setPixel(x, y, (255=
, 0, 0))</span><br><font face=3D"courier new, monospace"><br></font><span s=
tyle=3D"font-family: 'courier new', monospace; ">for y in range(image.heigh=
t):</span><br><span style=3D"font-family: 'courier new', monospace; "> =
; for x in range(image.width):</span><br><span style=3D"font-family:=
'courier new', monospace; "> pixel =3D getPixel=
(x, y)</span><br><span style=3D"font-family: 'courier new', monospace; ">&n=
bsp; setPixel(x, y, pixel)</span><br><font face=3D"cou=
rier new, monospace"><br></font><span style=3D"font-family: 'courier new', =
monospace; ">for x in range(0, WIDTH, 50):</span><br><span style=3D"font-fa=
mily: 'courier new', monospace; "> setPixel(x, 500, (0, 255, 0=
))</span><br><font face=3D"courier new, monospace"><br><br></font><span sty=
le=3D"font-family: 'courier new', monospace; ">for y in range(HEIGHT):</spa=
n><br><span style=3D"font-family: 'courier new', monospace; "> =
setPixel(500, y, (255, 255, 0))</span><br><font face=3D"courier new, monos=
pace"><br><br><br></font><span style=3D"font-family: 'courier new', monospa=
ce; "># render images ############################</span><br><font face=3D"=
courier new, monospace"><br></font><span style=3D"font-family: 'courier new=
', monospace; ">data =3D ''.join(data)</span><br><span style=3D"font-family=
: 'courier new', monospace; ">newImage.set_data(FORMAT, WIDTH * len(FORMAT)=
, data)</span><br><font face=3D"courier new, monospace"><br></font><span st=
yle=3D"font-family: 'courier new', monospace; ">win =3D pyglet.window.Windo=
w(WIDTH * 2, HEIGHT, caption=3D'image render')</span><br><font face=3D"cour=
ier new, monospace"><br></font><span style=3D"font-family: 'courier new', m=
onospace; ">@win.event</span><br><span style=3D"font-family: 'courier new',=
monospace; ">def on_draw():</span><br><span style=3D"font-family: 'courier=
new', monospace; "> image.blit(0, 0)</span><br><span style=3D=
"font-family: 'courier new', monospace; "> newImage.blit(WIDTH=
, 0)</span><br><font face=3D"courier new, monospace"><br></font><div><div><=
font face=3D"courier new, monospace">pyglet.app.run()</font></div></div><di=
v><div><br></div><div><br></div></div>
------=_Part_7_29404428.1350479447678--
------=_Part_6_14244464.1350479447678--