示例#1
0
 def toPILImage(self):
     item = self
     if item.image:
         return item.image
     elif item.template:
         from PIL.Image import frombuffer
         if item.tmplWidget:  #it has been modified
             tmplWidget = item.tmplWidget
         else:
             from template import BGTemplate
             Logger.info('[SGM] toPILIMage calling fromfile ')
             tmplWidget = BGTemplate.FromFile(item.template)
             if tmplWidget:
                 #only taking the last one
                 tmplWidget = tmplWidget[-1]
             else:
                 raise NameError('No such template: ' + item.template)
             print 'here to be added: adding on realizer, exporting & then removing. more tricky'
             if item.values:
                 tmplWidget.apply_values(item.values)
         cim = tmplWidget.toImage(for_print=True)
         pim = frombuffer('RGBA', cim.size, cim._texture.pixels, 'raw',
                          'RGBA', 0, 1)
     else:
         from PIL import Image
         pim = Image.open(self.source)
     return pim
示例#2
0
 def toPILImage(self):
     item = self
     if item.image:
         return item.image
     elif item.template:
         from PIL.Image import frombuffer
         if item.tmplWidget:#it has been modified
             tmplWidget = item.tmplWidget
         else:
             from template import BGTemplate
             Logger.info( '[SGM] toPILIMage calling fromfile ')
             tmplWidget = BGTemplate.FromFile(item.template)
             if tmplWidget:
                 #only taking the last one
                 tmplWidget = tmplWidget[-1]
             else:
                 raise NameError('No such template: '+ item.template)
             print 'here to be added: adding on realizer, exporting & then removing. more tricky'
             if item.values:
                 tmplWidget.apply_values(item.values)
         cim = tmplWidget.toImage(for_print=True)
         pim = frombuffer('RGBA', cim.size, cim._texture.pixels, 'raw', 'RGBA',0,1)
     else:
         from PIL import Image
         pim = Image.open(self.source)
     return pim
示例#3
0
    def to_PIL(self) -> 'PIL_Image':
        """Convert the given frame into a PIL image.

        Requires Pillow to be installed.
        """
        self.load()

        from PIL.Image import frombuffer
        return frombuffer(
            'RGBA',
            (self.width, self.height),
            self._data,
            'raw',
            'RGBA',
            0,
            1,
        ).copy()
示例#4
0
    def to_pil(self):
        if self._cached_img is not None:
            return self._cached_img

        size, format, raw = yield self.get_raw_data_async()
        raw = memoryview(raw)

        index = 0
        planes = []
        for i in range(format.num_planes):
            plane = self.plane_size(i)
            planedata = raw[index:index+plane]
            planes.append(frombuffer('L', size, planedata, 'raw', "L", 0, 1))
            index += plane

        pil_format = "RGB"
        if format.num_planes == 4:
            pil_format += "A"
        return merge(pil_format, planes)
示例#5
0
    def _scale_down(self, handle, ratio):
        xsize, ysize = self.size
        if ratio >= 1.0:
            # Convert
            surface = ImageSurface(FORMAT_ARGB32, xsize, ysize)
            ctx = Context(surface)
        else:
            # Scale
            xsize, ysize = int(xsize * ratio), int(ysize * ratio)
            surface = ImageSurface(FORMAT_ARGB32, xsize, ysize)
            ctx = Context(surface)
            ctx.scale(ratio, ratio)

        # Render
        handle.render_cairo(ctx)

        # Transform to a PIL image for further manipulation
        size = (xsize, ysize)
        im = frombuffer('RGBA', size, surface.get_data(), 'raw', 'BGRA', 0, 1)
        surface.finish()

        return im, xsize, ysize
