예제 #1
0
#
#   lbike.py main program started from /etc/rc.local 
##


import os
import subprocess
import wand
from wand.image import Image

# CONFIG
basedir = os.path.dirname(os.path.realpath(__file__))   


def callSubprocess(cmd):
      subprocess.call(cmd.split())

       
with Image(filename=basedir+'/tmp/qr.png') as qr:
      qr.transform(resize='176x264')
      qr.save(filename=basedir+'/tmp/qr.png')
      with Image(filename=basedir+'/img/pay_invoice_176x264.png') as bg:
              bg.composite(qr, 1, 1)
              bg.save(filename=basedir+'/tmp/qr_176x264.png')

      callSubprocess('convert '+basedir+'/tmp/qr_176x264.png  -type Palette -depth 2 -compress none bmp3:'+basedir+'/tmp/qr_176x264.bmp')
      


          
예제 #2
0
def get_image(tsf_buff, headers):
    m = bmp_re.search(tsf_buff)
    if(m):
        return Image(blob="BM" + m.group(2))
    else:
        return Image(width=mm2px(headers['Resolution'], headers['Size']['width']), height=mm2px(headers['Resolution'], headers['Size']['height']))
예제 #3
0
# Android - Supporting multiple screens
# http://developer.android.com/guide/practices/screens_support.html

MANIFEST = (
    # dpi, ratio
    ('h', .75),  # high-dpi
    ('m', .5),  # medium-dpi
    ('l', .375)  # low-dpi
)

if __name__ == "__main__":
    # We start from here (2.0x size images)
    files = os.listdir("res/drawable-xhdpi/")

    # Filter out non-desired files
    files = filter(lambda x: re.match(r"^\w+(.jpg|.png)$", x) is not None,
                   files)

    for filename in files:
        with Image(filename="res/drawable-xhdpi/%s" % filename) as img:

            print("Resizing %s..." % filename)

            width, height = img.size

            for dpi, ratio in MANIFEST:
                cimg = img.clone()
                cimg.resize(int(width * ratio), int(height * ratio))
                cimg.save(filename="res/drawable-%sdpi/%s" % (dpi, filename))
예제 #4
0
 def load_image(self, load_processed=False):
     if not self.image:
         filename = self.processed_filename if load_processed else self.filename
         self.image = Image(filename=Path('scans') / filename)
예제 #5
0
def create_asset(file_data,
                 friendly_name,
                 tags='',
                 url_path='',
                 optimize=False):
    data = {"optimized": optimize}

    if optimize:
        try:
            image = ImageProcessor(file_data)
            image.optimize(allow_svg_errors=True)
            file_data = image.data
        except:
            # If optimisation failed, just don't bother optimising
            data["optimized"] = "Optimization failed"

    if not url_path:
        url_path = settings.FILE_MANAGER.generate_asset_path(
            file_data, friendly_name)

    try:
        with Image(blob=file_data) as image_info:
            data['width'] = image_info.width
            data['height'] = image_info.height
    except:
        # Just don't worry if image reading fails
        pass

    # Create file
    if not settings.DATA_MANAGER.exists(url_path):
        try:
            settings.FILE_MANAGER.create(file_data, url_path)
        except:
            raise file_error(
                error_number=errno.EEXIST,
                message="Failed to create asset {0}.".format(url_path),
                filename=url_path)
    else:
        error_message = "Asset already exists at {0}.".format(url_path)
        asset = settings.DATA_MANAGER.fetch_one(url_path)

        image_data = {}

        if "width" not in asset and "width" in data:
            image_data["width"] = data["width"]

        if "height" not in asset and "height" in data:
            image_data["height"] = data["height"]

        if image_data:
            settings.DATA_MANAGER.update(url_path, asset['tags'], image_data)
            error_message += " Updated image information."

        raise file_error(error_number=errno.EEXIST,
                         message=error_message,
                         filename=url_path)

    # Once the file is created, create file metadata
    settings.DATA_MANAGER.update(url_path, tags, data)

    return url_path
예제 #6
0
def overlay():
    with Image(filename="booth_background.png") as overlay:
        with Image(filename="0.jpg") as underlay:
            underlay.composite_channel('default_channels', overlay, 'over', 0,
                                       0)
            underlay.save(filename="composited.jpg")
예제 #7
0
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color

# http://www.imagemagick.org/Usage/draw/#arcs

w = 100
h = 60
bgcolor = Color('skyblue')

# original imagemagick command:
#  convert -size 100x60 xc:skyblue -fill white -stroke black \
#          -draw "path 'M 30,40   A 3,2  45  0,0 70,20'" path_arc_x.gif

with Image(width=w, height=h, background=bgcolor) as img:
    with Drawing() as draw:
        draw.fill_color = Color('white')
        draw.stroke_color = Color('black')
        draw.path_start()
        draw.path_move((30, 40))
        draw.path_elliptic_arc(radius=(3, 2), to=(70, 20), rotation=45)
        draw.path_finish()
        draw(img)
    img.save(filename='sample23a.png')

# convert -size 100x60 xc:skyblue -fill white -stroke black \
#         -draw "path 'M 30,40   A 1,1  0  0,0 70,20'" path_hcircle.gif

with Image(width=w, height=h, background=bgcolor) as img:
    with Drawing() as draw:
