473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Request for help with Image color space conversion

Hello,

I'm working on an image processing project using the Python Imaging
Library along with numpy. Right now, I'm trying to build a speedy
script for converting whole images between the RGB and the HSV (a.k.a.
HSB) color spaces. Unfortunately, the code I've made so far runs
dreadfully slow with even moderate-sized images.

I'm under the impression that the crux of the problem is the fact that
PIL's point method operates on only one band at a time, and to do a
proper color space conversion, you need information about all three
bands in a particular problem. This has forced me to do an awkward
work-around where the image data is loaded into a numpy array
(1600x1200x3), and a dinky for-loop run runs through the array picking
up RGB values, converting to HSV, and dumping the results back into
another array.

How can I make this more efficient?

--------

from PIL import Image
from PIL import TiffImagePlugin
from scipy.misc import pilutil
import numpy
import colorsys

def myrgbtohsv(r,g,b):
'''A wrapper for the colorsys function rgb_to_hsv
that takes r,g,b values between 0 and 255
(standard pixel value range in most of my single-band imaging
operations)
and returns the corresponding 255-scaled h,s,v values'''
tr = r/255.0
tg = g/255.0
tb = b/255.0
ur,ug,ub = colorsys.rgb_to_hsv(tr,tg,tb)
pr = int(round(ur,0))*255
pg = int(round(ug,0))*255
pb = int(round(ub,0))*255
return pr,pg,pb

def rgbarrtohsvarr(pilarray):
'''Takes an 3d numpy ndarray loaded with a RGB PIL image and
converts
a copy of the contents to 255-scaled HSV array.

Returns the converted copy of the array.
'''
arrt = pilarray.copy()
r,c,d = arrt.shape
hsvarray = numpy.zeros((r,c,d))

print out
for i in range(0,r):
for j in range(0,c):
localrgb = (arrt[i,j,0],arrt[i,j,1],arrt[i,j,2])
(lh,ls,lv) = rgbtohsv(localrgb)
hsvarray[i,j,0],hsvarray[i,j,1],hsvarray[i,j,2]= lh,ls,lv

return hsvarray

im = Image.open(r'C:\test.tif')
arr = pilutil.fromimage(im)
out = rgbarrtohsvarr(arr)

--------

Jan 4 '08 #1
4 2198
ttest wrote:
Hello,

I'm working on an image processing project using the Python Imaging
Library along with numpy. Right now, I'm trying to build a speedy
script for converting whole images between the RGB and the HSV (a.k.a.
HSB) color spaces. Unfortunately, the code I've made so far runs
dreadfully slow with even moderate-sized images.

I'm under the impression that the crux of the problem is the fact that
PIL's point method operates on only one band at a time, and to do a
proper color space conversion, you need information about all three
bands in a particular problem. This has forced me to do an awkward
work-around where the image data is loaded into a numpy array
(1600x1200x3), and a dinky for-loop run runs through the array picking
up RGB values, converting to HSV, and dumping the results back into
another array.

How can I make this more efficient?
Reimplement colorsys.rgb_to_hsv() such that it operates on arrays instead of
scalars. Only minor modifications are necessary.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jan 4 '08 #2
Reimplement colorsys.rgb_to_hsv() such that it operates on arrays instead of
scalars. Only minor modifications are necessary.

--
Robert Kern
Thanks! I'll try and see if a newcomer like me can get his head
around the array-centric modifications to colorsys.
Jan 5 '08 #3
ttest wrote:
>Reimplement colorsys.rgb_to_hsv() such that it operates on arrays instead of
scalars. Only minor modifications are necessary.

--
Robert Kern

Thanks! I'll try and see if a newcomer like me can get his head
around the array-centric modifications to colorsys.
If you hit any roadblocks, drop in on numpy-discussion, and we'll help you out.

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jan 6 '08 #4
>
...where the image data is loaded into a numpy array
(1600x1200x3)...

One comment: that is a big array, too big for the cache memory. I know
that in these cases it makes a difference how many times the slices of
the array are loaded and unloaded from RAM onto cache. One issue is
that a 2D array l[i,j] is actually a 1D array:
either
l[i,j]=l[i*N+j]
or
l[i,j]=l[j*N+i]
I don't know which in python/numpy. In the first case, when you fix i
and change j, you get consecutive positions in the underlying 1D
array, and they all belong to the same slice, which is loaded onto
cache very fast as a block. If you do the opposite, you are making
jumps to distant positions in the array (not a slice), and performance
suffers.

In gimp, for example, the underlying array is split into 'tiles',
which are 64x64 pixel regions that can be loaded/unloaded as a block.
And that's about all I know on the issue.

So it ma


Jan 6 '08 #5

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

Similar topics

8
by: turnit \(removethis\) | last post by:
I have a login form that uses the post method to carry the information to the next page. The form works just fine in ie6.0, but fails in mozilla and fails in ie5.2 on a mac. "HTTP/1.1 400 Bad...
2
by: Stephen Weatherly | last post by:
Could anyone please help me with a problem I am having with my table widths??? If I have 2 images within a td tag, but using CSS relative positioning I position one over the top of the second (I...
5
by: Chris Beall | last post by:
I'm displaying an image that is also a link against a black background. In Netscape 7.1, the current background color is displayed as a horizontal bar below the image. This allows :hover effects...
5
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
1
by: Line 1 | last post by:
Hello!! Can you help me with my CSS problems? I have created a page that overlays text on an image. the problem is that it is leaving all this space at the bottom of background image that I also...
5
by: SP | last post by:
I am writing a function to read data into a structure, but I seem to be messing up the character string in the fopen() call. When the program runs it does not open the file and fopen() returns a...
4
by: larry | last post by:
Ok I'm a Python noob, been doing OK so far, working on a data conversion program and want to create some character image files from an 8-bit ROM file. Creating the image I've got down, I open...
0
by: richard12345 | last post by:
Hi Guys I have problem with site I am building. The sidebar with menu and other thinks is overlapping footer. The footer move with the content and but it dos it dos not move with the sidebar. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.