lundi, juin 01, 2020

Crop and Animated gif: using the Pillow library

# example of cropping an image
from PIL import Image
from PIL import ImageEnhance
# load image
image = Image.open('opera_house.jpg')
image.show()
# create a cropped image
cropped = image.crop((100, 100, 200, 200))
# show cropped image
cropped.show()
cropped.save('opera_house_cropped.gif', format='GIF')
#load the image again and inspect the format
image2 = Image.open('opera_house_cropped.gif')
print(image2.format)
#Enhance the contrast
enh = ImageEnhance.Contrast(image)
enh.enhance(1.8).show("80% more contrast")
#Create an animated gif
rotation = 0
names =['45.gif', '90.gif', '135.gif', '180.gif', '225.gif', '270.gif', '315.gif', '360.gif']
im = []
for i in range(0, len(names)):
im.append(cropped.rotate(rotation))
im[i].save(names[i])
rotation += 45
images = []
for n in names:
frame = Image.open(n)
images.append(frame)
images[0].save('anitest.gif', save_all=True, append_images=images[1:], duration=100, loop=0)

Aucun commentaire: