本文整理汇总了Python中PyQt5.QtCore.QByteArray.data方法的典型用法代码示例。如果您正苦于以下问题:Python QByteArray.data方法的具体用法?Python QByteArray.data怎么用?Python QByteArray.data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtCore.QByteArray
的用法示例。
在下文中一共展示了QByteArray.data方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read
# 需要导入模块: from PyQt5.QtCore import QByteArray [as 别名]
# 或者: from PyQt5.QtCore.QByteArray import data [as 别名]
def read(self, fileName):
uncompressed = QByteArray()
# Read data
f = QFile(fileName)
if (f.open(QIODevice.ReadOnly)) :
compressed = f.readAll()
f.close()
uncompressed, length = decompress(compressed, 48 * 48)
# Check the data
if (uncompressed.count() != 48 * 48) :
self.mError = self.tr("This is not a valid Droidcraft map file!")
return None
uncompressed = uncompressed.data()
# Build 48 x 48 map
# Create a Map -> Create a Tileset -> Add Tileset to map
# -> Create a TileLayer -> Fill layer -> Add TileLayer to Map
map = Map(Map.Orientation.Orthogonal, 48, 48, 32, 32)
mapTileset = Tileset.create("tileset", 32, 32)
mapTileset.loadFromImage(QImage(":/tileset.png"), "tileset.png")
map.addTileset(mapTileset)
# Fill layer
mapLayer = TileLayer("map", 0, 0, 48, 48)
# Load
for i in range(0, 48 * 48):
tileFile = int(uncompressed[i])&0xff
y = int(i / 48)
x = i - (48 * y)
tile = mapTileset.tileAt(tileFile)
mapLayer.setCell(x, y, Cell(tile))
map.addLayer(mapLayer)
return map
示例2: readData
# 需要导入模块: from PyQt5.QtCore import QByteArray [as 别名]
# 或者: from PyQt5.QtCore.QByteArray import data [as 别名]
def readData(self, maxlen):
data = QByteArray()
total = 0
while maxlen > total:
chunk = min(self.m_buffer.size() - self.m_pos, maxlen - total)
data.append(self.m_buffer.mid(self.m_pos, chunk))
self.m_pos = (self.m_pos + chunk) % self.m_buffer.size()
total += chunk
return data.data()
示例3: image_to_data
# 需要导入模块: from PyQt5.QtCore import QByteArray [as 别名]
# 或者: from PyQt5.QtCore.QByteArray import data [as 别名]
def image_to_data(img, compression_quality=95, fmt='JPEG', png_compression_level=9, jpeg_optimized=True, jpeg_progressive=False):
'''
Serialize image to bytestring in the specified format.
:param compression_quality: is for JPEG and goes from 0 to 100. 100 being lowest compression, highest image quality
:param png_compression_level: is for PNG and goes from 0-9. 9 being highest compression.
:param jpeg_optimized: Turns on the 'optimize' option for libjpeg which losslessly reduce file size
:param jpeg_progressive: Turns on the 'progressive scan' option for libjpeg which allows JPEG images to be downloaded in streaming fashion
'''
fmt = fmt.upper()
ba = QByteArray()
buf = QBuffer(ba)
buf.open(QBuffer.WriteOnly)
if fmt == 'GIF':
w = QImageWriter(buf, b'PNG')
w.setQuality(90)
if not w.write(img):
raise ValueError('Failed to export image as ' + fmt + ' with error: ' + w.errorString())
from PIL import Image
im = Image.open(BytesIO(ba.data()))
buf = BytesIO()
im.save(buf, 'gif')
return buf.getvalue()
is_jpeg = fmt in ('JPG', 'JPEG')
w = QImageWriter(buf, fmt.encode('ascii'))
if is_jpeg:
if img.hasAlphaChannel():
img = blend_image(img)
# QImageWriter only gained the following options in Qt 5.5
if jpeg_optimized:
w.setOptimizedWrite(True)
if jpeg_progressive:
w.setProgressiveScanWrite(True)
w.setQuality(compression_quality)
elif fmt == 'PNG':
cl = min(9, max(0, png_compression_level))
w.setQuality(10 * (9-cl))
if not w.write(img):
raise ValueError('Failed to export image as ' + fmt + ' with error: ' + w.errorString())
return ba.data()
示例4: getScreenAsData
# 需要导入模块: from PyQt5.QtCore import QByteArray [as 别名]
# 或者: from PyQt5.QtCore.QByteArray import data [as 别名]
def getScreenAsData(self, quality = 20):
ss = self.screen.grabWindow(self.sid);
#b = ss.toImage().mirrored(True, True);
#ss.convertFromImage(b);
painter = QPainter();
painter.begin(ss);
painter.setBrush(QColor( 255,0, 0))
size = 8;
half_size = size / 2;
cx, cy = self.getMouseXY();
painter.drawEllipse(cx - half_size, cy - half_size, size,size);
painter.end();
#
bytesArray = QByteArray();
buff = QBuffer(bytesArray);
ss.save(buff, 'JPEG', quality = quality)
data = bytesArray.data();
return data;
示例5: encode_jpeg
# 需要导入模块: from PyQt5.QtCore import QByteArray [as 别名]
# 或者: from PyQt5.QtCore.QByteArray import data [as 别名]
def encode_jpeg(file_path, quality=80):
from calibre.utils.speedups import ReadOnlyFileBuffer
quality = max(0, min(100, int(quality)))
exe = get_exe_path('cjpeg')
cmd = [exe] + '-optimize -progressive -maxmemory 100M -quality'.split() + [str(quality)]
img = QImage()
if not img.load(file_path):
raise ValueError('%s is not a valid image file' % file_path)
ba = QByteArray()
buf = QBuffer(ba)
buf.open(QBuffer.WriteOnly)
if not img.save(buf, 'PPM'):
raise ValueError('Failed to export image to PPM')
return run_optimizer(file_path, cmd, as_filter=True, input_data=ReadOnlyFileBuffer(ba.data()))