def draw_address_layout(position, element, text=None):
    global qr_png_path
    logger.debug('[draw_address_layout()]-qr_png_path: ' + qr_png_path)

    try:
        # 8 positions available on print layout
        # Position argument is an integer from 1-8
        if position == 1:
            x_qr = addr_features_qr['left_x']
            y_qr = addr_features_qr['row_one_y']

            x_addr = addr_features_addr['column_four_x']
            y_addr = addr_features_addr['top_y']

            x_label = addr_features_label['left_x']
            y_label = addr_features_label['row_one_y']

        elif position == 2:
            x_qr = addr_features_qr['right_x']
            y_qr = addr_features_qr['row_one_y']

            x_addr = addr_features_addr['column_four_x']
            y_addr = addr_features_addr['bottom_y']

            x_label = addr_features_label['right_x']
            y_label = addr_features_label['row_one_y']

        elif position == 3:
            x_qr = addr_features_qr['left_x']
            y_qr = addr_features_qr['row_two_y']

            x_addr = addr_features_addr['column_three_x']
            y_addr = addr_features_addr['top_y']

            x_label = addr_features_label['left_x']
            y_label = addr_features_label['row_two_y']

        elif position == 4:
            x_qr = addr_features_qr['right_x']
            y_qr = addr_features_qr['row_two_y']

            x_addr = addr_features_addr['column_three_x']
            y_addr = addr_features_addr['bottom_y']

            x_label = addr_features_label['right_x']
            y_label = addr_features_label['row_two_y']

        elif position == 5:
            x_qr = addr_features_qr['left_x']
            y_qr = addr_features_qr['row_three_y']

            x_addr = addr_features_addr['column_two_x']
            y_addr = addr_features_addr['top_y']

            x_label = addr_features_label['left_x']
            y_label = addr_features_label['row_three_y']

        elif position == 6:
            x_qr = addr_features_qr['right_x']
            y_qr = addr_features_qr['row_three_y']

            x_addr = addr_features_addr['column_two_x']
            y_addr = addr_features_addr['bottom_y']

            x_label = addr_features_label['right_x']
            y_label = addr_features_label['row_three_y']

        elif position == 7:
            x_qr = addr_features_qr['left_x']
            y_qr = addr_features_qr['row_four_y']

            x_addr = addr_features_addr['column_one_x']
            y_addr = addr_features_addr['top_y']

            x_label = addr_features_label['left_x']
            y_label = addr_features_label['row_four_y']

        elif position == 8:
            x_qr = addr_features_qr['right_x']
            y_qr = addr_features_qr['row_four_y']

            x_addr = addr_features_addr['column_one_x']
            y_addr = addr_features_addr['bottom_y']

            x_label = addr_features_label['right_x']
            y_label = addr_features_label['row_four_y']

        if element == 'qr':
            logger.info('Adding QR code to address card overlay.')
            # QR Code
            with Image(filename=addr_file) as layout:
                with Image(filename=qr_png_path) as img:
                    img.resize(addr_features_qr['square_elements'][0],
                               addr_features_qr['square_elements'][1])
                    layout.composite(img, left=x_qr, top=y_qr)
                    layout.save(filename=addr_file)

        elif element == 'address':
            logger.info('Adding public address to address card overlay.')
            # Public Address
            with Drawing() as draw:
                draw.font_family = 'Ubuntu'
                draw.font_size = addr_features_addr['font_size']
                draw.text_alignment = 'center'
                draw.text(x_addr, y_addr, text)
                with Image(filename=addr_file) as layout:
                    layout.rotate(90)
                    draw.draw(layout)
                    layout.rotate(270)
                    layout.save(filename=addr_file)

        elif element == 'label':
            logger.info('Adding label to address card overlay.')
            # Label
            with Drawing() as draw:
                draw.font_family = 'Ubuntu'
                draw.font_style = 'oblique'
                draw.font_size = addr_features_label['font_size']
                draw.text_alignment = 'left'
                draw.text(x_label, y_label, text)
                with Image(filename=addr_file) as layout:
                    draw.draw(layout)
                    layout.save(filename=addr_file)

    except Exception as e:
        logger.exception('Exception while drawing info on address layout.')
        logger.exception(e)
        raise
예제 #9
0
def main(argv):

    parser = argparse.ArgumentParser(
        description=
        'Extract images from MS Word .docx file(s) and save in JPEG format')

    parser.add_argument("infiles",
                        metavar="file",
                        type=argparse.FileType(),
                        nargs="+",
                        help="input .docx file(s)")

    parser.add_argument(
        "-o",
        "--outdir",
        type=Path,
        #default = Path(__file__).absolute().parent,
        default=Path.cwd(),
        help="output directory for image files")

    args = parser.parse_args()

    # check for directory existence and create/error out as needed
    if args.outdir.exists() and not args.outdir.is_dir():

        print("output directory name already exists as a file, please rename")
        exit(1)

    elif not args.outdir.exists():
        args.outdir.mkdir()

    for infile in args.infiles:

        try:
            indocx = zf.ZipFile(infile.name)

        except zf.BadZipfile:
            print(infile.name, "is an invalid / corrupt .docx file")
            exit(1)

        for f in indocx.namelist():
            if "word/media/" in f:
                with indocx.open(f) as mediafile:
                    print("processing", mediafile.name, "from", infile.name,
                          "...")

                    # Rename file w/format: x-image-uuid.jpeg as x in x.docx:
                    outfile = args.outdir / str(infile.name + "-image-" +
                                                str(uuid.uuid4()) + ".jpeg")

                    # test if jpeg format and one-to-one copy:
                    if (".jpeg" or ".jpg") in f:
                        print("writing to", outfile)

                        # pathlib.Path.write_bytes takes care of opening & closing file...
                        outfile.write_bytes(mediafile.read())

                    elif ".png" in f:
                        print("converting .png to", outfile)

                        im = Image(file=mediafile)
                        im.format = 'jpeg'
                        outfile.write_bytes(im.make_blob())

                    elif ".emf" in f:
                        print("converting .emf to", outfile)

                        # need to use unoconv, then ImageMagick, also fu@$!in tempfiles again!
                        tmp = args.outdir / "tmp_file.emf"
                        tmp.write_bytes(mediafile.read())

                        pdf = subprocess.run([
                            "unoconv", "--format=pdf", "--stdout",
                            tmp.resolve()
                        ],
                                             capture_output=True,
                                             check=True)

                        jpeg = subprocess.run([
                            "convert", "-density", "150", "-trim",
                            "-bordercolor", "white", "-border", "5", "-",
                            "jpeg:-"
                        ],
                                              capture_output=True,
                                              input=pdf.stdout,
                                              check=True)

                        outfile.write_bytes(jpeg.stdout)
                        tmp.unlink()

                    elif ".wmf" in f:
                        print("converting .wmf to", outfile)

                        # need to use unoconv, then ImageMagick, also fu@$!in tempfiles again!
                        tmp = args.outdir / "tmp_file.wmf"
                        tmp.write_bytes(mediafile.read())

                        pdf = subprocess.run([
                            "unoconv", "--format=pdf", "--stdout",
                            tmp.resolve()
                        ],
                                             capture_output=True,
                                             check=True)

                        jpeg = subprocess.run([
                            "convert", "-density", "150", "-trim",
                            "-bordercolor", "white", "-border", "5", "-",
                            "jpeg:-"
                        ],
                                              capture_output=True,
                                              input=pdf.stdout,
                                              check=True)

                        outfile.write_bytes(jpeg.stdout)
                        tmp.unlink()

                    elif ".wdp" in f:
                        print("converting .wdp to", outfile)

                        #ImageMagick JXR handler needs an actual file; won't take bytestream...
                        tmp = args.outdir / "tmp_file.wdp"
                        tmp.write_bytes(mediafile.read())

                        im = Image(filename=tmp.resolve())
                        im.format = 'jpeg'

                        #im.save(filename=outfile.name)
                        outfile.write_bytes(im.make_blob())

                        #delete tmp_file.wdp file
                        tmp.unlink()

                    else:
                        print(mediafile.name, "is not a recognized image type")

                    mediafile.close()
