在python(PIL)中,将一个图像集中在另一个图像的顶部,然后将一个图像隐藏在另一个图像中
这项任务是使两个函数,一个是将灰度图像嵌入到RGB函数中。第二个则显示隐藏的“水印”。我已经完成了任务(代码必须通过15个测试,我全部通过了:D),但是如果可能的话,我想整理我的代码。有人告诉我,我本可以用裁剪函数将水印放在照片顶部的中心位置(我不知道怎么做),而不是用数学逻辑在python(PIL)中,将一个图像集中在另一个图像的顶部,然后将一个图像隐藏在另一个图像中,python,image,watermark,Python,Image,Watermark,这项任务是使两个函数,一个是将灰度图像嵌入到RGB函数中。第二个则显示隐藏的“水印”。我已经完成了任务(代码必须通过15个测试,我全部通过了:D),但是如果可能的话,我想整理我的代码。有人告诉我,我本可以用裁剪函数将水印放在照片顶部的中心位置(我不知道怎么做),而不是用数学逻辑 from PIL import Image #function for embedding the watermark def add_watermark(clean_file, watermark_file): #i
from PIL import Image
#function for embedding the watermark
def add_watermark(clean_file, watermark_file):
#if the files are not compatible
#or doesn't exist end the function
try:
clean_photo = Image.open(clean_file)
watermark_photo = Image.open(watermark_file)
except:
return False
#the images pixels tuple values are attained
clean_photo_size = clean_photo.size
watermark_photo_size = watermark_photo.size
# .load was used to return a pixel access object
# that can be used to read and modify pixels
#for both images
photo_array = clean_photo.load()
watermark_array = watermark_photo.load()
#centring the watermarked image on the photo
start_x_coord=int((clean_photo_size[0]-watermark_photo_size[0])/2)
start_y_coord=int((clean_photo_size[1]-watermark_photo_size[1])/2)
#for the pixels that share the same position as the pixels in the
#watermark, manipulate their RGB tuple value to include the watermarks
#greyscale value as part of the original RGB tuple
for line in range(watermark_photo_size[0]):
for rank in range(watermark_photo_size[1]):
photo_array [(start_x_coord+line),\
(start_y_coord+rank)]\
=(int(((photo_array [(start_x_coord+line),\
(start_y_coord+rank)][0]/10)*10)\
+((watermark_array[line,rank]/100)%10)),\
int(((photo_array [(start_x_coord+line),\
(start_y_coord+rank)]\
[1]/10)*10)+((watermark_array[line,rank]/10)%10)),\
int(((photo_array [(start_x_coord+line),\
(start_y_coord+rank)][2]/10)*10)\
+(watermark_array[line,rank]%10)))
#create a new file name with _X at the end
new_File_Name = clean_file[:-4]+"_X"+clean_file[-4:]
#create a new file
clean_photo.save(new_File_Name)
#return true for the test
return True
我试图更正这篇文章的拼写和语法,但我已经醒了20个小时了,因此如果我遗漏了任何内容,我向您道歉。以下是如何将图像水印置于另一幅图像之上的方法: 注意:Image和ImageEnhanced要求将PIL系统库安装到Python运行时
import os
from os.path import join, exists
import shutil
import Image
class BatchWatermark:
def __init__(self):
self.data = []
def process(self, infolder, outfolder, watermarkImage):
if not exists(infolder):
print("Input folder '" + infolder + "' does not exist")
raise Exception("Input folder '" + infolder + "' does not exist")
#if exists(outfolder):
# shutil.rmtree(outfolder)
watermark = Image.open(watermarkImage)
for root, dirs, files in os.walk(infolder):
for name in files:
try:
im = Image.open(join(root, name))
if im.mode != 'RGBA':
im = im.convert('RGBA')
layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
wm_x, wm_y = watermark.size[0], watermark.size[1]
x = (im.size[0] - wm_x)/2
y = (im.size[1] - wm_y)/2
position = (x, y)
layer.paste(watermark, position)
if not exists(outfolder):
os.makedirs(outfolder)
Image.composite(layer, im, layer).save(join(outfolder, name))
print "Watermark added to image file '" + outfolder + "/" + name + "'";
except Exception, (msg):
print msg