Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better BMP default DPI #745

Merged
merged 4 commits into from
Jun 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@


from PIL import Image, ImageFile, ImagePalette, _binary
import math

i8 = _binary.i8
i16 = _binary.i16le
Expand Down Expand Up @@ -88,13 +89,16 @@ def _bitmap(self, header = 0, offset = 0):
bits = i16(s[14:])
self.size = i32(s[4:]), i32(s[8:])
compression = i32(s[16:])
pxperm = (i32(s[24:]), i32(s[28:])) # Pixels per meter
lutsize = 4
colors = i32(s[32:])
direction = -1
if i8(s[11]) == 0xff:
# upside-down storage
self.size = self.size[0], 2**32 - self.size[1]
direction = 0

self.info["dpi"] = tuple(map(lambda x: math.ceil(x / 39.3701), pxperm))

else:
raise IOError("Unsupported BMP header type (%d)" % len(s))
Expand Down Expand Up @@ -203,30 +207,37 @@ def _save(im, fp, filename, check=0):
if check:
return check

info = im.encoderinfo

dpi = info.get("dpi", (96, 96))

# 1 meter == 39.3701 inches
ppm = tuple(map(lambda x: int(x * 39.3701), dpi))

stride = ((im.size[0]*bits+7)//8+3)&(~3)
header = 40 # or 64 for OS/2 version 2
offset = 14 + header + colors * 4
image = stride * im.size[1]

# bitmap header
fp.write(b"BM" + # file type (magic)
o32(offset+image) + # file size
o32(0) + # reserved
o32(offset)) # image data offset
fp.write(b"BM" + # file type (magic)
o32(offset+image) + # file size
o32(0) + # reserved
o32(offset)) # image data offset

# bitmap info header
fp.write(o32(header) + # info header size
o32(im.size[0]) + # width
o32(im.size[1]) + # height
o16(1) + # planes
o16(bits) + # depth
o32(0) + # compression (0=uncompressed)
o32(image) + # size of bitmap
o32(1) + o32(1) + # resolution
o32(colors) + # colors used
o32(colors)) # colors important

fp.write(b"\0" * (header - 40)) # padding (for OS/2 format)
fp.write(o32(header) + # info header size
o32(im.size[0]) + # width
o32(im.size[1]) + # height
o16(1) + # planes
o16(bits) + # depth
o32(0) + # compression (0=uncompressed)
o32(image) + # size of bitmap
o32(ppm[0]) + o32(ppm[1]) + # resolution
o32(colors) + # colors used
o32(colors)) # colors important

fp.write(b"\0" * (header - 40)) # padding (for OS/2 format)

if im.mode == "1":
for i in (0, 255):
Expand Down
12 changes: 12 additions & 0 deletions Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ def test_save_to_bytes(self):
self.assertEqual(im.size, reloaded.size)
self.assertEqual(reloaded.format, "BMP")

def test_dpi(self):
dpi = (72, 72)

output = io.BytesIO()
im = lena()
im.save(output, "BMP", dpi=dpi)

output.seek(0)
reloaded = Image.open(output)

self.assertEqual(reloaded.info["dpi"], dpi)


if __name__ == '__main__':
unittest.main()
Expand Down