Permalink
Browse files

Add brightness_contrast option

  • Loading branch information...
1 parent 54f169d commit 1307671a955f82df71a565fb9783a4569f39583f @fengfengqi fengfengqi committed Oct 24, 2012
Showing with 56 additions and 7 deletions.
  1. +27 −0 README.md
  2. +18 −7 handlers.py
  3. +11 −0 magick.py
View
27 README.md
@@ -115,6 +115,33 @@ Full list of options supported by default:
Format to convert the image into. Defaults to jpeg.
png16 is 24-bit png pre-dithered for 16-bit (RGB555) screens.
+ &normalize=1
+ Histogram-based contrast increase. It passes the -normalize operator to ImageMagick.
+ The top two percent of the dark pixels will become black and the top one percent of the
+ light pixels will become white. The contrast of the rest of the pixels are maximized.
+ All the channels are normalized together to avoid color shift, so pure black or white
+ may not exist in the final image. Defaults to 0, which won't chain any operator to the
+ ImageMagick command.
+
+ &equalize=1
+ Histogram-based colour redistribution. It passes the -equalize operator to ImageMagick,
+ following the -normalize operator. It redistributes the colour of the image according to
+ uniform distribution. Each channel are changed independently, and color shift may happen.
+ Default to 0, which won't chain any operator to the ImageMagick command.
+
+ &contrast_stretch=axb
+ Histogram-based contrast adjustment. It passes the -contrast-stretch a%xb% operator to
+ ImageMagick, following the -equalize operator. The top a percent of the dark pixels will
+ become black and the top b percent of the light pixels will become white. The contrast
+ of the rest of the pixels are maximized. All the channels are normalized together to avoid
+ color shift, so pure black or white may not exist in the final image. Defaults to None, which
+ won't chain any operator to the ImageMagick command.
+
+ &brightness_contrast=cxd
+ Amplify brightness and contrast by percentages. It passes the -brightness-contrast c%xd%
+ operator to ImageMagick, following the -contrast-stretch operator. Defaults to None, which
+ won't chain any operator to the ImageMagick command.
+
Examples
==========
View
25 handlers.py
@@ -122,16 +122,21 @@ def calculate_options(self):
&equalize=1
Histogram-based colour redistribution. It passes the -equalize operator to ImageMagick,
- behind the -normalize operator. It redistributes the colour of the image according to
+ following the -normalize operator. It redistributes the colour of the image according to
uniform distribution. Each channel are changed independently, and color shift may happen.
Default to 0, which won't chain any operator to the ImageMagick command.
&contrast_stretch=axb
Histogram-based contrast adjustment. It passes the -contrast-stretch a%xb% operator to
- ImageMagick, behand the -equalize operator. The top a percent of the dark pixels will
+ ImageMagick, following the -equalize operator. The top a percent of the dark pixels will
become black and the top b percent of the light pixels will become white. The contrast
of the rest of the pixels are maximized. All the channels are normalized together to avoid
- color shift, so pure black or white may not exist in the final image. Defaults to 0, which
+ color shift, so pure black or white may not exist in the final image. Defaults to None, which
+ won't chain any operator to the ImageMagick command.
+
+ &brightness_contrast=cxd
+ Amplify brightness and contrast by percentages. It passes the -brightness-contrast c%xd%
+ operator to ImageMagick, following the -contrast-stretch operator. Defaults to None, which
won't chain any operator to the ImageMagick command.
"""
@@ -152,6 +157,7 @@ def calculate_options(self):
normalize = int(self.get_argument("normalize", 0)) == 1
equalize = int(self.get_argument("equalize", 0)) == 1
contrast_stretch = self.parse_2d_param(self.get_argument("contrast_stretch", None))
+ brightness_contrast = self.parse_2d_param(self.get_argument("brightness_contrast", None))
# size=&maintain_ratio=&crop=&crop_anchor=
if size:
@@ -191,19 +197,24 @@ def calculate_options(self):
if reflection_height:
magick.reflect(reflection_height, top, bottom)
- # normalize=1
+ # normalize=
if normalize:
magick.normalize()
- # equalize=1
+ # equalize=
if equalize:
magick.equalize()
- # contrast_stretch=axb
+ # contrast_stretch=
if contrast_stretch:
- a, b = contrast_stretch
+ (a, b) = contrast_stretch
magick.contrast_stretch(a, b)
+ # brightness_contrast=
+ if brightness_contrast:
+ (c, d) = brightness_contrast
+ magick.brightness_contrast(c, d)
+
magick.format = magick.JPEG
format_param = self.get_argument("format", "").lower()
if format_param[0:3] == "png":
View
11 magick.py
@@ -250,6 +250,17 @@ def contrast_stretch(self, a, b, prepend=False):
opt = ['-contrast-stretch', white_and_black_point]
self._chain_op(name, opt, prepend)
+ def brightness_contrast(self, a, b, prepend=False):
+ """
+ Add the -brightness-contrast a%xb% operator. a and b represent the percentage change
+ of brighteness and contrast, respectively.
+ a and b are expected to be integers.
+ """
+ brightness_and_contrast = "%d%%x%d%%" % (a, b)
+ name = "brightness_contrast_%d_%d" % (a, b)
+ opt = ['-brightness-contrast', brightness_and_contrast]
+ self._chain_op(name, opt, prepend)
+
def rgb555_dither(self, _colormap=None):
"""
Reduce color channels to 5-bit by dithering, preserving Alpha channel.

0 comments on commit 1307671

Please sign in to comment.