Exemple #1
0
    def printWtRotNScl(self, leakdDeg):
        bg = self.spotter.renderIm().convert('RGBA')
        self.draw = ImageDraw.Draw(bg)
        lineOriginY = self.formatter.marginTop
        lineOriginX = self.formatter.marginLeft
        boxOriginX = lineOriginX
        boxOriginY = lineOriginY
        leakVerOffset = 10
        for line in textwrap.wrap(self.text, width=self.formatter.char_num_per_line):
            line = line.replace(' ', '')
            boxOriginX = lineOriginX
            for char in line:

                box = self.formatter.font.getsize(char)
                charWidth = box[0]
                charHeight = box[1]
                charIm = _util.getTransparentImage(charWidth, charHeight)
                charDr = ImageDraw.Draw(charIm)
                charDr.text((0,-10), char, self.formatter.textColor, font=self.formatter.font)
                # Crop / Ink leak down
                offset = self.formatter.font.getoffset(char)
                cropPos = _util.getCropPos(box, offset, leakdDeg)
                pastePos = _util.getPastePos(box, offset, leakVerOffset)
                cropped = charIm.crop(cropPos)
                charIm.paste(cropped, pastePos)

                # Rotate and scale
                scaleW = _util.randomfWith(1.0,1.2)
                scaleH = _util.randomfWith(1.0,1.2)
                charIm = charIm.rotate(0, expand=1).resize((int(charWidth), int(charHeight)))
                bg.paste(charIm, (boxOriginX, boxOriginY), charIm)

                boxOriginX += charWidth
            boxOriginY += self.formatter.lineHeight
        _util.saveImage(bg, fullpath=(self.path+'_lkg'+'_lkd' + '.png'))
        return 'created/' + self.filename + '_lkg' + '_lkd' + '.png'
Exemple #2
0
    def initImageWithRotationScaleLeakDown(self,bg):
        leakVerOffset = 10
        leakHeight = self.leakHeight  # delta y
        output = []
        lineOriginY = self.formatter.marginTop
        lineOriginX = self.formatter.marginLeft
        
        for line in textwrap.wrap(self.text, width=self.formatter.char_num_per_line):
            # line = line.replace(' ', '')
            self.canvasDr.text((lineOriginX, lineOriginY), line, self.formatter.textColor, font=self.formatter.font)
            charOriginX = lineOriginX
            charOriginY = lineOriginY
            for char in line:
                charSize = self.formatter.font.getsize(char)
                charWidth = charSize[0]
                charHeight = charSize[1]
                charOffset  = self.formatter.font.getoffset(char)
                charOrigin = (charOriginX, charOriginY)
                charLfUp = [charOff_i + charOri_i for charOff_i, charOri_i in zip(charOffset, charOrigin)]
                    # Change it to list
                charLfUp = (charLfUp[0], charLfUp[1])
                charRtLo = [charLfUp_i + charSize_i for charLfUp_i, charSize_i in zip(charLfUp, charSize)]
                    # Change it to list
                charRtLo = (charRtLo[0], charRtLo[1])

                # # Draw debugging rectangle
                # bgD = ImageDraw.Draw(bg)
                # bgD.rectangle((charLfUp,charRtLo), outline='red')


                # Ink Leak Down
                cropBox = _util.getLeakDownCropBox(charLfUp,charSize,leakHeight)
                pastePos = _util.getLeakDownPastePosition(charLfUp,charSize,leakHeight,leakVerOffset)

                cropped = self.canvas.crop(cropBox)
                self.canvas.paste(cropped, pastePos, cropped)

                # Rotate and Scale
                scaleW = _util.randomfWith(1.0,1.2)
                scaleH = _util.randomfWith(1.0,1.2)
                    # Add the leak into the char box
                charRtLo = (charRtLo[0], charRtLo[1] + leakVerOffset)
                    # Crop the extended box
                cropBox = (charLfUp[0],charLfUp[1],charRtLo[0],charRtLo[1])
                cropped = self.canvas.crop(cropBox)
                cropped_rot = cropped.rotate(0, expand=1)
                # pastePos = _util.getPostRotatedPastePosition()
                pastePos = (charLfUp[0],charLfUp[1])

                # Paste the leaked, rotated character to the background
                bg.paste(cropped_rot, pastePos, cropped_rot)


                # Append coordinates to output
                    # Adapt coordinates to tesseract coordinates
                tessLfUp = _util.convertCoordinate(charLfUp, _util.size_training)
                tessRtLo = _util.convertCoordinate(charRtLo, _util.size_training)
                output.append(char + ' ' + 
                        str(tessLfUp[0]) + ' ' +  # left
                        str(tessRtLo[1]) + ' ' + # bottom
                        str(tessRtLo[0]) + ' ' + # right
                        str(tessLfUp[1]) + ' ' + # top
                        str(0)) # page

                charOriginX += charWidth
            lineOriginY += self.formatter.lineHeight
        return bg