mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-12 01:20:53 +03:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
9b8c29fdc0
commit
86476a9bd2
|
@ -18,7 +18,7 @@ def test_posy_link():
|
|||
assert im.size == (128, 128)
|
||||
assert im.getpixel((0, 0)) == (0, 0, 0, 0)
|
||||
assert im.getpixel((20, 20)) == (0, 0, 0, 255)
|
||||
assert im.getpixel((40,40)) == (255, 255, 255, 255)
|
||||
assert im.getpixel((40, 40)) == (255, 255, 255, 255)
|
||||
|
||||
|
||||
def test_invalid_file():
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
from io import BytesIO
|
||||
import struct
|
||||
from PIL import Image, CurImagePlugin, ImageFile, BmpImagePlugin
|
||||
from io import BytesIO
|
||||
|
||||
from PIL import BmpImagePlugin, CurImagePlugin, Image, ImageFile
|
||||
from PIL._binary import i16le as i16
|
||||
from PIL._binary import i32le as i32
|
||||
from PIL._binary import o8
|
||||
from PIL._binary import o16le as o16
|
||||
from PIL._binary import o32le as o32
|
||||
|
||||
|
||||
def _accept(s):
|
||||
return s[:4] == b'RIFF'
|
||||
return s[:4] == b"RIFF"
|
||||
|
||||
|
||||
def _save_frame(im: Image.Image, fp: BytesIO, filename: str, info: dict):
|
||||
|
@ -71,6 +73,7 @@ def _save_frame(im: Image.Image, fp: BytesIO, filename: str, info: dict):
|
|||
offset = offset + bytes_len
|
||||
fp.seek(current)
|
||||
|
||||
|
||||
def _write_single_frame(im: Image.Image, fp: BytesIO, filename: str):
|
||||
fp.write(b"anih")
|
||||
anih = o32(36) + o32(36) + (o32(1) * 2) + (o32(0) * 4) + o32(60) + o32(1)
|
||||
|
@ -100,7 +103,8 @@ def _write_single_frame(im: Image.Image, fp: BytesIO, filename: str):
|
|||
|
||||
fp.seek(fram_end)
|
||||
|
||||
def _write_multiple_frames(im: Image.Image, fp: BytesIO, filename:str):
|
||||
|
||||
def _write_multiple_frames(im: Image.Image, fp: BytesIO, filename: str):
|
||||
anih_offset = fp.tell()
|
||||
fp.write(b"anih" + o32(36))
|
||||
fp.write(o32(0) * 9)
|
||||
|
@ -182,13 +186,20 @@ def _write_multiple_frames(im: Image.Image, fp: BytesIO, filename:str):
|
|||
|
||||
fp.seek(anih_offset)
|
||||
fp.write(b"anih")
|
||||
anih = o32(36) + o32(36) + o32(n_frames) + o32(n_steps) + (o32(0) * 4) + o32(display_rate) + o32(flag)
|
||||
anih = (
|
||||
o32(36)
|
||||
+ o32(36)
|
||||
+ o32(n_frames)
|
||||
+ o32(n_steps)
|
||||
+ (o32(0) * 4)
|
||||
+ o32(display_rate)
|
||||
+ o32(flag)
|
||||
)
|
||||
fp.write(anih)
|
||||
|
||||
fp.seek(fram_end)
|
||||
|
||||
|
||||
|
||||
def _write_info(im: Image.Image, fp: BytesIO, filename: str):
|
||||
fp.write(b"LIST" + o32(0))
|
||||
list_offset = fp.tell()
|
||||
|
@ -233,6 +244,7 @@ def _write_info(im: Image.Image, fp: BytesIO, filename: str):
|
|||
|
||||
fp.seek(info_end)
|
||||
|
||||
|
||||
def _save(im: Image.Image, fp: BytesIO, filename: str):
|
||||
fp.write(b"RIFF\x00\x00\x00\x00")
|
||||
riff_offset = fp.tell()
|
||||
|
@ -254,6 +266,7 @@ def _save(im: Image.Image, fp: BytesIO, filename: str):
|
|||
|
||||
fp.seek(riff_end)
|
||||
|
||||
|
||||
class AniFile:
|
||||
def __init__(self, buf: BytesIO) -> None:
|
||||
self.image_data = []
|
||||
|
@ -263,22 +276,19 @@ class AniFile:
|
|||
self.seq = None
|
||||
self.anih = None
|
||||
|
||||
riff, size, fformat = struct.unpack('<4sI4s', buf.read(12))
|
||||
if riff != b'RIFF':
|
||||
riff, size, fformat = struct.unpack("<4sI4s", buf.read(12))
|
||||
if riff != b"RIFF":
|
||||
SyntaxError("Not an ANI file")
|
||||
|
||||
self.riff = {
|
||||
"size": size,
|
||||
"fformat": fformat
|
||||
}
|
||||
self.riff = {"size": size, "fformat": fformat}
|
||||
|
||||
chunkOffset = buf.tell()
|
||||
while chunkOffset < self.riff['size']:
|
||||
while chunkOffset < self.riff["size"]:
|
||||
buf.seek(chunkOffset)
|
||||
chunk, size = struct.unpack('<4sI', buf.read(8))
|
||||
chunk, size = struct.unpack("<4sI", buf.read(8))
|
||||
chunkOffset = chunkOffset + size + 8
|
||||
|
||||
if chunk == b'anih':
|
||||
if chunk == b"anih":
|
||||
s = buf.read(36)
|
||||
self.anih = {
|
||||
"size": i32(s), # Data structure size (in bytes)
|
||||
|
@ -293,22 +303,22 @@ class AniFile:
|
|||
"bfAttributes": i32(s, 32), # ANI attribute bit flags
|
||||
}
|
||||
|
||||
if chunk == b'seq ':
|
||||
if chunk == b"seq ":
|
||||
s = buf.read(size)
|
||||
self.seq = [i32(s, i*4) for i in range(size // 4)]
|
||||
self.seq = [i32(s, i * 4) for i in range(size // 4)]
|
||||
|
||||
if chunk == b'rate':
|
||||
if chunk == b"rate":
|
||||
s = buf.read(size)
|
||||
self.rate = [i32(s, i*4) for i in range(size // 4)]
|
||||
self.rate = [i32(s, i * 4) for i in range(size // 4)]
|
||||
|
||||
if chunk == b'LIST':
|
||||
listtype = struct.unpack('<4s', buf.read(4))[0]
|
||||
if listtype != b'fram':
|
||||
if chunk == b"LIST":
|
||||
listtype = struct.unpack("<4s", buf.read(4))[0]
|
||||
if listtype != b"fram":
|
||||
continue
|
||||
|
||||
listOffset = 0
|
||||
while listOffset < size - 8:
|
||||
_, lSize = struct.unpack('<4sI', buf.read(8))
|
||||
_, lSize = struct.unpack("<4sI", buf.read(8))
|
||||
self.image_data.append({"offset": buf.tell(), "size": lSize})
|
||||
|
||||
buf.read(lSize)
|
||||
|
@ -335,7 +345,6 @@ class AniFile:
|
|||
return im
|
||||
|
||||
|
||||
|
||||
class AniImageFile(ImageFile.ImageFile):
|
||||
format = "ANI"
|
||||
format_description = "Windows Animated Cursor"
|
||||
|
@ -354,14 +363,13 @@ class AniImageFile(ImageFile.ImageFile):
|
|||
|
||||
@size.setter
|
||||
def size(self, value):
|
||||
if value not in self.info['sizes']:
|
||||
if value not in self.info["sizes"]:
|
||||
raise ValueError("This is not one of the allowed sizes of this image")
|
||||
self._size = value
|
||||
|
||||
|
||||
def load(self):
|
||||
im = self.ani.frame(self.frame)
|
||||
self.info['sizes'] = im.info['sizes']
|
||||
self.info["sizes"] = im.info["sizes"]
|
||||
self.im = im.im
|
||||
self.mode = im.mode
|
||||
|
||||
|
|
|
@ -29,6 +29,7 @@ from ._binary import o32le as o32
|
|||
|
||||
_MAGIC = b"\0\0\2\0"
|
||||
|
||||
|
||||
def _save(im: Image.Image, fp: BytesIO, filename: str):
|
||||
fp.write(_MAGIC)
|
||||
bmp = im.encoderinfo.get("bitmap_format", "") == "bmp"
|
||||
|
@ -141,10 +142,15 @@ class CurFile(IcoImagePlugin.IcoFile):
|
|||
# subtracting the AND mask (equal to number of pixels * 1bpp) and dividing by the number
|
||||
# of pixels. This seems to work well so far.
|
||||
BITMAP_INFO_HEADER_SIZE = 40
|
||||
bpp_without_and = ((icon_header["size"] - BITMAP_INFO_HEADER_SIZE) * 8) // icon_header["square"]
|
||||
bpp_without_and = (
|
||||
(icon_header["size"] - BITMAP_INFO_HEADER_SIZE) * 8
|
||||
) // icon_header["square"]
|
||||
|
||||
if bpp_without_and != 32:
|
||||
icon_header["bpp"] = ((icon_header["size"] - BITMAP_INFO_HEADER_SIZE) * 8 - icon_header["square"]) // icon_header["square"]
|
||||
icon_header["bpp"] = (
|
||||
(icon_header["size"] - BITMAP_INFO_HEADER_SIZE) * 8
|
||||
- icon_header["square"]
|
||||
) // icon_header["square"]
|
||||
else:
|
||||
icon_header["bpp"] = bpp_without_and
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user