import datetime
import os
from wand.image import Image

PDF_SOURCE = '2018_code_calendar.pdf[{}]'
BACKGROUND_SOURCE = 'paper.jpg'

PAGE_OFFSET = 6
MARGIN_LEFT = 100
MARGIN_TOP = 10

current_week = datetime.datetime.now().isocalendar()[1]
OUTPUT = 'turing' + str(current_week) + '.jpg'
page = PAGE_OFFSET + current_week

with Image(filename=PDF_SOURCE.format(page), resolution=160) as calendar:
    with Image(filename=BACKGROUND_SOURCE) as background:
        background.composite_channel('default_channels', calendar, 'blend',
                                     MARGIN_LEFT, MARGIN_TOP)
        background.save(filename=OUTPUT)

address = os.getcwd() + "/" + OUTPUT
os.system(
    "osascript -e \"tell application \\\"Finder\\\" to set desktop picture to POSIX file \\\""
    + address + "\\\"\"")
예제 #11
0
def main():
    mc = redis.StrictRedis()
    while True:
        hash_value = mc.rpop('_incoming')
        if not hash_value:
            time.sleep(0.25)
            continue
        desc = mc.get(hash_value)
        if not desc:
            continue
        desc = cPickle.loads(desc)
        try:
            desc = desc._replace(status='Processing')
            mc.set(hash_value, cPickle.dumps(desc))

            im = Image(filename=os.path.join('upload', hash_value + '.' +
                                             desc.ext))
            dt_string = im.metadata.get('exif:DateTimeOriginal')
            uniq = im.metadata.get('exif:UniqueCameraModel')
            mm = im.metadata.get('exif:Make'), im.metadata.get('exif:Model')

            if dt_string is None:
                raise Exception("Image date undefined")

            dt = datetime.strptime(dt_string, '%Y:%m:%d %H:%M:%S')
            if (datetime.now() - dt).days > 365:
                raise Exception("Image too old")

            im.depth = 8
            im.format = 'RGB'
            blob = im.make_blob()
            im = PIL.Image.frombytes('RGB', im.size, blob)

            imgif = {}
            model_string = ''
            if uniq:
                model_string = uniq
                imgif[piexif.ImageIFD.UniqueCameraModel] = uniq
            else:
                if mm[0]:
                    imgif[piexif.ImageIFD.Make] = mm[0]
                if mm[1]:
                    imgif[piexif.ImageIFD.Model] = mm[1]
                model_string = ' '.join([v for v in mm if v])

            imgif[piexif.ImageIFD.DateTime] = desc.upload_date
            exif = {
                piexif.ExifIFD.DateTimeOriginal: dt_string,
                piexif.ExifIFD.UserComment: desc.name
            }
            exif_dict = {"0th": imgif, "Exif": exif}
            exif_bytes = piexif.dump(exif_dict)

            fp = io.BytesIO()
            im.save(fp, "JPEG", exif=exif_bytes)
            saved = fp.getvalue()
            size_string = '%i' % len(saved)

            desc = ImageDesc(hash_value=hash_value,
                             name=desc.name,
                             ext=desc.ext,
                             upload_date=desc.upload_date,
                             creation_date=dt_string,
                             camera=model_string,
                             size=size_string,
                             status='OK')

            os.remove(os.path.join('upload', hash_value + '.' + desc.ext))
            with open(os.path.join('static', hash_value + '.jpg'), 'wb') as f:
                f.write(saved)
            im.thumbnail((32, 32), PIL.Image.ANTIALIAS)
            im.save(os.path.join('static', hash_value + '_thumb.jpg'), "JPEG")

            mc.set(hash_value, cPickle.dumps(desc))
            mc.rpush('_images', hash_value)
        except Exception as e:
            desc = desc._replace(status=str(e))
            mc.setex(hash_value, 60 * 24, cPickle.dumps(desc))
예제 #12
0
#!/usr/bin/env python

from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color

# http://www.imagemagick.org/Usage/fonts/
# original imagemagick command:
# convert -size 320x100 xc:lightblue  -font Candice -pointsize 72 \
#         -fill Navy -draw "translate 28,68  skewX -20  text 0,0 'Anthony'" \
#         font_slanted.jpg

w = 320
h = 100
with Image(width=w, height=h, background=Color('lightblue')) as img:
    with Drawing() as draw:
        text = 'Anthony'
        draw.font = 'Candice'
        draw.font_size = 72
        draw.gravity = 'forget'
        draw.fill_color = Color('navy')
        draw.translate(28, 68)
        draw.skew(-20, 0)
        draw.text(0, 0, text)
        draw(img)
        img.save(filename='sample06.png')
