473,508 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

To paletted image

Hello, this time I have a question about PIL usage, maybe if Lundh has
some time he can answer me.
I am experimenting different color quantization algorithms, so having
computed the palette with a clustering function, I use the code below
to quantize the original image to produce an image without dithering
(so I can see better the quantization results).

I have seen that the *standard* distance function I use isn't standard
enough, because given the fixed palette computed by me, most graphics
programs give me a different (better) quantized image. I don't know
what's wrong/different in this quantize function, maybe you can tell
me.
I'd also like to know if there is a simpler code for PIL to do the same
thing (given my palette and a truecolor image), but this is less
important. I know the dither=Image.NONE for the im.convert() method,
but I don't know a good way to use it in this problem. (Note that the
quantize function below uses a perceptual-based color distance, but to
do the quantization with PIL I can settle with its standard color
distance function.)

Thank you,
bearophile
# Input:
# im = input truecolor image
# palette = a palette computed by me, of about 32 colors
# im_out = output image with no dithering

def quantize(data, palette_short):
out_data = []
for rgb in data:
dist_min = 1e100
closest_col = None
for col_pos, pal_col in enumerate(palette_short):
# Standard distance
#dr = rgb[0] - pal_col[0]
#dg = rgb[1] - pal_col[1]
#db = rgb[2] - pal_col[2]
#d = dr*dr + dg*dg + db*db
d = perceptualColorDistance(rgb, pal_col)
if d < dist_min:
dist_min = d
closest_col = col_pos
out_data.append(closest_col)
return out_data

#..........
import psyco; psyco.bind(quantize)

# Copy of palette, to speed up quantization
palette_short = list(palette)

# Add duplicated colors (the last one) to produce a palette of 256
colors
palette.extend( palette[-1] for i in xrange(256 - len(palette)) )

# Create empty paletted output image
im_out = Image.new("P", im.size, 0)

# Flatten the list of colors, for PIL
#flattened_palette = flatten(palette)
flattened_palette = [component for color in palette for component in
color]

# Put the computed palette in the output image
im_out.putpalette(flattened_palette)

# quantize the input image with the computed palette
out_data = quantize(data, palette_short)

# Put the computed data inside the output image
im_out.putdata(out_data)

# Save computed output image
im_out.save(out_filename)

Feb 10 '06 #1
0 1717

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

Similar topics

9
3330
by: Pierre Tremblay | last post by:
Hi! I am trying to display an image in my html document. The document contains the following line: <td class="Input"><img...
3
11737
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
2
4137
by: Tjerk | last post by:
Hello all, I have the script below to change an image depending on the date upto january it worked fine but then it just stopped working does anybody have an idea how I can make it work again or...
8
2595
by: Jef Driesen | last post by:
I'm implementing some image processing algorithms in C++. I created a class called 'image' (see declaration below), that will take care of the memory allocations and some basic (mathematical)...
6
3297
by: QuasiChameleon | last post by:
Hi, I'm trying to create a grayscale image class that reads and writes grayscale Targa format. This works well with smaller images, but corrupts larger images and creates a "Segmentation fault...
15
31771
by: Anand Ganesh | last post by:
HI All, I have an Image. I want to clip a portion of it and copy to another image. How to do this? I know the bounding rectangle to clip. Any suggestions please. Thanks for your time and...
7
11600
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
15
5302
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
10707
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
7224
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,...
0
7120
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
7380
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...
1
7039
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...
0
7494
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...
0
5626
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,...
0
3192
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...
0
1553
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 ...
0
415
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...

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.