Beispiel #1
0
def main():
    f = open('changedIPTCmeta.txt', 'w')

    parser = argparse.ArgumentParser()
    parser.add_argument("-image_path",
                        help='path to jpg images',
                        type=str,
                        required=True)
    image_path = parser.parse_args()

    data = pd.read_csv('iptcmeta.csv')
    i = 0

    for filename in data['fileName']:
        info = IPTCInfo(join(image_path.image_path, filename))
        oldKeywords = info['keywords']
        oldObjName = info['object name']
        oldCaption = info['caption/abstract']
        newkey = data.at[i, 'keywords'].split(',')
        info['object name'] = data.at[i, 'obj_name']
        info['caption/abstract'] = data.at[i, 'caption']
        info['keywords'] = newkey
        f.write('В файле ' + str(filename) +
                ' значения keywords изменились с ' + str(oldKeywords) +
                ' на ' + str(newkey) + ' значения object_name изменились с ' +
                str(oldObjName) + ' на ' + str(info['object name']) +
                ' значения caption изменились с ' + str(oldCaption) + ' на ' +
                str(info['caption/abstract']) + '\n')
        info.save()
        print(filename)
        i = i + 1

    f.close()
Beispiel #2
0
def process_image(ifile:str, siteconfig, ofile:str=None):
    '''resize an image and return the output file name and the exif and iptc meta data'''
    try:
        size = siteconfig.get('resize')
        site_copyright_notice = siteconfig.get('copyright_notice')

        im  = Image.open(ifile)
        im_xif = im.info.get('exif')
        im_iptc = retrieve_copyright_notice(ifile)

        dest = ofile if size and ofile else ifile #Target the input file if the output file is not provided
        
        # Image resizing with exif preservation
        if size:
            im2 = im.resize(size)

            params = dict(exif=im_xif) if im_xif else dict()   
            im2.save(dest, **params)

        # IPTC data rewriting. IPTC are lost during the resize operation. We have to put them back manually
        # If the inputfile contains a copyright notice, then use it instead of the siteconfig value
        im_iptc['copyright notice'] = im_iptc.get('copyright notice', site_copyright_notice)

        if im_iptc or site_copyright_notice:
            iptc = IPTCInfo(dest)

            for key in im_iptc:
                iptc[key] = im_iptc[key]
            iptc.save()

        return (dest, im_xif, im_iptc)
    except Exception as e:
        printfailure('could not process image ', ifile, ' traceback ', str(e))
        return (ifile, None, None)
Beispiel #3
0
    def mutate(self, info, input):
        abs_path = Path(BASE_DIR, input["path"])
        if not abs_path.exists():
            raise Exception("Image not found")

        image = {"path": abs_path}
        iptc_info = IPTCInfo(str(abs_path), inp_charset="utf_8")
        if iptc_info["keywords"] == input["keywords"]:
            return SetKeywordsPayload(image=image)

        iptc_info["keywords"] = input["keywords"]
        iptc_info.save()
        return SetKeywordsPayload(image=image)
Beispiel #4
0
def mangle(file: IPTCInfo) -> int:
    tracking: str = file['special instructions']

    if tracking is not None:
        if tracking.startswith(b'FBMD'):
            print(
                f'Old tracking: {file["special instructions"].decode("utf-8")}'
            )
            file[
                'special instructions'] = f'FBMD{secrets.token_hex(int((len(tracking) - 4) / 2))}'
            print(f'New tracking: {file["special instructions"]}')
            file.save()
            return 1
        else:
            return 0
    else:
        return 0
def process_file(filepath, random_replace=False, verbose=False):
    img_type = imghdr.what(filepath)

    if img_type is None:
        if verbose:
            print("Skipping).")
        return

    if verbose:
        print(img_type, end='). ')

    info = IPTCInfo(filepath, force=True)

    special_instructions = info['special instructions']
    if special_instructions is None:
        if verbose:
            print("FBMD not found")
        return

    fbmd_index = special_instructions.find(b'FBMD')
    if fbmd_index < 0:
        if verbose:
            print("FBMD not found")
        return

    length_hex = special_instructions[fbmd_index + 6:fbmd_index + 6 + 4]
    length = int(length_hex, 16)

    info['special instructions'] = update_instructions(special_instructions,
                                                       fbmd_index,
                                                       length,
                                                       random=random_replace)

    if verbose:
        print(special_instructions[fbmd_index:fbmd_index + 6 + 4 +
                                   (length + 1) * 8])

    info.save()
    os.remove(filepath + "~")
Beispiel #6
0
for key, value in data.items():
    print(key)
    print(value)

    # get image path from resize method
    new_key = resize(key)

    # set recognition model
    response = model.predict_by_filename(new_key)

    # removing the resized image after it's being processed.
    os.remove(new_key)

    # append generated keywords in dictionary
    for r in response['outputs'][0]['data']['concepts']:
        data[key].append(r['name'])
        print(r['name'])  # for each element in response print key-words


# take the values from dictionary,
# match key value to a file path and append IPTC keywords to image file
for key, value in data.items():
    # print(os.path.basename(key))
    info = IPTCInfo(key)
    for item in value:
        newValue = item
        info['keywords'].append(newValue)
    info.save()

#--------------------------------------------------------------------#