예제 #13
0
def image_upload():
    error = ''
    APP_RELATED = 'static/tmp/' + session['username'] + str(get_milliseconds())
    tmp_target = os.path.join(APP_ROOT, APP_RELATED)
    target = IMAGES_PATH + session['username']

    try:
        if request.method == "POST":
            # access to database
            cnx = get_db()
            cursor = cnx.cursor()
            s3 = get_s3bucket()

            # fetch the user ID of user
            cursor.execute("SELECT userID FROM users WHERE username = (%s)",
                           (escape_string(session['username'])))
            uID = cursor.fetchone()[0]

            # file path of images
            if not os.path.isdir(os.path.join(APP_ROOT, 'static/tmp/')):
                os.mkdir(os.path.join(APP_ROOT, 'static/tmp/'))

            # create a tmp folder for the user if it does not exist
            if not os.path.isdir(tmp_target):
                os.mkdir(tmp_target)

            # check if file exists in the request
            if 'file' not in request.files:
                error = "file does not exist"
                cursor.close()
                cnx.close()
                return render_template("image-upload.html",
                                       title="upload images",
                                       error=error)

            # get the list for uploaded files
            for file in request.files.getlist("file"):
                # double check if the file exists
                if file == None or file.filename == '':
                    error = "file does not exist"
                    cursor.close()
                    cnx.close()
                    return render_template("image-upload.html",
                                           title="upload images",
                                           error=error)

                # give a pID to the new image
                cursor.execute("SELECT max(pID) FROM images")
                x = cursor.fetchone()
                if x[0] == None:
                    pID = 1
                else:
                    pID = x[0] + 1

                # give a new image name accepted by database
                filename = str(file.filename).split('.')[-1]
                filename = escape_string(
                    str(get_milliseconds()) + '.' + filename)

                # insert the image info into the database
                cursor.execute(
                    "INSERT INTO images (pName, users_userID) VALUES (%s, %s)",
                    (filename, int(uID)))
                cnx.commit()

                # save the image file
                destination = "/".join([target, filename])
                tmp_dest = "/".join([tmp_target, filename])

                file.save(tmp_dest)
                file.seek(0)
                s3.put_object(Key=destination, Body=file, ACL='public-read')

                # get pID of the new image
                cursor.execute("SELECT pID FROM images WHERE pName = (%s)",
                               (filename))
                x = cursor.fetchone()
                pID = x[0]

                # apply image transformation
                for i in range(3):
                    # give a tpID to the transformed image
                    # cursor.execute("SELECT max(tpID) FROM trimages")
                    # x = cursor.fetchone()
                    # if x[0] == None:
                    #     tpID = 1
                    # else:
                    #     tpID = x[0] + 1

                    # give a file name to the transformed image
                    tfilename = escape_string("tr" + str(i) + "_" + filename)

                    # insert the image info into the database
                    cursor.execute(
                        "INSERT INTO trimages (tpName, images_pID) VALUES (%s, %s)",
                        (tfilename, int(pID)))
                    cnx.commit()

                    # apply transformation
                    img = Image(filename=tmp_dest)
                    with img.clone() as tfile:
                        image_transfer(tfile, i)
                        # save the image file
                        tmp_tdest = "/".join([tmp_target, tfilename])
                        tfile.save(filename=tmp_tdest)
                        tdestination = "/".join([target, tfilename])
                        s3.put_object(Key=tdestination,
                                      Body=open(tmp_tdest, 'rb'),
                                      ACL='public-read')

            # database commit, cleanup and garbage collect
            shutil.rmtree(tmp_target)
            cursor.close()
            cnx.close()
            gc.collect()

            flash("upload successful")
            return redirect(url_for("gallery"))

        return render_template("image-upload.html", title="upload images")

    except Exception as e:
        if os.path.isdir(tmp_target):
            shutil.rmtree(tmp_target)
        teardown_db(e)
        return str(e)
예제 #14
0
파일: full.py 프로젝트: ml1976/scanned-mail
def start_scanning(file_path):
    tool = pyocr.get_available_tools()[0]
    req_image = []
    final_text = []
    path_to_pdf = file_path
    file_name = path_to_pdf.split('/')[-1]
    print(file_name)
    print('This is path to pdf: ' + path_to_pdf)
    path_to_new_pdf = 'scanned_pdf_for_email'
    image_pdf = Image(filename=path_to_pdf, resolution=300)
    image_jpeg = image_pdf.convert('jpeg')

    for img in image_jpeg.sequence:
        img_page = Image(image=img)
        req_image.append(img_page.make_blob('jpeg'))

    for img in req_image:
        txt = tool.image_to_string(PI.open(io.BytesIO(img)),
                                   lang='eng',
                                   builder=pyocr.builders.TextBuilder())
        final_text.append(txt)

    with open('./output_text_file/listfile.txt', 'w') as f:
        for item in final_text:
            f.write('%s\n' % item)

    company_name = []

    sender_names = ['triumph']

    found_company = ''
    list_text_file = []

    with open('./output_text_file/listfile.txt', 'r') as f:
        list_text_file = f.read().split()

    list_to_string = ' '.join(map(str, list_text_file))

    # uding regex to get all caps words
    result = filter(
        None, [x.strip() for x in re.findall(r"\b[A-Z\s]+\b", list_to_string)])
    print(*result)  # printing the list using * operator separated by space

    # values = keywords(text=list_to_string, split='\n', scores=True)
    # data = pd.DataFrame(values, columns=['keyword','score'])
    # data = data.sort_values('score', ascending=False)
    # print(data.head(10))

    print('this is text in string: ' + list_to_string)

    # r = Rake()
    # r.extract_keywords_from_text(list_to_string)
    # phrases = r.get_ranked_phrases_with_scores()
    # table = pd.DataFrame(phrases, columns=['score', 'Phrase'])
    # table = table.sort_values('score', ascending=False)
    # print(table.head(10))

    #print(list_text_file)

    for word in list_text_file:
        if word.lower() in company_name:
            found_company = word.lower()
            break

    subject_str = str(found_company.capitalize()) + ' mail' + '.pdf'
    print(subject_str)
    #os.rename(path_to_pdf, path_to_new_pdf + subject_str)
    copy('./scanned_pdf_files/' + file_name, path_to_new_pdf)
    os.rename(path_to_new_pdf + '/' + file_name,
              path_to_new_pdf + '/' + subject_str)
