This article will help you stand out from your competitor by displaying a better product images and enhancing your product branding proporsition.
TL;DR: for complete Jupyter notebook code in this article, you can obtain it here
Setting up Different Image Dimension
Scenario: Your e-commerce platform requires you to upload product images with specific dimensions.
The most common problem of multiple e-commerce is that different platforms (or even across your e-commerce website) have different product sizes and dimension requirements, it is tedious to resize every image to fit into each different platform.
Luckily with the power of python image manipulation, we can easily generate the product images in a single script in few seconds! Here how we gonna do it:
Put every product images into a folder, for example as below as "images" folder:
Run this python code:
from PIL import Image
import os, sys
#State your folder directory
path = "images/"
dirs = os.listdir(path)
#Set your desired image height and width
new_image_height = 500
new_image_width = 500
#Function for resizing images
def resize_aspect_fit():
for item in dirs:
if os.path.isfile(path+item):
image = Image.open(path+item)
file_path, extension = os.path.splitext(path+item)
imResize = image.resize((new_image_height,new_image_width), Image.ANTIALIAS)
imResize.save(file_path+ '-resized'+str(new_image_height)+"-"+str(new_image_width)+'.jpg', 'JPEG', quality=90)
#Run your function
resize_aspect_fit()
From the script, you only need to modify 2 parameters, 1 is the path to your image folder, another parameter is the desired image height and width, in new_image_height, new_image_width, after you run the function, there will be a nicely resized photo in the same folder as below:
Watermarking Product Photos
Scenario: You want to prevent your competitor to use your photo as theirs, you need to watermark all your 1000+ product photos.
Preventing other competitors to steal away your product photoshopping efforts by watermarking your product image is essential, using software to do it is possible, but using python gives you more flexibility and control of the whole image manipulation process.
To do it, firstly, prepare your watermark, preferably in .png format as it has transparency. save this photo into the "Watermark" folder
Run this python script:
from PIL import Image, ImageDraw, ImageFont, ImageFilter
#Define image path, same as above
path = "images/"
dirs = os.listdir(path)
#Standardizing the image size for watermarking
new_image_height = 500
new_image_width = 500
#Define watermark function
def watermark():
for item in dirs:
if os.path.isfile(path+item):
watermark = Image.open("Watermark/watermark.png").resize((150,150), Image.ANTIALIAS)
print(item)
image = Image.open(path+item).resize((new_image_height, new_image_width), Image.ANTIALIAS)
image.paste(watermark,(350,350),watermark)
file_path, extension = os.path.splitext(path+item)
image.save(file_path+ '-watermarked'+'.jpg', 'JPEG', quality=90)
#Run the function
watermark()
You will get a watermarked product image as below after running the script:
It is easier if you use photoshop to set the alpha transparency of the watermark then proceed with batch watermarking.
Product Frame Generation
Scenario: You have multiple products on sales and you want to make a sales picture with photo and price in a template.
Record your product name and price in excel as below, save it as a .csv (productList.csv) file.
Prepare a template frame and product photos, in my case, it is as below:
Code:
#Load the csv file
dfProduct = pd.read_csv("productList.csv")
#Change the product to a list for looping
records = dfProduct.to_dict(orient='record')
#Set the font
font = ImageFont.truetype("OpenSans-Semibold.ttf", size=65)
#Function for edit, adjust the x,y accordingly to your template
def generate_card(data):
template = Image.open("Images/frame.png")
pic = Image.open(f"Images/{data['Name']}.jpg").resize((380, 380), Image.ANTIALIAS)
template.paste(pic,(130,90))
draw = ImageDraw.Draw(template)
draw.text((120, 380), data['Name'].upper(), font=font, fill='black')
draw.text((250, 485), "RM "+str(data['Price']), font=font, fill='white')
return template
#Loop across the csv file and save as png
for record in records:
card = generate_card(record)
card.save(f"Generated/{record['Name']}.png")
The result is generated photo in the folder "generated"
This trick will become handy when you have thousands of products SKU, if you hiring 1 person to do everything, it will be very tedious and it might take that person 1 week to complete this task which can be done in less than 20 lines of codes. Moreover, you can use this script to help enrich your social media posting too. I will be writing more on this topic in my next few articles.
Final words:
Supercharging your product images means giving you a higher probability to stand out from your competitor, it gives your product a unique proposition to your buyer and enhances their shopping experience especially when you are selling on e-commerce platforms such as Amazon, Lazada, or Alibaba.
Using python solution save your business money to search for unique software that fits your business need and save your employee time with the reduction of efforts to make the image manually.
Hope this guide can help your business save time and money. There are more trick such as automated emails, automated data analytic, scheduled task, and even you can automate your clicks and mouse function, be sure to follow me for more productivity hacks articles such as below:
Lastly, Thank you for your time in reading my article!
For the complete Jupyter notebook code in this article, you can obtain it here. Star this GitHub repository as I will upload all my posting code here.