def test_additional_metadata(self, tmp_path):
        # these should not crash. Seriously dummy data, most of it doesn't make
        # any sense, so we're running up against limits where we're asking
        # libtiff to do stupid things.

        # Get the list of the ones that we should be able to write

        core_items = {
            tag: info
            for tag, info in ((s, TiffTags.lookup(s))
                              for s in TiffTags.LIBTIFF_CORE)
            if info.type is not None
        }

        # Exclude ones that have special meaning
        # that we're already testing them
        with Image.open("Tests/images/hopper_g4.tif") as im:
            for tag in im.tag_v2:
                try:
                    del core_items[tag]
                except KeyError:
                    pass
            del core_items[320]  # colormap is special, tested below

            # Type codes:
            #     2: "ascii",
            #     3: "short",
            #     4: "long",
            #     5: "rational",
            #     12: "double",
            # Type: dummy value
            values = {
                2: "test",
                3: 1,
                4: 2**20,
                5: TiffImagePlugin.IFDRational(100, 1),
                12: 1.05,
            }

            new_ifd = TiffImagePlugin.ImageFileDirectory_v2()
            for tag, info in core_items.items():
                if info.length == 1:
                    new_ifd[tag] = values[info.type]
                if info.length == 0:
                    new_ifd[tag] = tuple(values[info.type] for _ in range(3))
                else:
                    new_ifd[tag] = tuple(values[info.type]
                                         for _ in range(info.length))

            # Extra samples really doesn't make sense in this application.
            del new_ifd[338]

            out = str(tmp_path / "temp.tif")
            TiffImagePlugin.WRITE_LIBTIFF = True

            im.save(out, tiffinfo=new_ifd)

        TiffImagePlugin.WRITE_LIBTIFF = False
    def test_additional_metadata(self):
        # these should not crash. Seriously dummy data, most of it doesn't make
        # any sense, so we're running up against limits where we're asking
        # libtiff to do stupid things.

        # Get the list of the ones that we should be able to write

        core_items = dict((tag, info)
                          for tag, info in [(s, TiffTags.lookup(s))
                                            for s in TiffTags.LIBTIFF_CORE]
                          if info.type is not None)

        # Exclude ones that have special meaning
        # that we're already testing them
        im = Image.open('Tests/images/hopper_g4.tif')
        for tag in im.tag_v2.keys():
            try:
                del (core_items[tag])
            except:
                pass

        # Type codes:
        #     2: "ascii",
        #     3: "short",
        #     4: "long",
        #     5: "rational",
        #     12: "double",
        # type: dummy value
        values = {
            2: 'test',
            3: 1,
            4: 2**20,
            5: TiffImagePlugin.IFDRational(100, 1),
            12: 1.05
        }

        new_ifd = TiffImagePlugin.ImageFileDirectory_v2()
        for tag, info in core_items.items():
            if info.length == 1:
                new_ifd[tag] = values[info.type]
            if info.length == 0:
                new_ifd[tag] = tuple(values[info.type] for _ in range(3))
            else:
                new_ifd[tag] = tuple(values[info.type]
                                     for _ in range(info.length))

        # Extra samples really doesn't make sense in this application.
        del (new_ifd[338])

        out = self.tempfile("temp.tif")
        TiffImagePlugin.WRITE_LIBTIFF = True

        im.save(out, tiffinfo=new_ifd)

        TiffImagePlugin.WRITE_LIBTIFF = False
Beispiel #3
0
    def test_additional_metadata(self):
        # these should not crash. Seriously dummy data, most of it doesn't make
        # any sense, so we're running up against limits where we're asking
        # libtiff to do stupid things.

        # Get the list of the ones that we should be able to write

        core_items = {tag: info for tag, info in ((s, TiffTags.lookup(s)) for s
                                                  in TiffTags.LIBTIFF_CORE)
                      if info.type is not None}

        # Exclude ones that have special meaning
        # that we're already testing them
        im = Image.open('Tests/images/hopper_g4.tif')
        for tag in im.tag_v2:
            try:
                del core_items[tag]
            except KeyError:
                pass

        # Type codes:
        #     2: "ascii",
        #     3: "short",
        #     4: "long",
        #     5: "rational",
        #     12: "double",
        # Type: dummy value
        values = {2: 'test',
                  3: 1,
                  4: 2**20,
                  5: TiffImagePlugin.IFDRational(100, 1),
                  12: 1.05}

        new_ifd = TiffImagePlugin.ImageFileDirectory_v2()
        for tag, info in core_items.items():
            if info.length == 1:
                new_ifd[tag] = values[info.type]
            if info.length == 0:
                new_ifd[tag] = tuple(values[info.type] for _ in range(3))
            else:
                new_ifd[tag] = tuple(values[info.type]
                                     for _ in range(info.length))

        # Extra samples really doesn't make sense in this application.
        del new_ifd[338]

        out = self.tempfile("temp.tif")
        TiffImagePlugin.WRITE_LIBTIFF = True

        im.save(out, tiffinfo=new_ifd)

        TiffImagePlugin.WRITE_LIBTIFF = False
Beispiel #4
0
def per_file(f):
    im = Image.open(f)

    if im.width > im.height:
        ratio = 3508 / im.width
    else:
        ratio = 3508 / im.height
    new_size = round(im.width * ratio), round(im.height * ratio)

    if ratio < 1:  # only shrink, dont inflate
        new_str = f.stem + shape
        if not Path(new_str).is_file():
            # print(Image.TiffTags.TAGS_V2)
            print(f"{f}: ({im.width}, {im.height}) -> {new_size} {ratio}")
            # I get error with test image
            # https://github.com/python-pillow/Pillow/issues/5314
            # Image.DEBUG=True
            im.thumbnail(new_size, resample=Image.LANCZOS)
            TiffTags.TAGS_V2[33723] = TiffTags.TagInfo("IptcNaaInfo",
                                                       TiffTags.BYTE, 0)
            print(f"\t saving {new_str}")
            im.save(new_str)