예제 #15
0
def test_getitem_slice(slice_name, slice_, fx_asset):
    with Image(filename=str(fx_asset.join('apple.ico'))) as img:
        assert list(img.sequence[slice_]) == list(img.sequence)[slice_]
예제 #16
0
def data_parse(msg_dict):
    # print('###')
    logging.propagate = False
    logging.getLogger().setLevel(logging.ERROR)
    # print(msg_dict.get('Subject'))
    if not msg_dict.get('Subject')[0] == '您':
        logging.info('# tr filter ')
        return
    # print(type(msg_dict.get('Subject')))
    ticket_no = msg_dict.get('Subject').split(' ')[-1]

    # file_name = 'Itinerary - ' + ticket_no + '.pdf'

    file_name = 'Itinerary – ' + ticket_no + '.pdf'
    # file_name = 'Itinerary - V44DFI.pdf'
    # print(file_name)

    try:

        data = msg_dict.get('attach').get(file_name)

        if not data:
            return
        with open('parse/tr_data/test.pdf', 'wb') as f:
            f.write(data)

        tools = pyocr.get_available_tools()
        if len(tools) == 0:
            print("No OCR tool found")
            sys.exit(1)
        tool = tools[0]
        print("Will use tool '%s'" % (tool.get_name()))

        print(tool.get_available_languages())

        langs = tool.get_available_languages()
        print("Available languages: %s" % ", ".join(langs))
        lang = langs[0]
        print("Will use lang '%s'" % (lang))

        req_image = []
        final_text = []

        image_pdf = Image(filename="parse/tr_data/test.pdf", resolution=400)
        image_jpeg = image_pdf.convert('jpeg')
        image_jpeg.save(filename='parse/tr_data/test.jpeg')

        img_page = Image(image=image_jpeg.sequence[0])
        req_image.append(img_page.make_blob('jpeg'))

        # for img in image_jpeg.sequence:
        #     img_page = Image(image=img)
        #     req_image.append(img_page.make_blob('jpeg'))
        img = req_image[0]
        txt = tool.image_to_string(PI.open(io.BytesIO(img)),
                                   lang=lang,
                                   builder=pyocr.builders.TextBuilder())

        # 全部数据
        final_text.append(txt)

        # for img in req_image:
        #     txt = tool.image_to_string(
        #         PI.open(io.BytesIO(img)),
        #         lang=lang,
        #         builder=pyocr.builders.TextBuilder()
        #     )
        #     final_text.append(txt)
        # print(final_text)
        a = 0
        for text in final_text[0].replace('  ', '').replace('   ',
                                                            '').split(' '):
            # print('#' * 66, a)
            # print(text)
            a = a + 1

        print('long: ', len(final_text))
        print('# 107', final_text)

        flight_number_list = re.findall(r'\((.*?)\)', final_text[0])
        flight_number = ''
        for i in flight_number_list:
            if i[0:2] == 'TR':
                if flight_number == '':
                    flight_number = i
                else:
                    flight_number = flight_number + ',' + i

        print('flight_number: ', flight_number)

        gender_dict = {
            '先生': 'M',
            '女士': 'F',
            '夫人': 'F',
        }

        name_str_list = final_text[0].split('Umber')
        gender = ''
        name_list = []
        p = 0
        name_str = ''

        # 处理乘客姓名
        for i in name_str_list[1:]:
            # print('# i: ', i, p)
            # p = p + 1

            # 列表,全部名字
            name_str = i.replace('\n', ' ').replace('  ', '').replace(
                '   ', '').replace('-', '').replace('“',
                                                    ' ').strip().split(' ')
            # name_str = i.replace('  ', '').replace('   ', '').replace('-', '').strip().split(' ')
            print('# 121 namestr: ', name_str)

            count = 0

            # 单人
            for j in range(len(name_str)):

                # print('### 131: ', name_str[j], len(name_str[j]))
                if len(name_str[j]) > 8:
                    # print(name_str[j])
                    if name_str[j][:-2] == 'KrisFlyer':
                        # print('###################### KrisFlyer')
                        count = j
                        break

                if name_str[j] == '' or name_str[j] == '-' or name_str[j][-2:] == 'kg' or name_str[j][
                                                                                          :-2] == 'KrisFlyer' or \
                        name_str[j] == ' ' or name_str[j] == '-' or len(name_str[j]) == 0:
                    name_str.remove(name_str[j])
                if len(name_str[j]) > 20:
                    count = j
                    break

            # ['女士', 'THANYALAK', '-', 'SRIJUMPA', '先生', 'YONG', 'JIN', 'CRUZ']
            name_str = name_str[:count]
            # print('### 139: ', name_str, len(name_str))

            # # 多人
            # for j in range(len(name_str)):
            #     if name_str[j] == '' or name_str[j] == '-' or len(name_str[j]) == 0:
            #         name_str.remove(name_str[j])
            #     if len(name_str[j]) > 20:
            #         count = j
            #         break

            name_str = name_str[:count]
            print('### 148: ', name_str, len(name_str))

            # 不统计返程
            break

        # 去空
        for i in name_str:
            if len(i) == 0:
                name_str.remove(i)

        temp_name_list = []
        temp_a = []
        temp_b = []
        # 获取乘客姓名
        for i in range(len(name_str)):

            # _temp
            if i != 0 and name_str[i] in gender_dict:
                for j in name_str[i:]:
                    temp_b.append(j)
                break
            temp_a.append(name_str[i])

        temp_name_list.append(temp_a)
        if len(temp_b) != 0:
            temp_name_list.append(temp_b)

        print('# 179', temp_name_list)

        name = ''
        for i in temp_name_list:
            if name == '':
                for j in i[1:-1]:
                    name = name + j
                name = i[-1] + '/' + name
                gender = gender_dict.get(i[0])
            else:
                temp_name = ''
                for j in i[1:-1]:
                    temp_name = temp_name + j
                name = name + ',' + i[-1] + '/' + temp_name
                gender = gender + ',' + gender_dict.get(i[0])

        print('# name', name, gender)

        # 出发时间
        name_str_list = name_str_list[0].replace('\n', ' ').replace(
            '  ', '').replace('   ',
                              '').replace('-',
                                          '').replace('“',
                                                      ' ').strip().split(' ')

        print('# 221 deptime ', name_str_list)

        dep_time = ''
        dep_date = ''
        ret_time = 0
        ret_date = ''
        for i in range(len(name_str_list)):
            if name_str_list[i] == '值机时间':

                if dep_time != '':
                    ret_time = name_str_list[i - 1].split(')')[1]
                    # break
                if dep_time == '':
                    dep_time = name_str_list[i - 1].split(')')[1]

            if name_str_list[i] == 'Fare':

                # if dep_date == '':
                #     dep_date = name_str_list[i - 1].replace('年', '-').replace('月', '-').replace('日', '')
                # ret_date = name_str_list[i - 1].replace('年', '-').replace('月', '-').replace('日', '')

                # print('# 226 ',name_str_list[i-1])
                if dep_date != '':
                    ret_date = name_str_list[i - 1].replace('年', '-').replace(
                        '月', '-').replace('日', '')
                    break
                dep_date = name_str_list[i - 1].replace('年', '-').replace(
                    '月', '-').replace('日', '')

        # print('# deptime: ', dep_date, dep_time, ret_date, ret_time)
        dep_time = time.mktime(
            time.strptime(dep_date + dep_time, '%Y-%m-%d%H:%M'))

        flight_type = 1
        if ret_date != '' and ret_time != '':
            ret_time = time.mktime(
                time.strptime(ret_date + ret_time, '%Y-%m-%d%H:%M'))
            flight_type = 2

        # ticket_no = 123

        data = {
            "name": name.strip(',').replace(' ', '').upper(),
            "dep_time": dep_time,
            "ret_time": ret_time,
            "flight_number": flight_number,
            "ticketNo": ticket_no,
            "addtime": time.time(),
            "flight_type": flight_type,
            "gender": gender.strip(','),
            "email": msg_dict['To'][1:-1].lower()
            # "email": 123
        }

    except:
        print(traceback.print_exc())
        return

    print('# data: ', data)

    return data
