镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
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
29 changes: 28 additions & 1 deletion PIL/ImageQt.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ def fromqpixmap(im):
# bytes_io.seek(0)
# return PIL.Image.open(bytes_io)

def align8to32(bytes, width, mode):
"""
converts each scanline of data from 8 bit to 32 bit aligned
"""

bits_per_pixel = {
'1': 1,
'L': 8,
'P': 8,
}[mode]

# calculate bytes per line and the extra padding if needed
bits_per_line = bits_per_pixel * width
full_bytes_per_line, remaining_bits_per_line = divmod(bits_per_line, 8)
bytes_per_line = full_bytes_per_line + (1 if remaining_bits_per_line else 0)

extra_padding = -bytes_per_line % 4

# already 32 bit aligned by luck
if not extra_padding:
return bytes

new_data = []
for i in range(len(bytes) / bytes_per_line):
new_data.append(bytes[i*bytes_per_line:(i+1)*bytes_per_line] + '\x00' * extra_padding)

return ''.join(new_data)

def _toqclass_helper(im):
data = None
Expand Down Expand Up @@ -123,7 +150,7 @@ def _toqclass_helper(im):
raise ValueError("unsupported image mode %r" % im.mode)

# must keep a reference, or Qt will crash!
__data = data or im.tobytes()
__data = data or align8to32(im.tobytes(), im.size[0], im.mode)
return {
'data': __data, 'im': im, 'format': format, 'colortable': colortable
}
Expand Down
Binary file added Tests/images/7x13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 17 additions & 6 deletions Tests/test_image_fromqimage.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
from helper import unittest, PillowTestCase, hopper
from test_imageqt import PillowQtTestCase

from PIL import ImageQt
from PIL import ImageQt, Image


class TestFromQImage(PillowQtTestCase, PillowTestCase):

files_to_test = [
hopper(),
Image.open('Tests/images/transparent.png'),
Image.open('Tests/images/7x13.png'),
]

def roundtrip(self, expected):
result = ImageQt.fromqimage(expected.toqimage())
# Qt saves all images as rgb
self.assert_image_equal(result, expected.convert('RGB'))

def test_sanity_1(self):
self.roundtrip(hopper('1'))
for im in self.files_to_test:
self.roundtrip(im.convert('1'))

def test_sanity_rgb(self):
self.roundtrip(hopper('RGB'))
for im in self.files_to_test:
self.roundtrip(im.convert('RGB'))

def test_sanity_rgba(self):
self.roundtrip(hopper('RGBA'))
for im in self.files_to_test:
self.roundtrip(im.convert('RGBA'))

def test_sanity_l(self):
self.roundtrip(hopper('L'))
for im in self.files_to_test:
self.roundtrip(im.convert('L'))

def test_sanity_p(self):
self.roundtrip(hopper('P'))
for im in self.files_to_test:
self.roundtrip(im.convert('P'))


if __name__ == '__main__':
Expand Down