def extract(self):
width = self.imag['width']
height = self.imag['height']
png_data = []
for y in range(height):
row = []
for x in range(width):
pos = x + (y * width)
if self.has_cv:
# OpenCV keeps a BGRA format internally so swap R and B
rgba = list(self.bmp[pos])
r = rgba[0]
rgba[0] = rgba[2]
rgba[2] = r
row.append(rgba)
else:
for color in self.bmp[x + (y * width)]:
row.append(int(color))
png_data.append(row)
basename = os.path.splitext(os.path.basename(self.filename))[0]
filename = '%s.png' % basename
if self.has_cv:
img = numpy.array(png_data, dtype=numpy.uint8)
swizzle = self.imag['swizzle']
if swizzle == SWIZZLE_ROT_90:
img = self._rotate_image(img, 90, width, height)
elif swizzle == SWIZZLE_TRANSPOSE:
# the lazy transpose... rotate and flip one axis
img = self._rotate_image(img, 90, width, height)
cv2.flip(img, 0, dst=img)
cv2.imwrite(filename, img)
else:
file_ = open(filename, 'wb')
writer = png.Writer(width, height, alpha=True)
writer.write(file_, png_data)
file_.close()
# OpenCV doesn't resize around the center when rotating (since it's just working with matrices)
# so we need to do some lame canvas work to ensure a clean rotation + resize around the center
评论列表
文章目录