示例#6
0
文件: image.py 项目: hforge/itools
    def _scale_down(self, handle, ratio):
        xsize, ysize = self.size
        if ratio >= 1.0:
            # Convert
            surface = ImageSurface(FORMAT_ARGB32, xsize, ysize)
            ctx = Context(surface)
        else:
            # Scale
            xsize, ysize = int(xsize * ratio), int(ysize * ratio)
            surface = ImageSurface(FORMAT_ARGB32, xsize, ysize)
            ctx = Context(surface)
            ctx.scale(ratio, ratio)

        # Render
        handle.render_cairo(ctx)

        # Transform to a PIL image for further manipulation
        size = (xsize, ysize)
        im = frombuffer('RGBA', size, surface.get_data(), 'raw', 'BGRA', 0, 1)
        surface.finish()

        return im, xsize, ysize
示例#7
0
    def generation_step(self, with_realize=False):
        "Will be called in order to show some advancement"
        i, row, col, face, item = self.index.pop()

        #Determine wheter a new page should be appended

        face_index = 0 if self.current_face == 'F' else 1
        if self.current_face != face:
            self.current_face = face
            self.current_page_num[1 - face_index] = i
            self.AddPage()
        elif self.current_page_num[face_index] != i:
            self.current_page_num[face_index] = i
            self.AddPage()

        #Determine Image X/Y/W/H depending on print mode
        if self.mode == 'LAYOUT':
            x, y, self.x, self.y, angle, pageindex = item.layout
            x /= cm(1)
            y /= cm(1)
            self.x /= cm(1)
            self.y /= cm(1)
        elif self.mode == 'BINCAN':
            x, y, self.x, self.y, angle, item = item  #hackick: replace item in place !
            x /= cm(1)
            y /= cm(1)
            self.x /= cm(1)
            self.y /= cm(1)
        else:
            if face == 'F':
                x, y = col * self.x + left, height - (1 + row) * self.y - top
            else:
                x, y = width - (1 + col) * self.x - left - right, height - (
                    1 + row) * self.y - top
                #x, y = width - (1+col)*self.x - right, height-(1+row)*self.y - top
            #self.x & slef.y has already been setp by calculate_size
            #Check is there is a layout that could be used, just for the angle
            if getattr(item, 'layout', 0):
                angle = item.layout[4]
            else:
                angle = 0

        #Now that in item lies the Stack Item, before rendering it, inject special print vairables
        item.print_index = {
            'pagenum': i,
            'stackrow': row,
            'stackcol': col,
            'pageface': face
        }

        # Now, define source for image: we either get the source or convert to image
        if item.image:  #speicla case for complex image manip

            src = ImageReader(item.image.rotate(angle))
        elif item.template:
            if with_realize:
                item.realise(True, True)
                tmplWidget = item.tmplWidget
            elif item.tmplWidget:  #it has been modified
                tmplWidget = item.tmplWidget
            else:
                from template import BGTemplate
                Logger.info('[Printer] Generation Step without tmplWidget')
                tmplWidget = BGTemplate.FromFile(item.template)
                if tmplWidget:
                    #only taking the last one
                    tmplWidget = tmplWidget[-1]
                else:
                    raise NameError('No such template: ' + item.template)
                if item.values:
                    tmplWidget.apply_values(item.values)
                from kivy.base import EventLoop
                EventLoop.idle()
            cim = tmplWidget.toImage(for_print=True)
            pim = frombuffer('RGBA', cim.size, cim._texture.pixels, 'raw',
                             'RGBA', 0, 1)
            src = ImageReader(pim.rotate(angle))
        else:
            src = item.source
            from utils import find_path
            src = find_path(src)
            if angle:
                src = ImageReader(PILOpen(src).rotate(angle))

        #print "Adding Image to pdf", i, row, col, face, item, src, x, y, self.x, self.y, angle
        self.pdf.drawImage(src,
                           x * r_cm,
                           y * r_cm,
                           self.x * r_cm,
                           self.y * r_cm,
                           mask='auto')
        from conf import CP
        if CP.getboolean('Print', 'draw_cut_rect'):
            #add line after image: they ll be above
            self.AddLines(x, y, self.x, self.y)
