I have this perfect Python Pillow Code that edits each frame of a GIF file with some colors and saves it
def process_image(filename, color_depth):
original = Image.open(filename)
new = []
for frame_num in range(original.n_frames):
original.seek(frame_num)
new_frame = Image.new('RGBA', original.size)
new_frame.paste(original)
new_frame = new_frame.convert(mode='P', palette=Image.ADAPTIVE, colors=color_depth)
new.append(new_frame)
new[0].save('Images/new.gif', append_images=new[1:], save_all=True, loop=0)
And I have this code that edits a single image and adds a watermark on it
def Image_Watermark(directory, filename):
#Opening Image & Creating New Text Layer
img = Image.open(directory + "/" + filename).convert("RGBA")
txt = Image.new('RGBA', img.size, (255,255,255,0))
#Creating Text
text = "WATERMARK EXAMPLE"
font = ImageFont.truetype("arial.ttf", 27)
#Creating Draw Object
draw = ImageDraw.Draw(txt)
#Positioning of Text
width, height = img.size
# textwidth, textheight = d.textsize(text, font)
# x=width/2-textwidth/2
# y=height-textheight-300
# Loop for Multiple Watermarks
y=200
for i in range(7):
x=random.randint(0, width-300)
y+=random.randrange(0,int(height/8), 19)+random.randint(0,100)
draw.text((x,y), text, fill=(255,255,255, 75), font=font)
#Combining both layers and saving new image
watermarked = Image.alpha_composite(img, txt)
watermarked.save(filename)
Now the first code edits the gifs perfectly but i dont want that color change , instead i want what the second code does, adding a watermark , to each frame of the gif and save it.
It seems like a simple combination of both codes that I need to do but I cant figure out how, could someone help me out on how to get this done?
Thanks!
Put both functions in the same file (with the Image_Watermark()
function declared above process_image()
)
Edit the Image_Watermark function to get image from process_image instead of reading from a file:
def Image_Watermark(img):
# use img passed as argument instead of reading from a file
txt = Image.new('RGBA', img.size, (255,255,255,0))
#Creating Text
text = "WATERMARK EXAMPLE"
font = ImageFont.truetype("arial.ttf", 27)
#Creating Draw Object
draw = ImageDraw.Draw(txt)
#Positioning of Text
width, height = img.size
# textwidth, textheight = d.textsize(text, font)
# x=width/2-textwidth/2
# y=height-textheight-300
# Loop for Multiple Watermarks
y=200
for i in range(7):
x=random.randint(0, width-300)
y+=random.randrange(0,int(height/8), 19)+random.randint(0,100)
draw.text((x,y), text, fill=(255,255,255, 75), font=font)
#Combining both layers and return the new image
watermarked = Image.alpha_composite(img, txt)
return watermarked
Modify the process_image to the following:
def process_image(filename):
original = Image.open(filename)
new = []
for frame_num in range(original.n_frames):
original.seek(frame_num)
new_frame = Image.new('RGBA', original.size)
new_frame.paste(original)
# call the watermark function instead of changing colors
new_frame = Image_Watermark(new_frame)
new.append(new_frame)
new[0].save('Images/new.gif', append_images=new[1:], save_all=True, loop=0)
Call the process_image(image_file_path)
function with the path to your file.
NOTE: I haven't tested if it's working. If you face any errors, please post them in a comment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.