Python Image 模块,FLIP_TOP_BottOM 实例源码
我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用Image.FLIP_TOP_BottOM。
def decode(self, file, filename):
try:
image = Image.open(file)
except Exception as e:
raise ImageDecodeException(
'PIL cannot read %r: %s' % (filename or file, e))
try:
image = image.transpose(Image.FLIP_TOP_BottOM)
except Exception as e:
raise ImageDecodeException(
'PIL Failed to transpose %r: %s' % (filename or file, e))
# Convert bitmap and palette images to component
if image.mode in ('1', 'P'):
image = image.convert()
if image.mode not in ('L', 'LA', 'RGB', 'RGBA'):
raise ImageDecodeException('Unsupported mode "%s"' % image.mode)
type = GL_UNSIGNED_BYTE
width, height = image.size
# tostring is deprecated,replaced by tobytes in Pillow (PIL fork)
# (1.1.7) PIL still uses it
image_data_fn = getattr(image, "tobytes", getattr(image, "tostring"))
return ImageData(width, height, image.mode, image_data_fn())
def exif_orientation(im):
"""
Rotate and/or flip an image to respect the image's EXIF orientation data.
"""
try:
exif = im._getexif()
except Exception:
# There are many ways that _getexif fails,we're just going to blanket
# cover them all.
exif = None
if exif:
orientation = exif.get(0x0112)
if orientation == 2:
im = im.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
im = im.rotate(180)
elif orientation == 4:
im = im.transpose(Image.FLIP_TOP_BottOM)
elif orientation == 5:
im = im.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 6:
im = im.rotate(-90)
elif orientation == 7:
im = im.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 8:
im = im.rotate(90)
return im
def _CorrectOrientation(self, image, orientation):
"""Use PIL to correct the image orientation based on its EXIF.
See JEITA CP-3451 at http://www.exif.org/specifications.html,
Exif 2.2,page 18.
Args:
image: source PIL.Image.Image object.
orientation: integer in range (1,8) inclusive,corresponding the image
orientation from EXIF.
Returns:
PIL.Image.Image with transforms performed on it. If no correction was
done,it returns the input image.
"""
if orientation == 2:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
image = image.rotate(180)
elif orientation == 4:
image = image.transpose(Image.FLIP_TOP_BottOM)
elif orientation == 5:
image = image.transpose(Image.FLIP_TOP_BottOM)
image = image.rotate(270)
elif orientation == 6:
image = image.rotate(270)
elif orientation == 7:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
image = image.rotate(270)
elif orientation == 8:
image = image.rotate(90)
return image
def getmask2(self, text, mode="", fill=Image.core.fill):
size, offset = self.font.getsize(text)
im = fill("L", size, 0)
self.font.render(text, im.id, mode=="1")
return im, offset
##
# Wrapper that creates a transposed font from any existing font
# object.
#
# @param font A font object.
# @param orientation An optional orientation. If given,this should
# be one of Image.FLIP_LEFT_RIGHT,Image.FLIP_TOP_BottOM,
# Image.ROTATE_90,Image.ROTATE_180,or Image.ROTATE_270.
def save(self,file=None):
self.w.update() # force image on screen to be current before saving it
pstring = glreadPixels(0,0,self.xpixels,self.ypixels,
GL_RGBA,GL_UNSIGNED_BYTE)
snapshot = Image.fromstring("RGBA",(self.xpixels,self.ypixels),pstring)
snapshot = snapshot.transpose(Image.FLIP_TOP_BottOM)
if not file: file = self.file
snapshot.save(file + ".png")
# --------------------------------------------------------------------
def add_reflection(im, bgcolor="#00000", amount=0.4, opacity=0.6):
""" Returns the supplied PIL Image (im) with a reflection effect
bgcolor The background color of the reflection gradient
amount The height of the reflection as a percentage of the orignal image
opacity The initial opacity of the reflection gradient
Originally written for the Photologue image management system for Django
and Based on the original concept by Bernd Schlapsi
"""
# convert bgcolor string to rgb value
background_color = ImageColor.getrgb(bgcolor)
# copy orignial image and flip the orientation
reflection = im.copy().transpose(Image.FLIP_TOP_BottOM)
# create a new image filled with the bgcolor the same size
background = Image.new("RGB", im.size, background_color)
# calculate our alpha mask
start = int(255 - (255 * opacity)) # The start of our gradient
steps = int(255 * amount) # the number of intermedite values
increment = (255 - start) / float(steps)
mask = Image.new('L', (1, 255))
for y in range(255):
if y < steps:
val = int(y * increment + start)
else:
val = 255
mask.putpixel((0, y), val)
alpha_mask = mask.resize(im.size)
# merge the reflection onto our background color using the alpha mask
reflection = Image.composite(background, reflection, alpha_mask)
# crop the reflection
reflection_height = int(im.size[1] * amount)
reflection = reflection.crop((0, 0, im.size[0], reflection_height))
# create new image sized to hold both the original image and the reflection
composite = Image.new("RGB", (im.size[0], im.size[1] + reflection_height), background_color)
# paste the orignal image and the reflection into the composite image
composite.paste(im, (0, 0))
composite.paste(reflection, im.size[1]))
# return the image complete with reflection effect
return composite