示例#8
0
def getPILimg(im):
    sz = im.shape;
    return frombuffer("RGB", [sz[1], sz[0]], im, "raw", "RGB", 0, 1);
示例#9
0
    def generation_step(self, with_realize = False):
        "Will be called in order to show some advancement"
        i, row, col, face, item = self.index.pop()

        #Determine wheter a new page should be appended

        face_index = 0 if self.current_face == 'F' else 1
        if self.current_face != face:
            self.current_face = face
            self.current_page_num[1-face_index] = i
            self.AddPage()
        elif self.current_page_num[face_index] != i:
            self.current_page_num[face_index] = i
            self.AddPage()


        #Determine Image X/Y/W/H depending on print mode
        if self.mode == 'LAYOUT':
            x, y, self.x, self.y, angle, pageindex = item.layout
            x /= cm(1)
            y /= cm(1)
            self.x /= cm(1)
            self.y /= cm(1)
        elif self.mode == 'BINCAN':
            x, y, self.x, self.y, angle, item = item #hackick: replace item in place !
            x /= cm(1)
            y /= cm(1)
            self.x /= cm(1)
            self.y /= cm(1)
        else:
            if face == 'F':
                x, y = col * self.x + left, height-(1+row)*self.y - top
            else:
                x, y = width - (1+col)*self.x - left - right, height-(1+row)*self.y - top
                #x, y = width - (1+col)*self.x - right, height-(1+row)*self.y - top
            #self.x & slef.y has already been setp by calculate_size
            #Check is there is a layout that could be used, just for the angle
            if getattr(item, 'layout',0):
                angle = item.layout[4]
            else:
                angle = 0

        #Now that in item lies the Stack Item, before rendering it, inject special print vairables
        item.print_index = {
            'pagenum' : i,
            'stackrow': row,
            'stackcol': col,
            'pageface': face
        }

        # Now, define source for image: we either get the source or convert to image
        if item.image:#speicla case for complex image manip

            src = ImageReader(item.image.rotate(angle))
        elif item.template:
            if with_realize:
                item.realise(True,True)
                tmplWidget = item.tmplWidget
            elif item.tmplWidget:#it has been modified
                tmplWidget = item.tmplWidget
            else:
                from template import BGTemplate
                Logger.info( '[Printer] Generation Step without tmplWidget')
                tmplWidget = BGTemplate.FromFile(item.template)
                if tmplWidget:
                    #only taking the last one
                    tmplWidget = tmplWidget[-1]
                else:
                    raise NameError('No such template: '+ item.template)
                if item.values:
                    tmplWidget.apply_values(item.values)
                from kivy.base import EventLoop
                EventLoop.idle()
            cim = tmplWidget.toImage(for_print=True)
            pim = frombuffer('RGBA', cim.size, cim._texture.pixels, 'raw', 'RGBA',0,1)
            src = ImageReader(pim.rotate(angle))
        else:
            src = item.source
            from utils import find_path
            src = find_path(src)
            if angle:
                src = ImageReader(PILOpen(src).rotate(angle))

        #print "Adding Image to pdf", i, row, col, face, item, src, x, y, self.x, self.y, angle
        self.pdf.drawImage(src, x*r_cm, y*r_cm, self.x*r_cm, self.y*r_cm, mask='auto')
        from conf import CP
        if CP.getboolean('Print','draw_cut_rect'):
            #add line after image: they ll be above
            self.AddLines(x,y,self.x,self.y)
示例#10
0
def getPILimg(im):
    sz = im.shape
    return frombuffer("RGB", [sz[1], sz[0]], im, "raw", "RGB", 0, 1)
示例#11
0
 def toPILImage(self,bg_color=(1,1,1,0), for_print = False):
     from PIL.Image import frombuffer
     cim = self.toImage(bg_color, for_print)
     return frombuffer('RGBA', cim.size, cim._texture.pixels, 'raw', 'RGBA', 0, 1)