本文整理匯總了Python中PIL.Image.frombuffer方法的典型用法代碼示例。如果您正苦於以下問題:Python Image.frombuffer方法的具體用法?Python Image.frombuffer怎麽用?Python Image.frombuffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類PIL.Image
的用法示例。
在下文中一共展示了Image.frombuffer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: displayImage
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def displayImage(image):
global grey_palette
l_height = SCREEN_HEIGHT
l_width = SCREEN_WIDTH
pgImage = pygame.image.frombuffer(image.tobytes(), image.size, 'P' )
pgImage.set_palette(grey_palette)
i_width = pgImage.get_width()
i_height = pgImage.get_height()
pgImage = pygame.transform.scale( pgImage, (int(i_width*(1.0*l_height/i_height)), l_height) )
screen.fill(backgroundColor)
screen.blit(pgImage, ((l_width - pgImage.get_width())/2, (l_height-pgImage.get_height())/2))
pygame.display.flip()
示例2: writeMetaData
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def writeMetaData(imgname, tag, replace):
try:
metaData = {}
imgFile = Image.open(imgname)
imgData = imgFile.getdata()
print "Getting meta data..."
info = imgFile._getexif()
if info:
print "found meta data!"
i = 0
for (tag, value) in info.items():
i = i + 1
tagname = TAGS.get(tag, tag)
#if tagname == tagToReplace:
#info.items()[i][1] = replace
print "Saving Change"
outImg = Image.frombuffer("RGBX",len(imgData)+len(info),imgData+info)
outImg.save(img)
except:
print "Failed"
示例3: write
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def write(self, s):
global lcd
image = Image.frombuffer('L', P_SIZE, s, "raw", 'L', 0, 1)
image = image.crop((self.x, 0, self.x+1, P_HEIGHT))
self.image_scan.paste(image,(self.x, 0))
if self.x < P_WIDTH-1:
self.x += 1
image = ImageOps.invert(self.image_scan)
image.thumbnail(S_SIZE, Image.NEAREST)
image = image.convert('1')
lcd.write(image.tobytes())
示例4: write
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def write(self, s):
if self.mode == SCAN_MODE:
image = Image.frombuffer('L', PRINTER_SIZE, s, "raw", 'L', 0, 1)
image = image.crop((self.x, 0, self.x+1, PRINTER_HEIGHT))
self.image_stack.paste(image,(self.x, 0))
if self.x < PRINTER_WIDTH-1:
self.x += 1
else:
self.scanDone = True
print("spent for 640 lines: ", time.time()-self.lastTime)
if self.mode == SCAN_MODE_FIX:
image = Image.frombuffer('L', PRINTER_SIZE, s, "raw", 'L', 0, 1)
image = image.crop((PRINTER_WIDTH/2, 0, (PRINTER_WIDTH/2)+1, PRINTER_HEIGHT))
image_total = Image.new('L', (self.x+1, PRINTER_HEIGHT), 0)
image_total.paste(self.image_stack, (0, 0))
image_total.paste(image,(self.x, 0))
self.image_stack = image_total.copy()
if self.x < 5000:
self.x += 1
else:
self.scanDone = True
print("spent for 5000 lines: ", time.time()-self.lastTime)
示例5: write
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def write(self, s):
global lcd
self.size += len(s)
image = Image.frombuffer('L', (416, 240), s, "raw", 'L', 0, 1)
image = image.crop((0, 0, S_WIDTH, S_HEIGHT))
image = ImageOps.invert(image)
image = image.convert('1')
lcd.write(image.tobytes())
示例6: write
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def write(self, s):
global lcd
b1 = bytearray()
b1.extend(s[:(P_WIDTH*P_HEIGHT)])
mi = min(b1)
ma = max(b1)
ra = ma-mi
b2 = bytearray()
for pix in range(P_WIDTH*P_HEIGHT):
b2.append( (b1[pix]*(255/ra))-mi )
print(max(b1), min(b1), max(b2), min(b2))
image = Image.frombuffer('L', P_SIZE, b2, "raw", 'L', 0, 1)
image.thumbnail(S_SIZE, Image.NEAREST)
# draw = ImageDraw.Draw(image)
# font = ImageFont.truetype('arial.ttf', 18)
#
# draw.rectangle([(0, 0), (115, 22)], fill=255, outline=0)
# draw.text((2, 2), "TESt *", fill='black', font=font)
image = ImageOps.invert(image)
image = image.convert('1')
lcd.write(image.tobytes())
示例7: write
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def write(self, s):
global lcd
image = Image.frombuffer('L', (416, 240), s, "raw", 'L', 0, 1)
image = image.crop((8, 0, S_WIDTH+8, S_HEIGHT))
image = ImageOps.invert(image)
image = image.convert('1')
lcd.write(image.tobytes())
示例8: get_bitmap
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def get_bitmap() -> image:
"""Get and return a bitmap of the Window."""
left, top, right, bot = win32gui.GetWindowRect(Window.id)
w = right - left
h = bot - top
hwnd_dc = win32gui.GetWindowDC(Window.id)
mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc)
save_dc = mfc_dc.CreateCompatibleDC()
save_bitmap = win32ui.CreateBitmap()
save_bitmap.CreateCompatibleBitmap(mfc_dc, w, h)
save_dc.SelectObject(save_bitmap)
windll.user32.PrintWindow(Window.id, save_dc.GetSafeHdc(), 0)
bmpinfo = save_bitmap.GetInfo()
bmpstr = save_bitmap.GetBitmapBits(True)
# This creates an Image object from Pillow
bmp = image.frombuffer('RGB',
(bmpinfo['bmWidth'],
bmpinfo['bmHeight']),
bmpstr, 'raw', 'BGRX', 0, 1)
win32gui.DeleteObject(save_bitmap.GetHandle())
save_dc.DeleteDC()
mfc_dc.DeleteDC()
win32gui.ReleaseDC(Window.id, hwnd_dc)
# bmp.save("asdf.png")
return bmp
示例9: array2PIL
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def array2PIL(arr):
mode = 'RGBA'
shape = arr.shape
arr = arr.reshape(arr.shape[0]*arr.shape[1], arr.shape[2])
if len(arr[0]) == 3:
arr = np.c_[arr, 255*np.ones((len(arr),1), np.uint8)]
return Image.frombuffer(mode, (shape[1], shape[0]), arr.tostring(), 'raw', mode, 0, 1)
示例10: _saveImage
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def _saveImage(img, imgsize, destFile):
mode = 'RGBA'
arr = img.reshape(img.shape[0]*img.shape[1], img.shape[2])
if len(arr[0]) == 3:
arr = np.c_[arr, 255*np.ones((len(arr),1), np.uint8)]
img = Image.frombuffer(mode, imgsize, arr.tostring(), 'raw', mode, 0, 1)
img.save(destFile)
示例11: print_jpg
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def print_jpg(self, filename_or_obj, *args, **kwargs):
"""
Supported kwargs:
*quality*: The image quality, on a scale from 1 (worst) to
95 (best). The default is 95, if not given in the
matplotlibrc file in the savefig.jpeg_quality parameter.
Values above 95 should be avoided; 100 completely
disables the JPEG quantization stage.
*optimize*: If present, indicates that the encoder should
make an extra pass over the image in order to select
optimal encoder settings.
*progressive*: If present, indicates that this image
should be stored as a progressive JPEG file.
"""
from backends.backend_agg import FigureCanvasAgg # lazy import
agg = self.switch_backends(FigureCanvasAgg)
buf, size = agg.print_to_buffer()
if kwargs.pop("dryrun", False):
return
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
options = cbook.restrict_dict(kwargs, ['quality', 'optimize',
'progressive'])
if 'quality' not in options:
options['quality'] = rcParams['savefig.jpeg_quality']
return image.save(filename_or_obj, format='jpeg', **options)
示例12: print_tif
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def print_tif(self, filename_or_obj, *args, **kwargs):
from backends.backend_agg import FigureCanvasAgg # lazy import
agg = self.switch_backends(FigureCanvasAgg)
buf, size = agg.print_to_buffer()
if kwargs.pop("dryrun", False):
return
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
dpi = (self.figure.dpi, self.figure.dpi)
return image.save(filename_or_obj, format='tiff',
dpi=dpi)
示例13: pil_to_array
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def pil_to_array(pilImage):
"""Load a `PIL image`_ and return it as a numpy array.
.. _PIL image: https://pillow.readthedocs.io/en/latest/reference/Image.html
Returns
-------
numpy.array
The array shape depends on the image type:
- (M, N) for grayscale images.
- (M, N, 3) for RGB images.
- (M, N, 4) for RGBA images.
"""
if pilImage.mode in ['RGBA', 'RGBX', 'RGB', 'L']:
# return MxNx4 RGBA, MxNx3 RBA, or MxN luminance array
return np.asarray(pilImage)
elif pilImage.mode.startswith('I;16'):
# return MxN luminance array of uint16
raw = pilImage.tobytes('raw', pilImage.mode)
if pilImage.mode.endswith('B'):
x = np.frombuffer(raw, '>u2')
else:
x = np.frombuffer(raw, '<u2')
return x.reshape(pilImage.size[::-1]).astype('=u2')
else: # try to convert to an rgba image
try:
pilImage = pilImage.convert('RGBA')
except ValueError:
raise RuntimeError('Unknown image mode')
return np.asarray(pilImage) # return MxNx4 RGBA array
示例14: stop_filter
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def stop_filter(self, post_processing):
"""
Save the plot in the current canvas as a image and apply
the *post_processing* function.
def post_processing(image, dpi):
# ny, nx, depth = image.shape
# image (numpy array) has RGBA channels and has a depth of 4.
...
# create a new_image (numpy array of 4 channels, size can be
# different). The resulting image may have offsets from
# lower-left corner of the original image
return new_image, offset_x, offset_y
The saved renderer is restored and the returned image from
post_processing is plotted (using draw_image) on it.
"""
width, height = int(self.width), int(self.height)
buffer, (l, b, w, h) = self.tostring_rgba_minimized()
self._renderer = self._filter_renderers.pop()
self._update_methods()
if w > 0 and h > 0:
img = np.frombuffer(buffer, np.uint8)
img, ox, oy = post_processing(img.reshape((h, w, 4)) / 255.,
self.dpi)
gc = self.new_gc()
if img.dtype.kind == 'f':
img = np.asarray(img * 255., np.uint8)
img = img[::-1]
self._renderer.draw_image(gc, l + ox, height - b - h + oy, img)
示例15: print_tif
# 需要導入模塊: from PIL import Image [as 別名]
# 或者: from PIL.Image import frombuffer [as 別名]
def print_tif(self, filename_or_obj, *args, dryrun=False,
pil_kwargs=None, **kwargs):
buf, size = self.print_to_buffer()
if dryrun:
return
image = Image.frombuffer('RGBA', size, buf, 'raw', 'RGBA', 0, 1)
if pil_kwargs is None:
pil_kwargs = {}
pil_kwargs.setdefault("dpi", (self.figure.dpi, self.figure.dpi))
return image.save(filename_or_obj, format='tiff', **pil_kwargs)