예제 #17
0
def pdf_preview(tmp_file_path, tmp_dir):
    if use_generic_pdf_cover:
        return None
    else:
        if use_PIL:
            try:
                input1 = PdfFileReader(open(tmp_file_path, 'rb'), strict=False)
                page0 = input1.getPage(0)
                xObject = page0['/Resources']['/XObject'].getObject()

                for obj in xObject:
                    if xObject[obj]['/Subtype'] == '/Image':
                        size = (xObject[obj]['/Width'], xObject[obj]['/Height'])
                        data = xObject[obj]._data # xObject[obj].getData()
                        mode = "P"
                        if xObject[obj]['/ColorSpace'] == '/DeviceRGB':
                            mode = "RGB"
                        if xObject[obj]['/ColorSpace'] == '/DeviceCMYK':
                            mode = "CMYK"
                        if '/Filter' in xObject[obj]:
                            if xObject[obj]['/Filter'] == '/FlateDecode':
                                img = Image.frombytes(mode, size, data)
                                cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.png"
                                img.save(filename=os.path.join(tmp_dir, cover_file_name))
                                return cover_file_name
                                # img.save(obj[1:] + ".png")
                            elif xObject[obj]['/Filter'] == '/DCTDecode':
                                cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg"
                                img = open(cover_file_name, "wb")
                                img.write(data)
                                img.close()
                                if mode == 'CMYK':
                                    img2 = Image.open(cover_file_name)# .convert('RGB')
                                    img2 = CMYKInvert(img2)
                                    img2.save(cover_file_name)
                                return cover_file_name
                            elif xObject[obj]['/Filter'] == '/JPXDecode':
                                cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jp2"
                                img = open(cover_file_name, "wb")
                                img.write(data)
                                img.close()
                                if mode == 'CMYK':
                                    img2 = Image.open(cover_file_name)# .convert('RGB')
                                    img2 = CMYKInvert(img2)
                                    img2.save(cover_file_name)
                                return cover_file_name
                        else:
                            img = Image.frombytes(mode, size, data)
                            cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.png"
                            img.save(filename=os.path.join(tmp_dir, cover_file_name))
                            return cover_file_name
                            # img.save(obj[1:] + ".png")
            except Exception as ex:
                print(ex)

        try:
            cover_file_name = os.path.splitext(tmp_file_path)[0] + ".cover.jpg"
            with Image(filename=tmp_file_path + "[0]", resolution=150) as img:
                img.compression_quality = 88
                img.save(filename=os.path.join(tmp_dir, cover_file_name))
            return cover_file_name
        except PolicyError as ex:
            log.warning('Pdf extraction forbidden by Imagemagick policy: %s', ex)
            return None
        except Exception as ex:
            log.warning('Cannot extract cover image, using default: %s', ex)
            return None
예제 #18
0
def test_length(fx_asset):
    with Image(filename=str(fx_asset.join('apple.ico'))) as img:
        assert len(img.sequence) == 4
예제 #19
0
def process(path, height_each):
    image = Image(filename=path)
    crop_square_center(image)
    image.resize(width=1040, height=960)
    return image
예제 #20
0
def test_validate_position_error(fx_asset):
    with Image(filename=str(fx_asset.join('apple.ico'))) as img:
        with raises(TypeError):
            img.sequence.validate_position(0.001)
예제 #21
0
mid_url = "http://dba.bn-ent.net/character/images/" + midchar + "/portrait.png"
right_url = "http://dba.bn-ent.net/character/images/" + rightchar + "/portrait.png"

character_distance_multiplier = 0.5
character_height_multiplier = 3

character_distance_multiplier = float(sys.argv[4])
character_height_multiplier = float(sys.argv[5])

left_char_bigger = 0
mid_char_bigger = 0
right_char_bigger = 0

mid_char_bigger = int(float(sys.argv[6]))

