我正在尝试将水印应用于照片。我有一个黑色和一个白色。我正在尝试打开图像,然后将每个水印分别应用于照片,然后保存两个版本。因此,最终产品将是原始图像中的两个,一个仅使用黑色水印,另一个采用白色。我的问题是应用两个水印并将它们保存在那里。
from tkinter import *
from PIL import Image
from PIL import ImageFilter
from PIL import ImageTk
import os
from msvcrt import getch
def watermark(event):
blackWatermark = Image.open("Watermark(Black).png")
whiteWatermark = Image.open("Watermark(White).png")
fileLocation = str(fileEntry.get())
#window = Tk()
#window.title("Preview Window")
count = 0
for f in os.listdir(fileLocation):
if f.endswith(".JPG") or f.endswith(".jpg"):
#Opening the Image
imageAddress = str(fileLocation)+ "\\" + str(f)
print(str(imageAddress))
im1 = im2 = Image.open(imageAddress)
print("Image Opened: "+str(f))
#setting the watermark size
wmSize = im1.size
print("Image Size: "+str(wmSize))
wNew = whiteWatermark.resize(wmSize)
print("White Watermark Size: "+str(wNew.size))
bNew = blackWatermark.resize(wmSize)
print("Black Watermark Size: "+str(bNew.size))
im1.paste(wNew,(0,0),wNew)
print("White Watermark Placed")
im2.paste(bNew,(0,0),bNew)
print("Black Watermark Placed")
saveLocation = r"C:\Users\Rockw\Desktop\Python\Modified Photos"
fileName = "\WM("+ str(count) +").JPG"
saveName = saveLocation + fileName
print("White Watermark Save Name: "+saveName)
im1.save(saveName)
count+=1
fileName = "\WM("+ str(count) +").JPG"
saveName = saveLocation + fileName
print("Black Watermark Save Name: "+saveName)
im2.save(saveName)
print("New Images Saved")
print("------------------------------")
count+=1
print("All Files Edited")
root = Tk()
fileLabel = Label(root,text="File Location").grid(row=0,column=0,sticky=W)
fileEntry = Entry(root,width=50)
fileEntry.grid(row=0,column=1,sticky=W)
watermarkButton = Button(root,text="Watermark")
watermarkButton.grid(row=1,column=0,sticky=W)
watermarkButton.bind("<Button-1>",watermark)
bwButton = Button(root,text="Black and White").grid(row=1,column=1,sticky=W)
root.mainloop()
这是代码..我道歉并不是非常漂亮
编辑:添加了所有代码,而不是之前的代码片段