473,411 Members | 1,894 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,411 software developers and data experts.

Palette-mode PNG images

I'm trying to create PNG files to use in menus for authoring DVDs. As you
may know, these menus are only allowed to have limited numbers of colours.

Ideally I'd like to create a PNG file with just two bits per pixel, with
four colour-table entries of my choice. I'm using PyCairo
<http://www.cairographics.org/pycairo/to do the drawing, but that doesn't
seem to support colour-table images as far as I can tell. So I'm trying to
figure out how to use PIL
<http://www.pythonware.com/library/pil/handbook/index.htmto save the
images to PNG files with a suitable format.

However, it looks like PIL wants 256 colour-table entries. When I try to
pass fewer, e.g.

ThePix = array.array("B", '\0' * ImageWidth * ImageHeight * 4)
ThePixSurface = cairo.ImageSurface.create_for_data(ThePix,
cairo.FORMAT_ARGB32, ImageWidth, ImageHeight, ImageWidth * 4)
# can't find format_stride_for_width?
TheDraw = cairo.Context(ThePixSurface)
...
ThePixSurface.flush() # prior to writing out pixels myself
TheImage = Image.frombuffer("RGBA", (ImageWidth, ImageHeight),
ThePix, "raw", "RGBA", 0, 1)
TheImage = TheImage.convert("P")
TheImage.putpalette([(0, 0, 0), (255, 255, 255), (0, 255, 0),
(0, 255, 255)])
TheImage.save("png_palette_test.png")

it dies with the following, on the putpalette line:

Traceback (most recent call last):
File "./png_palette_test", line 41, in <module>
TheImage.putpalette([(0, 0, 0), (255, 255, 255), (0, 255, 0),
(0, 255, 255)])
File "/usr/lib64/python2.5/site-packages/PIL/Image.py", line 1205,
in putpalette
data = string.join(map(chr, data), "")
TypeError: an integer is required

Cairo also supports FORMAT_A8 and FORMAT_A1 images--should I be using the
latter, perhaps?

Also I see that the PIL PNG encoder/decoder supports a "bits" output option
<http://www.pythonware.com/library/pil/handbook/format-png.htmwhich is
marked as "experimental". Can this be useful for constraining the pixel
depth of the output image?

Thanks for any suggestions.
Nov 16 '08 #1
1 4803
In message <gf**********@lust.ihug.co.nz>, I wrote:
data = string.join(map(chr, data), "")
TypeError: an integer is required
OK, I figured that out, the putpalette call wants a sequence of integers
being (R, G, B, R, G, B ...), not a sequence of sequences ((R, G, B), (R,
G, B)...).
TheImage = TheImage.convert("P")
This gives me 8 bits per pixel, which is too many. And this automatically
converts the image using a default palette; a subsequent putpalette call
replaces the palette, but doesn't change the pixels, so the colours come
out wrong.

I can also do Image.convert("1"), which gives me a 1-bit-per-pixel image,
but that only allows a fixed colour table (black and white), no option to
change either entry.
Also I see that the PIL PNG encoder/decoder supports a "bits" output
option ...
Hmm, interesting, but adding either "bits = 2" or "bits = 1" to the save
call just seems to turn the image all black. Examination with
ImageMagick's "identify" command shows that the default colour table ramp
has been stair-stepped, with all components turned into multiples of 51 (=
255 / 5).

Nov 19 '08 #2

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

Similar topics

0
by: Jean-Sebastien Carle | last post by:
I've been ripping my hair our trying to get this to work for a week and I can't seem to get it. I'm trying to display a 4BppIndexed Bitmap from within the ntoskrnl.exe file that contains no palette....
4
by: Just Me | last post by:
I asked this in the VB NG and didn't get a response. I think this NG is no apt to know this kind of thing so I'm trying here. Trying to display the palette that is on the clipboard. I get a...
0
by: Micus | last post by:
Hi all, Does anyone have experience with using a palette with a progress control? Everything works fine with a scrollbar control, but I think there are issues with the progress control because it's...
0
by: Nathan Sokalski | last post by:
I am learning how to use the System.ComponentModel class in VB.NET so that I can add my ASP.NET controls to Visual Studio .NET 2003. I have managed to make my control properties visible in the...
0
by: Carles | last post by:
Hi, Currently, I use a cDIB class to manage all color depth DIBs. But now I would like to avoid selecting my hDIB into a DC, and process all through manipulating data directly from/through...
6
by: active | last post by:
I asked this in VB without results. I'm not surprised that no one there knows anything about because it's kind of an uncommon subject. Maybe some one here knows. Dim DataO As DataObject =...
15
by: active | last post by:
Below is a small but complete program that appears to show you can't retrive a Palette from the clipboard. This is true whether the palette is placed on the clipboard by Photoshop or Photoshop...
8
by: active | last post by:
Guess I'm looking for someone who likes to work difficult puzzles. I can't seem to ever retrieve a palette handle from the clipboard. Below is a simple test program that demonstrates the...
3
by: krishnasamy | last post by:
Hi, I have writing the code for change the Palette color of the Bitmap. But I didn't get any different between given image and result image. Here I given my code, Bitmap *bmp=new...
1
by: jj | last post by:
The following creates a copy of an image. It worked fine in Visual Studio 2005, but after installing Visual Studio 2008 then opening the solution as a ..net 2.0 project, I get an error on the...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.