Example #1
0
def update_iptc(tmp_img, orig_iptc):
    info = IPTCInfo(tmp_img, force=True)
    valids = ['keywords', 'caption/abstract', 'country/primary location name', 'city', 'sub-location',
              'credit', 'copyright notice', 'writer/editor', 'by-line', 'headline']
    for k in orig_iptc:
        if k in valids:
            info[k] = orig_iptc[k]
    print('saving new copy with IPTC tags:', tmp_img)
    info.save_as(tmp_img, {'overwrite': True})
Example #2
0
def test_save_as_saves_as_new_file_with_new_info():
    if os.path.isfile('fixtures/deleteme.jpg'):  # pragma: no cover
        os.unlink('fixtures/deleteme.jpg')

    new_headline = b'test headline %d' % random.randint(0, 100)
    info = IPTCInfo('fixtures/Lenna.jpg')
    info['headline'] = new_headline
    info.save_as('fixtures/deleteme.jpg')

    info2 = IPTCInfo('fixtures/deleteme.jpg')

    assert info2['headline'] == new_headline
Example #3
0
def test_save_as_saves_as_new_file_with_info():
    if os.path.isfile('fixtures/deleteme.jpg'):  # pragma: no cover
        os.unlink('fixtures/deleteme.jpg')

    info = IPTCInfo('fixtures/Lenna.jpg')
    info.save_as('fixtures/deleteme.jpg')

    info2 = IPTCInfo('fixtures/deleteme.jpg')

    # The files won't be byte for byte exact, so filecmp won't work
    assert info._data == info2._data
    with open('fixtures/Lenna.jpg', 'rb') as fh, open('fixtures/deleteme.jpg',
                                                      'rb') as fh2:
        start, end, adobe = jpeg_collect_file_parts(fh)
        start2, end2, adobe2 = jpeg_collect_file_parts(fh2)

    # But we can compare each section
    assert start == start2
    assert end == end2
    assert adobe == adobe2
Example #4
0
def get_date(DATADIR_RAW, DATADIR_RAW_NEW):
    '''Function wich takes as input path of dirs. in those dirs the function will irreterate throug all jpg, extract a date from their meta date and it a IPTC protocol'''

    # get the image name from the dir DATADIR_RAW
    for img in os.listdir(DATADIR_RAW):
        image_path = os.path.join(DATADIR_RAW, img)
        info = IPTCInfo(image_path, force=True)
        print(img)

        # Open the image and access the metadate. Use regex to extract date:
        image = Image.open(image_path)
        txt = str(image.info['exif'])  # exif are mostly JPEG
        r = '[\d]{4}:[\d]{2}:[\d]{2}'  # format yyyy:mm:dd
        date = re.search(r, txt).group().replace(':', '')
        print(f'date of picture: {date}')

        # Force open the IPTC protocol to insert date
        info = IPTCInfo(image_path, force=True)
        print(f"info before: {info['date created']}"
              )  #Before. Shows wheter or not the entry was empty before
        info['date created'] = date
        print(f"info after: {info['date created']}"
              )  # after. Shows the date extracted from the meta data
        print('\n')

        # Create the new dir DATADIR_RAW_NEW if it does not already exist
        if not os.path.exists(DATADIR_RAW_NEW):
            os.makedirs(DATADIR_RAW_NEW)

        # Create the new path for the image
        new_path = os.path.join(DATADIR_RAW_NEW, 'new' + img)

        # If that image is already in the path, delete it.
        if os.path.exists(DATADIR_RAW_NEW):
            try:
                os.remove(new_path)
            except:
                pass

        # Save new image.
        info.save_as(new_path)
Example #5
0
def write_keywords(keywords, output_file):
    info = IPTCInfo(output_file, force=True, inp_charset='utf8')
    info['keywords'] = list(keywords)
    info.save_as(output_file)
Example #6
0
from iptcinfo3 import IPTCInfo


def remove_prefix(text, prefix):
    if text.startswith(prefix):
        return text[len(prefix):]
    return text


def write_keywords(keywords, output_file):
    info = IPTCInfo(output_file, force=True, inp_charset='utf8')
    info['keywords'] = list(keywords)
    info.save_as(output_file)


if __name__ == '__main__':
    input_dir = sys.argv[1]
    if not os.path.isdir(input_dir):
        raise ValueError("Input directory path is not correct")
    for filename in os.listdir(input_dir):
        info = IPTCInfo(input_dir + "/" + filename,
                        force=True,
                        inp_charset='utf_8')
        new_keywords = set()
        for each in info['keywords']:
            new_keywords.add(remove_prefix(each, " ").lower())
        info['keywords'] = list(new_keywords)
        info.save_as(input_dir + "2/" + filename)
    print()