with Image(filename=bg_url) as img:
    character_height = int(img.height * character_height_multiplier)
    with Image(filename=mid_url) as mid:
        if mid_char_bigger == 1:
            resizeImgToHeight(mid, int(character_height * 1.2))
        else:
            resizeImgToHeight(mid, character_height)
        with Image(filename=left_url) as left:
            resizeImgToHeight(left, character_height)
            with Image(filename=right_url) as right:
                resizeImgToHeight(right, character_height)
                with Image(filename="http://i.imgur.com/QvZ9Ce0.png"
                           ) as watermark:

                    #print(img.size)
                    #print(left.size)
예제 #22
0
def test_validate_slice_error(fx_asset):
    with Image(filename=str(fx_asset.join('apple.ico'))) as img:
        with raises(ValueError):
            img.sequence.validate_slice(slice(0, 10, 3))
예제 #23
0
import sys
from wand.image import Image

if len(sys.argv)<2:
    print('{0} <ORIGINAL PATH>'.format(sys.argv[0]))
    sys.exit()

original_path=sys.argv[1]

#이미지 파일을 Image 객체로 열 때는 생성자의 filename 매개변수에 파일 경로를 입력.
with Image(filename=original_path) as image:
    print("Original : {0}, {1}".format(image.format, image.size))

    #clone()은 메모리상에 복제본을 생성합니다.
    clone=image.clone()
    print("Clone {0}, {1}".format(clone.format, clone.size))
    #save()는 Image 객체의 내용을 이미지 파일에 저장합니다.
    clone.save(filename='clone.{0}'.format(original_path))
예제 #24
0
def test_container(fx_asset):
    with Image(filename=str(fx_asset.join('apple.ico'))) as img:
        for single in img.sequence:
            assert single.container is img
예제 #25
0
def rendertask(task):
    global args
    # in debug mode we have no thread-worker which prints its progress
    if debug:
        print("generating {0} from {1}".format(task.outfile, task.infile))

    if args.skip_frames and 'only_rerender_frames_after' not in task.parameters:
        if os.path.isdir(os.path.join(task.workdir, '.frames')):
            shutil.rmtree(os.path.join(task.workdir, '.frames'))

    # make sure a .frames-directory exists in out workdir
    ensurePathExists(os.path.join(task.workdir, '.frames'))

    # open and parse the input file
    with open(os.path.join(task.workdir, task.infile), 'r') as fp:
        svgstr = fp.read()
        for key in task.parameters.keys():
            svgstr = svgstr.replace(key, xmlescape(str(task.parameters[key])))

        parser = etree.XMLParser(huge_tree=True)
        svg = etree.fromstring(svgstr.encode('utf-8'), parser)

    # if '$subtitle' in task.parameters and task.parameters['$subtitle'] == '':
    #   child = svg.findall(".//*[@id='subtitle']")[0]
    #   child.getparent().remove(child)

    # frame-number counter
    frameNr = 0

    # iterate through the animation seqence frame by frame
    # frame is a ... tbd
    cache = {}
    for frame in task.sequence(task.parameters):
        skip_rendering = False
        # skip first n frames, to speed up rerendering during debugging
        if 'only_rerender_frames_after' in task.parameters:
            skip_rendering = (frameNr <=
                              task.parameters['only_rerender_frames_after'])

        if args.skip_frames:
            skip_rendering = (frameNr <= args.skip_frames)

        if args.only_frame:
            skip_rendering = (frameNr != args.only_frame)

        # print a line for each and every frame generated
        if debug and not skip_rendering:
            print("frameNr {0:3d} => {1}".format(frameNr, frame))

        frame = tuple(frame)
        if frame in cache:
            if debug:
                print("cache hit, reusing frame {0}".format(cache[frame]))

            framedir = task.workdir + "/.frames/"
            shutil.copyfile("{0}/{1:04d}.png".format(framedir, cache[frame]),
                            "{0}/{1:04d}.png".format(framedir, frameNr))

            frameNr += 1
            continue
        elif not skip_rendering:
            cache[frame] = frameNr

        # apply the replace-pairs to the input text, by finding the specified xml-elements by thier id and modify thier css-parameter the correct value
        for replaceinfo in frame:
            (id, type, key, value) = replaceinfo

            for el in svg.findall(".//*[@id='" + id.replace("'", "\\'") +
                                  "']"):
                if type == 'style':
                    style = cssutils.parseStyle(
                        el.attrib['style'] if 'style' in el.attrib else '')
                    style[key] = str(value)
                    el.attrib['style'] = style.cssText

                elif type == 'attr':
                    el.attrib[key] = str(value)

                elif type == 'text':
                    el.text = str(value)

        if not skip_rendering:
            # open the output-file (named ".gen.svg" in the workdir)
            with open(os.path.join(task.workdir, '.gen.svg'), 'w') as fp:
                # write the generated svg-text into the output-file
                fp.write(etree.tostring(svg, encoding='unicode'))

            if task.outfile.endswith('.ts') or task.outfile.endswith('.mov'):
                width = 1920
                height = 1080
            else:
                width = 1024
                height = 576

            if args.imagemagick:
                # invoke imagemagick to convert the generated svg-file into a png inside the .frames-directory
                infile = '{0}/.gen.svg'.format(task.workdir)
                outfile = '{0}/.frames/{1:04d}.png'.format(
                    task.workdir, frameNr)
                with Image(filename=infile) as img:
                    with img.convert('png') as converted:
                        converted.save(filename=outfile)
            else:
                # invoke inkscape to convert the generated svg-file into a png inside the .frames-directory
                cmd = 'cd {0} && inkscape --export-background=white --export-background-opacity=0 --export-width={2} --export-height={3} --export-png=$(pwd)/.frames/{1:04d}.png $(pwd)/.gen.svg 2>&1 >/dev/null'.format(
                    task.workdir, frameNr, width, height)
                errorReturn = subprocess.check_output(cmd,
                                                      shell=True,
                                                      universal_newlines=True,
                                                      stderr=subprocess.STDOUT)
                if errorReturn != '':
                    print("inkscape exitted with error\n" + errorReturn)
                    # sys.exit(42)

        # increment frame-number
        frameNr += 1

    if args.only_frame:
        task.outfile = '{0}.frame{1:04d}.png'.format(task.outfile,
                                                     args.only_frame)

    # remove the dv/ts we are about to (re-)generate
    ensureFilesRemoved(os.path.join(task.workdir, task.outfile))

    if task.outfile.endswith('.png'):
        cmd = 'cd {0} && cp ".frames/{1:04d}.png" "{2}"'.format(
            task.workdir, args.only_frame, task.outfile)

    # invoke avconv aka ffmpeg and renerate a lossles-dv from the frames
    #  if we're not in debug-mode, suppress all output
    elif task.outfile.endswith('.ts'):
        cmd = 'cd {0} && '.format(task.workdir)
        cmd += 'ffmpeg -f image2 -i .frames/%04d.png '
        if task.audiofile is None:
            cmd += '-ar 48000 -ac 1 -f s16le -i /dev/zero -ar 48000 -ac 1 -f s16le -i /dev/zero '
        else:
            cmd += '-i {0} -i {0} '.format(task.audiofile)

        cmd += '-map 0:0 -c:v mpeg2video -q:v 2 -aspect 16:9 '

        if task.audiofile is None:
            cmd += '-map 1:0 -map 2:0 '
        else:
            cmd += '-map 1:0 -c:a copy -map 2:0 -c:a copy '
        cmd += '-shortest -f mpegts "{0}"'.format(task.outfile)
    elif task.outfile.endswith('.mov'):
        cmd = 'cd {0} && '.format(task.workdir)
        cmd += 'ffmpeg -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -f image2 -i .frames/%04d.png -r 25 -shortest -c:v qtrle -f mov "{0}"'.format(
            task.outfile)
    else:
        cmd = 'cd {0} && ffmpeg -ar 48000 -ac 2 -f s16le -i /dev/zero -f image2 -i .frames/%04d.png -target pal-dv -aspect 16:9 -shortest "{1}"'.format(
            task.workdir, task.outfile)

    if debug:
        print(cmd)

    r = os.system(cmd + ('' if debug else '>/dev/null 2>&1'))

    # as before, in non-debug-mode the thread-worker does all progress messages
    if debug:
        if r != 0:
            sys.exit()

    if not debug:
        print("cleanup")

        # remove the generated svg
        ensureFilesRemoved(os.path.join(task.workdir, '.gen.svg'))
