pip install Pillow
img = Image.new(mode, size, color)
mode: RGB or RGBA from PIL import Image
path = "F:\\testing\\images\\test3.png" # new image to create
img = Image.new("RGB", (400, 200), color="#F1F1CC")
img.save(path)
Image.save(path, format=None, **params)
Image.open(path, mode='r', formats=None)
path: Path of the image to open from PIL import Image
path = "F:\\testing\\images\\cat.png" # existing image to open
img = Image.open(path, mode="r")
img.show()
path = "F:\\testing\\images\\cat.png" # existing image to open
img = Image.open(path)
print(img.format) # Image format
print(img.mode) # pixel mode RGBA
print(img.size) # ( 620,500)
print(img.height) # 500
print(img.width) # 620
print(img.info) # all details about the image.
from PIL import Image
path = "F:\\testing\\images\\cat.png" # existing image to open
path2 = "F:\\testing\\images\\cat2.png" # Path to save
img = Image.open(path)
img2 = img.rotate(45) # rotate by 45 degree
img2.show()
img2.save(path2) # save rotated image
Retaining the size after rotate.
img2 = img.rotate(45, expand=True) # rotate by 45 degree
Image.resize(size, resample=None, box=None, reducing_gap=None)[source]
size: Tuple (width, height), required size.from PIL import Image
path = "F:\\testing\\images\\jump.jpeg" # Path to Image
path2 = "F:\\testing\\images\\jump_resize.jpeg" # resized
img = Image.open(path, mode="r") # create image object
img.show() # display
img_new = img.resize((200, 200)) # resize the image
print(img.size) # original image size ( width , height)
print(img_new.size) # resized image size ( width, height)
img_new.show() # display resized image
img_new.save(path2) # save resized image
from PIL import Image
path = "F:\\testing\\images\\jump.jpeg" # Path to Image
path2 = "F:\\testing\\images\\jump_thumbnail.jpeg" # resized
img = Image.open(path, mode="r") # create image object
img.show() # display
print(img.size) # main image size ( width, height)
img.thumbnail((200, 200)) # max width, max height
img.save(path2) # save thumbnail image
print(img.size) # thumbnail size ( width, height)
img.show() # display resized image
Above code will print two outputs as sizes, note how the aspect ratio is maintained while creating Thumbnail image.
(1024, 768)
(200, 150)
Thumbnails using Pillow and Tkinter GUI
Image.crop(box=None)
box : the rectangle or tuple( left,upper, right, lower )
from PIL import Image
path = "F:\\testing\\images\\cat-crop.jpeg" # Path to Image
path2 = "F:\\testing\\images\\cat-crop2.jpeg" # new image
img = Image.open(path, mode="r") # create image object
img.show() # display
print(img.size) # main image size ( width, height)
box = (20, 65, 470, 550) # rectangle for crop
img_crop = img.crop(box) # Cropped image object
img_crop.save(path2) # save cropped image
print(img_crop.size) # cropped image size ( width, height)
img_crop.show() # display cropped image
Check the size of images, compare with the rectangle ( box ) input given to the crop().
(768, 1024)
(450, 485)
pip install rembg
Full code is here
from rembg import remove
from PIL import Image
input_path = 'D:\\testing\\images\\yanam-raj.png'
output_path = 'D:\\testing\\images\\out1.png'
input = Image.open(input_path)
output = remove(input)
output.save(output_path)
ImageDraw to add Line, rectangle, circle to an Image.
ImageDraw to add Text to an Image. ImageFilter
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.