Allow loading of WMF images at a given DPI

This commit is contained in:
Andrew Murray 2019-12-28 11:25:39 +11:00
parent a1635ac6c4
commit 61d0784933
3 changed files with 28 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@ -53,6 +53,17 @@ class TestFileWmf(PillowTestCase):
with Image.open("Tests/images/drawing_roundDown.emf") as im:
self.assertEqual(im.info["dpi"], 1426)
def test_load_set_dpi(self):
with Image.open("Tests/images/drawing.wmf") as im:
self.assertEquals(im.size, (82, 82))
if hasattr(Image.core, "drawwmf"):
im.load(144)
self.assertEquals(im.size, (164, 164))
with Image.open("Tests/images/drawing_wmf_ref_144.png") as expected:
self.assert_image_similar(im, expected, 2.0)
def test_save(self):
im = hopper()

View File

@ -78,6 +78,7 @@ class WmfStubImageFile(ImageFile.StubImageFile):
format_description = "Windows Metafile"
def _open(self):
self._inch = None
# check placable header
s = self.fp.read(80)
@ -87,7 +88,7 @@ class WmfStubImageFile(ImageFile.StubImageFile):
# placeable windows metafile
# get units per inch
inch = word(s, 14)
self._inch = word(s, 14)
# get bounding box
x0 = short(s, 6)
@ -96,12 +97,14 @@ class WmfStubImageFile(ImageFile.StubImageFile):
y1 = short(s, 12)
# normalize size to 72 dots per inch
size = (x1 - x0) * 72 // inch, (y1 - y0) * 72 // inch
self.info["dpi"] = 72
size = (
(x1 - x0) * self.info["dpi"] // self._inch,
(y1 - y0) * self.info["dpi"] // self._inch,
)
self.info["wmf_bbox"] = x0, y0, x1, y1
self.info["dpi"] = 72
# sanity check (standard metafile header)
if s[22:26] != b"\x01\x00\t\x00":
raise SyntaxError("Unsupported WMF file format")
@ -118,7 +121,6 @@ class WmfStubImageFile(ImageFile.StubImageFile):
# get frame (in 0.01 millimeter units)
frame = _long(s, 24), _long(s, 28), _long(s, 32), _long(s, 36)
# normalize size to 72 dots per inch
size = x1 - x0, y1 - y0
# calculate dots per inch from bbox and frame
@ -145,6 +147,16 @@ class WmfStubImageFile(ImageFile.StubImageFile):
def _load(self):
return _handler
def load(self, dpi=None):
if dpi is not None and self._inch is not None:
self.info["dpi"] = int(dpi + 0.5)
x0, y0, x1, y1 = self.info["wmf_bbox"]
self._size = (
(x1 - x0) * self.info["dpi"] // self._inch,
(y1 - y0) * self.info["dpi"] // self._inch,
)
super().load()
def _save(im, fp, filename):
if _handler is None or not hasattr(_handler, "save"):