Commits

squeaky committed 8a00dc9

apply background when saving to nontranspanret formats

Comments (0)

Files changed (4)

 TODO
 ====
 
-
-- from_hsl, get_int24
+- fit method [0.1.1]
 - weakrefing cached lena should be only used in tests [0.1.1]
 - investigate __all__ of main module, image and color
 - i.e. registry module to use for paths, chainability and any other things
   that affect runtime
 - implement complex threaded test with many operations
+- report get range error
   -------------------------
+- compile with xz, bzip and gzip on all oses
 - Jython support
 - move more things to registry - paths, and chainability
 - instead of relying on renames and get_platfom we could use our own solution [0.1.1]
   into one class with many test methods [0.1.1]
 - compress method that let's you apply compression and reread image, useful for
   preving compression result
-- fit method [0.1.1]
 - set_pixel (possible?) [0.1.1]
 - keep track of showed temp files and delete them on exit
 - rgb8, rgba8 color factory, and accessors [0.1.1]

pystacia/api/func.py

     if init:
         get_dll()
     
-    msg = formattable('Calling {0}')
-    logger.debug(msg.format(method_name))
-    
     if isinstance(obj, Resource):
         args = (obj,) + args
     
+    # if objects are casted here and then
+    # there is only their resource passed
+    # there is a risk that GC will collect
+    # them and __del__ will be called between
+    # driving Imagick to SIGSEGV
+    # lets keep references to them
+    keep_ = []
     args_ = []
     for arg, type in zip(args, c_method.argtypes):  # @ReservedAssignment
-        if isinstance(arg, Resource):
-            arg = arg.resource
-        elif type == c_char_p:
+        if type == c_char_p:
             arg = b(arg)
         elif type == PixelWand_p:
             arg = color_cast(arg)
-            
+            keep_.append(arg)
+        
+        if isinstance(arg, Resource):
+            arg = arg.resource
+        
         args_.append(arg)
     
+    msg = formattable('Calling {0}')
+    logger.debug(msg.format(method_name))
+    
     result = c_method(*args_)
     
     if c_method.restype == c_char_p:

pystacia/image/__init__.py

         return value
     
     def write(self, filename, format=None,  # @ReservedAssignment
-              compression=None, quality=None):
+              compression=None, quality=None, flatten=None, background=None):
         """Write an image to filesystem.
            
            :param filename: file name to write to.
            
            This method can be chained.
         """
-        call(io.write, self, filename, format, compression, quality)
+        call(io.write, self, filename, format, compression,
+             quality, flatten, background)
     
     def get_blob(self, format, compression=None,  # @ReservedAssignment
                  quality=None, factory=None):
             setattr(Image, key, chainable(item))
 
 
+from pystacia.image.generic import blank  # prevent circular references
 from pystacia.image._impl import (io, geometry, color as color_impl,
                                  blur as blur_impl, deform, special, pixel)
 
 from pystacia.image.enum import (types, filters, colorspaces,  # @UnusedImport
                                  compressions, composites, axes, noises,
                                  thresholds)
-from pystacia.image.generic import (blank, checkerboard, noise,  # @UnusedImport
+from pystacia.image.generic import (checkerboard, noise,  # @UnusedImport
                                     plasma)
 from pystacia.image.sample import (lena, magick_logo, rose,  # @UnusedImport
                                    wizard, granite, netscape)

pystacia/image/_impl/io.py

 
 
 def write(image, filename, format,  # @ReservedAssignment
-              compression, quality):
+              compression, quality, flatten, background):
+    if not format:
+        format = splitext(filename)[1][1:]  #@ReservedAssignment
+        
+    if flatten == None:
+        flatten = format in ('jpg', 'jpeg')
+        
+    if not background:
+        background = 'white'
+    
+    if flatten:
+        background = blank(image.width, image.height, background)
+        background.overlay(image)
+        image = background
+        
     with state(image, format=format, compression_quality=quality):
         c_call(image, 'write', filename)
 
         
         return blob
 
-
+from os.path import splitext
 from ctypes import c_size_t, string_at
 
 from pystacia.common import state
 from pystacia.image import _instantiate
+from pystacia.image.generic import blank
 from pystacia.api.func import c_call