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

These give some clues:

Architecture

Controling the Quality of Images

Quality and Depth are two terms are often talked about in Mailing Lists and in these example pages, so I'd like to explain them a little. Quality is a compile time setting in ImageMagick, and is used to determine the size of the values use to store images in IM memory and during processing. Basically it means the Quality of Processing that a specific IM was compiled for.

The Depth is the size of the values used when an image is either read or saved to/from an Image File Format. It is as such more highly variable. and controlled by the "-depth" setting, or by the original 'depth' of the image that was read in. more on this in a moment.

Remember...

Quality is 'in memory' value size, and is compiled into IM.
Depth is file format value size, and is variable.

Working with color

PixelPacket is the internal representation of a pixel in ImageMagick. ImageMagick may be compiled to support 32, 64, or 128 bit pixels of type PixelPacket. This is controlled by the value of the QuantumDepth define. The default is 32 bit pixels (QuantumDepth=8), which provides the best performance and the least resource consumption. If additional color precision or range is desired, then ImageMagick may be compiled with QuantumDepth=16 or QuantumDepth=32. The following table shows the relationship between QuantumDepth, the type of Quantum, and the overall PixelPacket size.

using readable pixeldata from an Image

Type

convert -version
Is your version Q16? If so there are 2 bytes per pixel component. You can add -quantum-depth=8 to your configure script command line and rebuild and reinstall ImageMagick. That produces a Q8 version of ImageMagick where each pixel component is 8-bits (0..255).

So it looks like we're both using ImageMagick built for two bytes per pixel (Q16).

@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