473,785 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(localr gb)
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.fromima ge(im)
out = rgbarrtohsvarr( arr)

--------

Jan 4 '08 #1
4 2215
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
10651
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 Request" was the original error msg now that I reinstalled iislockdown and urscan I get "The parameter is incorrect." If I use the get method with this form it works just fine on everything. I can't say for certain but I think this problem started...
2
7750
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 am placing a transparent gif over the top of a normal gif) then the width of my table is large enough to accommodate both images side by side My browsers (both IE ad Firefox) do not appear take into account the relative positioning of my...
5
3487
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 that change the background color to have a visible effect, as this bar changes color. By default, it appears that the color is inherited from the body color, i.e. with no link style applied, the bar is black, and invisible against the background....
5
3360
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 printer using a RawPrinterHelper class that i found I believe in the msdn. the class works wonderfully when I send stright text data (in the correctly formated string that the sato printer requires.) but when i go to send a image nothing is...
1
3722
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 attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
1
4493
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 need to remove. The page is for a nonprofit organization. You can see my HTML code here, and I have attached a graphic that shows how the page should look.
5
1553
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 NULL value. I put in a few fprintf() calls to verify that the file name & path is being set properly. ?? am I referencing the structure element correctly in the freadpng() function ??
4
2106
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 the file and use TK to draw the images... but 1) It does not seem to end (running in IDLE), I have to kill the process to retry it seems tkinter does not close(?)
0
2847
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. Here is the website: holtz-realty And also the html file and css file. Anny help will by mostly appreciated. I did try everything I can think of. HTML:
0
9645
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
10325
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
10091
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
8972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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
4053
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
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.