How To Scale Images Using OpenPyXL?

Method

img2=img.resize((300,200))

Sample Code

#Scaling Transformation

from openpyxl import Workbook
from openpyxl.drawing.image import Image as xlImage
from PIL import Image as pilImage

#Create a workbook
wb=Workbook()
#Get the active worksheet
ws=wb.active

# Open the original image file
img=pilImage.open('pic.jpg')

#Apply a scaling transformation to the image
img2=img.resize((300,200))

# Save the transformed image as a temporary file
path='image3.jpg'
img2.save(path)

# Add the image to the worksheet
img=xlImage(path)
ws.add_image(img, 'A1')

# Save the workbook
wb.save('image05.xlsx')

# Delete the temporary file
import os
os.remove(path)
Scale Images Using OpenPyXL

Leave a Reply

Your email address will not be published. Required fields are marked *