The PIL(Python Imaging Library) is the python library which primarily uses for
for the image manipulation like create, resize, merge etc.
This blog post contains the code snippets related to PIL.
How to resize an image
import os from PIL import Image img = Image.open("original.png") #Open the original Image size = img.size img = img.resize(size,Image.ANTIALIAS) #Resize the original Image img.save("result.jpg",optimize=True,quality=95) #Save the result image
==>How to add transparent image watermarkup on an image:
import os from PIL import Image img = Image.open("original.jpg") #Open the original Image width, height = img.size #Get the image width, height image_mode = img.mode #Get the image mode watermark = Image.open('watermark_image.jpg').convert("RGBA") # watermark = watermark.resize((width, height), Image.ANTIALIAS) position_left,position_top = 10 ,10 #Watermark position position=(position_left,position_top) orientation = 'horizontal' #Watermark orientation if orientation=='vertical': # In case vertical, rotate the image by 45 degree angle = 45 watermark = watermark.rotate( angle, expand=1 ) img.paste(watermark ,position,watermark) else: img.paste(im = watermark,box= position,mask=0) transparent = Image.new(mode = 'RGBA', size = (width, height), color = (0,0,0,0)) # Create a new transparent image transparent.paste(img, (0,0)) # paste the original image transparent.paste(watermark, position, mask=watermark) # paste the watermark image if image_mode=='RGB': transparent = transparent.convert(image_mode) else: transparent = transparent.convert('P') transparent.save("result.jpg",optimize=True,quality=95) #Save the result image
==>How to add text watermarkup on an image:
from PIL import Image from PIL import Image from PIL import ImageEnhance from PIL import ImageDraw, ImageFont img = Image.open("original.jpg") margin_left,margin_top = img.size width, height = img.size image_mode = img.mode position_left,position_top = 10 ,10 position=(position_left,position_top) fill = (0,0,0) #Watermark text color watermark = Image.new("RGBA", img.size, color = (0, 0, 0, 0)) #Create a Watermark image waterdraw = ImageDraw.ImageDraw(watermark, "RGBA") #Create a waterdraw for text font_path = 'Lato-HaiIta-webfont.ttf' #Image font path url txt = 'example.com' #Watermark text size = max(1, 100) ##################################### # this part will reduce the watermark text size fontsize = 1 img_fraction = .40 font_obj = ImageFont.truetype(font_path, 1) while font_obj.getsize(txt)[0] < img_fraction*margin_left: fontsize += 1 font_obj = ImageFont.truetype(font_path, fontsize) fontsize -= 1 ##################################### font_obj = ImageFont.truetype(font_path, fontsize) #Create the font obj with fonth type and size waterdraw.text(position, text = txt, fill=fill,font=font_obj ) positioning = 'horizontal' if positioning=='vertical': angle = 45 watermark = watermark.rotate( angle, expand=1 ) transparent_mode = 'RGBA' if image_mode=='P' else 'RGB' transparent = Image.new(mode = transparent_mode, size =img.size, color = (0,0,0,0)) #Create the new image(result image) transparent.paste(img, (0,0)) #Paste the original image transparent.paste(watermark, None, watermark) #Paste the watermark image transparent.save("final.jpg",optimize=True,quality=95) #Save the result image
That’s all for today. I hope this blog will help you. I’d be very grateful if you’d write your opinions, comments, and suggestions to keep the page updated and interesting.
You may also like our post on Using StringIO and BytesIO for managing data as the file object.
Thank you very much for your work, I was looking for this
Mick,