mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-11 04:07:21 +03:00
Added context manager when opening files in WalImageFile
This commit is contained in:
parent
06895b6fa4
commit
87de178e0b
|
@ -47,33 +47,34 @@ def open(filename):
|
||||||
# FIXME: modify to return a WalImageFile instance instead of
|
# FIXME: modify to return a WalImageFile instance instead of
|
||||||
# plain Image object ?
|
# plain Image object ?
|
||||||
|
|
||||||
|
def imopen(fp):
|
||||||
|
# read header fields
|
||||||
|
header = fp.read(32+24+32+12)
|
||||||
|
size = i32(header, 32), i32(header, 36)
|
||||||
|
offset = i32(header, 40)
|
||||||
|
|
||||||
|
# load pixel data
|
||||||
|
fp.seek(offset)
|
||||||
|
|
||||||
|
im = Image.frombytes("P", size, fp.read(size[0] * size[1]))
|
||||||
|
im.putpalette(quake2palette)
|
||||||
|
|
||||||
|
im.format = "WAL"
|
||||||
|
im.format_description = "Quake2 Texture"
|
||||||
|
|
||||||
|
# strings are null-terminated
|
||||||
|
im.info["name"] = header[:32].split(b"\0", 1)[0]
|
||||||
|
next_name = header[56:56+32].split(b"\0", 1)[0]
|
||||||
|
if next_name:
|
||||||
|
im.info["next_name"] = next_name
|
||||||
|
|
||||||
|
return im
|
||||||
|
|
||||||
if hasattr(filename, "read"):
|
if hasattr(filename, "read"):
|
||||||
fp = filename
|
return imopen(filename)
|
||||||
else:
|
else:
|
||||||
fp = builtins.open(filename, "rb")
|
with builtins.open(filename, "rb") as fp:
|
||||||
|
return imopen(fp)
|
||||||
# read header fields
|
|
||||||
header = fp.read(32+24+32+12)
|
|
||||||
size = i32(header, 32), i32(header, 36)
|
|
||||||
offset = i32(header, 40)
|
|
||||||
|
|
||||||
# load pixel data
|
|
||||||
fp.seek(offset)
|
|
||||||
|
|
||||||
im = Image.frombytes("P", size, fp.read(size[0] * size[1]))
|
|
||||||
im.putpalette(quake2palette)
|
|
||||||
|
|
||||||
im.format = "WAL"
|
|
||||||
im.format_description = "Quake2 Texture"
|
|
||||||
|
|
||||||
# strings are null-terminated
|
|
||||||
im.info["name"] = header[:32].split(b"\0", 1)[0]
|
|
||||||
next_name = header[56:56+32].split(b"\0", 1)[0]
|
|
||||||
if next_name:
|
|
||||||
im.info["next_name"] = next_name
|
|
||||||
|
|
||||||
return im
|
|
||||||
|
|
||||||
|
|
||||||
quake2palette = (
|
quake2palette = (
|
||||||
# default palette taken from piffo 0.93 by Hans Häggström
|
# default palette taken from piffo 0.93 by Hans Häggström
|
||||||
|
|
Loading…
Reference in New Issue
Block a user