473,888 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fastest Way To Loop Through Every Pixel

As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.

Jul 28 '06 #1
30 9106

Chaos wrote:
As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY
OT: you don't need the 0 in the range call. Taking it out doesn't make
it run faster, though.
>
But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.
Unsuccessful because .... what?
[I wasn't aware that swig was intended to compile Python scripts]

Sticking with Python:
With the "for thisX" try
(1) using xrange instead of range.
(2) widthRange = range(thisWidth )
for thisY in range(thisHeigh t):
for thisX in widthrange:
and in general, hoist loop-invariants outside the loop.

Have you considered Pyrex?

It all depends on what "#Actions here for Pixel thisX, thisY" is doing.
Perhaps there is a library (PIL, pygame, ...) that does what you are
trying to do.
Perhaps if you show us what you are doing, we can give you better
advice.

HTH,
John

Jul 28 '06 #2

John Machin wrote:
Chaos wrote:
As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY

OT: you don't need the 0 in the range call. Taking it out doesn't make
it run faster, though.

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.

Unsuccessful because .... what?
[I wasn't aware that swig was intended to compile Python scripts]

Sticking with Python:
With the "for thisX" try
(1) using xrange instead of range.
(2) widthRange = range(thisWidth )
for thisY in range(thisHeigh t):
for thisX in widthrange:
and in general, hoist loop-invariants outside the loop.

Have you considered Pyrex?

It all depends on what "#Actions here for Pixel thisX, thisY" is doing.
Perhaps there is a library (PIL, pygame, ...) that does what you are
trying to do.
Perhaps if you show us what you are doing, we can give you better
advice.

HTH,
John
Nope still same speed. I also tried pyrex but I couldnt understand how
to build pyx files. I didnt see how to set up the files using C. I
wasnt sure if you were supposed use the example or build your own.

With pypy I got a strange error trying to open py files. It said the
first character of evey py file was unknown.

I may try SWIG again becuase I fail to rememeber why I stopped using it.

Jul 28 '06 #3
Hello Chaos,

Whatever you do in "#Actions here ..." might be expressed nicely as a
ufunction for numeric. Then you might be able to convert the expression
to a numeric expression. Check out numpy/scipy.

In general, if thisHeight, thisWidth are large use xrange not range.
range() generates a list of numbers first then iterates through them.
So try that first.

Then of course if you do the whole thing many times you could just
pre-generate the indices as in:
all_indices=[]
for i in xrange(thisHeig ht):
for j in xrange(thisWidt h):
all_indices.app end( (i,j) )

Then each time you need to run '#Actions here...' you can just use
for (i,j) in all_indices:
#Actions here ... blah blah

In general, if there would be a way to significantly optimize generic
for loops, they would probably be already optimized...

Nick V.
Chaos wrote:
As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.
Jul 28 '06 #4

Chaos wrote:
John Machin wrote:
Chaos wrote:
As my first attempt to loop through every pixel of an image, I used
>
for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY
OT: you don't need the 0 in the range call. Taking it out doesn't make
it run faster, though.
>
But it takes 450-1000 milliseconds
>
I want speeds less than 10 milliseconds
>
I have tried using SWIG, and pypy but they all are
unsuccessfull in
compiling my files.
Unsuccessful because .... what?
[I wasn't aware that swig was intended to compile Python scripts]

Sticking with Python:
With the "for thisX" try
(1) using xrange instead of range.
(2) widthRange = range(thisWidth )
for thisY in range(thisHeigh t):
for thisX in widthrange:
and in general, hoist loop-invariants outside the loop.

Have you considered Pyrex?

It all depends on what "#Actions here for Pixel thisX, thisY" is doing.
Perhaps there is a library (PIL, pygame, ...) that does what you are
trying to do.
Perhaps if you show us what you are doing, we can give you better
advice.

HTH,
John

Nope still same speed. I also tried pyrex but I couldnt understand how
to build pyx files. I didnt see how to set up the files using C. I
wasnt sure if you were supposed use the example or build your own.

With pypy I got a strange error trying to open py files. It said the
first character of evey py file was unknown.

I may try SWIG again becuase I fail to rememeber why I stopped using it.
With SWIG when I tried to execute ld -shared example.o example_wrap.o
-o _example.so line in CMD I got example_wrap.o: example.o
:<.text+0x####> : undifned refrence to object

--Nick

Thank you but it didnt work, but I think I am getting somewhere because
I used your method and got 400 ms, I think that is something because I
used 2 loops

He is the code #Actions here

myCol = (0.3 * image.GetRed(th isX, thisY)) + (0.59 *
image.GetGreen( thisX, thisY)) + (0.11 * image.GetBlue(t hisX, thisY))
if myCol < darkestCol:
darkestCol = myCol
possX = thisX
possY = thisY

Jul 28 '06 #5
Chaos wrote:
As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.
You could try the PIL package.
>From the docs at
http://www.pythonware.com/library/pi...book/image.htm

Image.eval(func tion, image) =image

Applies the function (which should take one argument) to each pixel in
the given image. If the image has more than one band, the same function
is applied to each band. Note that the function is evaluated once for
each possible pixel value, so you cannot use random components or other
generators.

HTH,
~Simon

Jul 28 '06 #6
Have you tried PIL? (Python Imaging Library)

"Chaos" <ps*******@gmai l.comwrote:
>As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.
--
Regards,
Casey
Jul 28 '06 #7

Simon Forman wrote:
Chaos wrote:
As my first attempt to loop through every pixel of an image, I used

for thisY in range(0, thisHeight):
for thisX in range(0, thisWidth):
#Actions here for Pixel thisX, thisY

But it takes 450-1000 milliseconds

I want speeds less than 10 milliseconds

I have tried using SWIG, and pypy but they all are unsuccessfull in
compiling my files.

You could try the PIL package.
From the docs at
http://www.pythonware.com/library/pi...book/image.htm

Image.eval(func tion, image) =image

Applies the function (which should take one argument) to each pixel in
the given image. If the image has more than one band, the same function
is applied to each band. Note that the function is evaluated once for
each possible pixel value, so you cannot use random components or other
generators.

HTH,
~Simon
I have tried PIL. Not only that, but the Image.eval function had no
success either. I did some tests and I found out that Image.eval only
called the function a certain number of times either 250, or 255.
Unless I can find a working example for this function, its impossible
to use.

Jul 28 '06 #8
"Chaos" <ps*******@gmai l.comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
>

myCol = (0.3 * image.GetRed(th isX, thisY)) + (0.59 *
image.GetGreen( thisX, thisY)) + (0.11 * image.GetBlue(t hisX, thisY))
if myCol < darkestCol:
darkestCol = myCol
possX = thisX
possY = thisY
Psyco may be of some help to you, especially if you extract out your myCol
expression into its own function, something like:

def darkness(img,x, y):
return (0.3 * img.GetRed(x,y) ) + (0.59 * img.GetGreen(x, y)) + (0.11 *
img.GetBlue(x,y ))

You may also be paying a penalty for the floating-point multiplications .
Since you are only concerned with the relative values, what if you scale up
all of your weighting coefficients, so that you only do integer multiplies?

def darkness(img,x, y):
return (30 * img.GetRed(x,y) ) + (59 * img.GetGreen(x, y)) + (11 *
img.GetBlue(x,y ))

You can also cut down on resolution of your GetXXX functions by saving them
to locals.

RedVal = Image.GetRed
GrnVal = Image.GetGreen
BluVal = Image.GetBlue
def darkness(img,x, y):
return (30 * RedVal(img,x,y) ) + (59 * GreenVal(img,x, y)) + (11 *
BlueVal(img,x,y ))

Even downer-and-dirtier, you could approximate 30 with 32, 59 with 64, and
11 with 8, and do bit-shifting instead of multiplying:

def darkness(img,x, y):
return (RedVal(img,x,y ) << 5) + (GreenVal(img,x ,y) << 6) +
(BlueVal(img,x, y) << 3)
-- Paul
Jul 28 '06 #9

"Nick Vatamaniuc" <va******@gmail .comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.com.. .
Hello Chaos,

Then of course if you do the whole thing many times you could just
pre-generate the indices as in:
all_indices=[]
for i in xrange(thisHeig ht):
for j in xrange(thisWidt h):
all_indices.app end( (i,j) )
Or just:

all_indices = [ (i,j) for i in xrange(thisHeig ht) for j in
xrange(thisWidt h) ]

Embrace those list comprehensions!

-- Paul
Jul 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2345
by: DraguVaso | last post by:
Hi, I need to find the FASTEST way to get a string in a Loop, that goes from "a" to "ZZZZZZZZZZZZZZZZZ". So it has to go like this: a b .... z
3
21414
by: Lance | last post by:
What is the fastest way to determine if two arrays that contain ValueTypes are equal? For example, lets say I have the following: Dim pt1 as New Drawing.Point(1, 2) Dim pt2 as New Drawing.Point(2, 3) Dim pt3 as New Drawing.Point(3, 4) Dim pt4 as New Drawing.Point(4, 5) Dim A() as Drawing.Point = {pt1, pt2, pt3}
44
3249
by: Don Kim | last post by:
Ok, so I posted a rant earlier about the lack of marketing for C++/CLI, and it forked over into another rant about which was the faster compiler. Some said C# was just as fast as C++/CLI, whereas others said C++/CLI was more optimized. Anyway, I wrote up some very simple test code, and at least on my computer C++/CLI came out the fastest. Here's the sample code, and just for good measure I wrote one in java, and it was the slowest! ;-)...
7
6058
by: kebalex | last post by:
Hi, I have an app (written in .NET 2.0) which updates a picturebox according to some user input (a slider control). when the user makes a change i loop through all of the pixels, do a calculation and update the picture. i currently use the GDI SetPixel method on each pixel of the Pictureboxes image. This is proving far to slow, about 1.5 seconds on average. This app needs to display the update as fast as possible. Has anyone got any...
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5817
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4244
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.