예제 #26
0
def test_delay(fx_asset):
    with Image(filename=str(fx_asset.join('nocomments-delay-100.gif'))) as img:
        for s in img.sequence:
            assert s.delay == 100
예제 #27
0
    for j, label in enumerate(cluster.label):
        print(label)
        selection.append(label)
        os.system(f"cp assets/{label}* {cluster_dir}")
        column = j + 1
        plt.subplot(1, span, column)
        plt.axis("off")
        fpath = f"../coloured/{label}.png"
        plt.imshow(io.imread(fpath))
    selections.append(selection)
    plt.savefig(f"results/cluster_{cluster_id}.png")

# Trim montages in place
run_paths = list(Path("results").glob("cluster_?.png"))
for source in run_paths:
    with Image(filename=source.as_posix()) as img:
        img.trim()
        img.save(filename=source.as_posix())

i = 0
# choose three random images from each cluster
for s in selections:
    a = random.sample(s, 3)
    print(a)
    span = 3
    plt.subplots(1, span)  #, figsize=(span, 4))

    rnd = []
    for j, label in enumerate(a):
        print(label)
        rnd.append(label)
예제 #28
0
def test_delitem_not_loaded(fx_asset):
    with Image(filename=str(fx_asset.join('apple.ico'))) as img:
        del img.sequence[1]
        assert len(img.sequence) == 3
def draw_bg(width, height, filename):
    with Image(width=width, height=height, background=Color('white')) as img:
        img.save(filename=filename)
예제 #30
0
def convert_pdf_to_png(event, context):
    """
    Simple use of ImageMagick to convert a 1-n page PDF into images in groups of 3 pages.

    Takes in b64 encoded PDF files and turns them into b64 encoded PNG files to be sent to Google
    Vision OCR. Print statements are used for CloudWatch logging.
    :param event: JSON data POST'd to endpoint.
    :param context: Unused param in all lambda functions.
    :return: list of zlibcompressed/b64encoded PNG data.
    """
    GROUP_SIZE = 3
    img_data = []
    pdf_data = event.get('pdf_bin')

    if not pdf_data:
        return {'error': 'No PDF data passed'}

    decoded = b64decode(pdf_data)
    del pdf_data
    print 'Decoded'

    #Creates a single large Image object from the PDF data.
    with Image(blob=decoded, resolution=300) as pdf:
        """
        pdf.sequence returns an iterable ie pictures of each page from the pdf.
        When the iterable is 3 or less:
            ((<wand.sequence.SingleImage: dc1d561 (2470x3281)>, None, None))
        When the iterable is more than 3:
            (
                (<wand.sequence>, (<wand.sequence>, (<wand.sequence>),
                (<wand.sequence>, None, None)
            )
        """
        del decoded
        grouped = grouper(pdf.sequence, GROUP_SIZE)

        for group in grouped:
            # Creates a new 'blank' Image that is smaller than the above pdf context.
            trimmed = [item for item in group if item]
            with Image(width=pdf.width, height=pdf.height * len(trimmed)) as image:

                for i, sequence in enumerate(trimmed):
                    image.composite(
                        sequence,
                        top=pdf.height * i,
                        left=0
                    )
                del trimmed
                print 'Image composed'

                blob = image.make_blob('png')
                del image
                print 'Blob made'

                compressed = compress(blob)
                del blob
                print 'Compressed'

                encoded = b64encode(compressed)
                del compressed
                print 'Encoded'

                img_data.append(encoded)
                del encoded

    timeouts = xrange(45, 75)
    new_timeout = random.choice(timeouts)
    lambda_client.update_function_configuration(
        FunctionName = 'imageMagick',
        Timeout=new_timeout
    )
    return {'img_data': img_data}