Python - JPG to Transparent PNG
Personally I am writing a lot of ppt and creating illustrations for various purpose. In may cases, I wanted to get many images with transparent background (like transparent png). In most case, this used to be the way I got those images.
Find images from Google. Trying to get the image with transparent background
However I was facing difficulties as below
- A lot of images that I liked required license / payment
- A lot of images that I liked does not have transparent background
Now with many of free AI image generation tools, the way I am working with the image became as follows
- Create image with AI image generation tool (in most case, I tell AI to generate images with "white background")
- Go to on-line tool to convert an image to transparent png
This new workflow helps me a lot but still I was facing difficulties to the second step (i.e, the step of producing transparaent image). Without purchasing licese, the converted image should be used only for personal purpose. Another problem was that I need to convert the images manually which is tedious job.
Then a flashing idea popped up. Why don't I ask chatGPT to write a python script for converting jpg to transparent png.
|
Jpg2Tpng.py |
|
from PIL import Image
def color_distance(c1, c2): return sum((c1_i - c2_i) ** 2 for c1_i, c2_i in zip(c1, c2)) ** 0.5
def jpg_to_png_with_transparency(input_jpg, output_png, transparency_color=(255, 255, 255), similarity_percentage=0): jpg_image = Image.open(input_jpg) png_image = Image.new("RGBA", jpg_image.size, (0, 0, 0, 0))
max_color_distance = color_distance((0, 0, 0), (255, 255, 255)) threshold_distance = max_color_distance * similarity_percentage / 100
for x in range(jpg_image.size[0]): for y in range(jpg_image.size[1]): pixel_color = jpg_image.getpixel((x, y)) distance = color_distance(pixel_color, transparency_color)
if distance > threshold_distance: png_image.putpixel((x, y), pixel_color + (255,))
png_image.save(output_png)
# Example usage input_jpg = "C:\\temp\\angry_bear_01.jpeg" output_png = "C:\\temp\\angry_bear_01.png" transparency_color = (255, 255, 255) # Set the color you want to be transparent (default is white) similarity_percentage = 5 # Set the similarity percentage threshold (default is 0) jpg_to_png_with_transparency(input_jpg, output_png, transparency_color, similarity_percentage) |
|
jpg2TpngAll.py |
|
import os from PIL import Image
def color_distance(c1, c2): return sum((c1_i - c2_i) ** 2 for c1_i, c2_i in zip(c1, c2)) ** 0.5
def jpg_to_png_with_transparency(input_jpg, output_png, transparency_color=(255, 255, 255), similarity_percentage=0): jpg_image = Image.open(input_jpg) png_image = Image.new("RGBA", jpg_image.size, (0, 0, 0, 0))
max_color_distance = color_distance((0, 0, 0), (255, 255, 255)) threshold_distance = max_color_distance * similarity_percentage / 100
for x in range(jpg_image.size[0]): for y in range(jpg_image.size[1]): pixel_color = jpg_image.getpixel((x, y)) distance = color_distance(pixel_color, transparency_color)
if distance > threshold_distance: png_image.putpixel((x, y), pixel_color + (255,))
png_image.save(output_png)
def batch_convert_jpg_to_png(input_folder, output_folder, transparency_color=(255, 255, 255), similarity_percentage=0): if not os.path.exists(output_folder): os.makedirs(output_folder)
for file in os.listdir(input_folder): if file.lower().endswith(".jpg") or file.lower().endswith(".jpeg"): input_jpg = os.path.join(input_folder, file) output_png = os.path.join(output_folder, os.path.splitext(file)[0] + ".png")
if not os.path.exists(output_png): jpg_to_png_with_transparency(input_jpg, output_png, transparency_color, similarity_percentage) print(f"File {file} conversion done!!!") else: print(f"File {file} already exists in the destination directory. Skipping...")
# Example usage input_folder = "C:\\Original" output_folder = "C:\\Transparent" transparency_color = (255, 255, 255) # Set the color you want to be transparent (default is white) similarity_percentage = 5 # Set the similarity percentage threshold (default is 0) batch_convert_jpg_to_png(input_folder, output_folder, transparency_color, similarity_percentage) |