Skip to content

Instantly share code, notes, and snippets.

@dahlia
Created March 4, 2013 12:00
Show Gist options
  • Save dahlia/5081821 to your computer and use it in GitHub Desktop.
Save dahlia/5081821 to your computer and use it in GitHub Desktop.
Wand experiments with PixelGet{Red,Green,Blue,Alpha}Quantum() functions
import ctypes
from wand.api import library
from wand.color import Color
library.PixelGetRedQuantum.argtypes = [ctypes.c_void_p]
library.PixelGetRedQuantum.restype = ctypes.c_size_t
library.PixelGetGreenQuantum.argtypes = [ctypes.c_void_p]
library.PixelGetGreenQuantum.restype = ctypes.c_size_t
library.PixelGetBlueQuantum.argtypes = [ctypes.c_void_p]
library.PixelGetBlueQuantum.restype = ctypes.c_size_t
library.PixelGetAlphaQuantum.argtypes = [ctypes.c_void_p]
library.PixelGetAlphaQuantum.restype = ctypes.c_size_t
with Color('#ff0088') as c:
print (c.red, c.green, c.blue, c.alpha)
# Prints: (1.0, 0.0, 0.5333333333333333, 1.0)
print (
library.PixelGetRedQuantum(c.resource),
library.PixelGetGreenQuantum(c.resource),
library.PixelGetBlueQuantum(c.resource),
library.PixelGetAlphaQuantum(c.resource),
)
# Prints: (65535L, 0L, 34952L, 65535L)
@inactivist
Copy link

Apparently you can override output color depth on the convert command line using the -depth switch:

convert -size 256x256 -depth 8 img.rgba img.png

Not sure how to do this in Wand but ImageMagick seems capable.

@inactivist
Copy link

More:
Trouble setting RGB color depth
Controling the Quality of Images

There's a way to specify image 'depth' for the output image (image.depth(8)) but it doesn't alter the internal representation, just the output format, it seems.

So yeah, I'll need to use ScaleQuantumToChar() or its equivalent.

@dahlia
Copy link
Author

dahlia commented Mar 4, 2013

@inactivist Thanks for your surveys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment