Python JES中图像与图像和文本叠加的代码
我需要帮助,使代码,将覆盖图像上的图像和文字上的图像在JES 由于在图像上叠加图像、图像上文本、上下缩放文本和图像以及透明度级别方面的知识有限,我无法开始 如果有人愿意帮助我,那就太好了。谢谢Python JES中图像与图像和文本叠加的代码,python,Python,我需要帮助,使代码,将覆盖图像上的图像和文字上的图像在JES 由于在图像上叠加图像、图像上文本、上下缩放文本和图像以及透明度级别方面的知识有限,我无法开始 如果有人愿意帮助我,那就太好了。谢谢 def scale(picture,factor): newHeight = int(factor*getHeight(picture))+1 newWidth = int(factor*getWidth(picture))+1 returnPic = makeEmptyPicture(int
def scale(picture,factor):
newHeight = int(factor*getHeight(picture))+1
newWidth = int(factor*getWidth(picture))+1
returnPic = makeEmptyPicture(int(newWidth),int(newHeight))
sx = 0
for tx in range(0,newWidth):
sy = 0
for ty in range(0,newHeight):
if (int(sx) < getWidth(picture)) and (int(sy) < getHeight(picture)):
sp = getPixel(picture,int(sx),int(sy))
tp = getPixel(returnPic,tx,ty)
setColor(tp,getColor(sp))
sy = sy + (1/factor)
sx = sx + (1/factor)
show(returnPic)
def grayScaleNew():
picture = makePicture("forbidden-city.jpg")
for px in getPixels(picture):
newRed = getRed(px) * 0.2121
newGreen = getGreen(px) * 0.7152
newBlue = getBlue(px) * 0.0722
luminance = newRed + newGreen + newBlue
setColor(px,makeColor(luminance,luminance,luminance))
def testTransparentOverlay():
# This function reads in two pictures and produces a new picture that
# is made by copying picture 1 and then overlaying picture 2 on top
# of picture 1. (It is placed in the centre of the image)
# This requires the width and height of picture 2 to be less than
# the width and height of picture 2 (so it fits). So it checks
# that the size of each image is correct before creating the new picture.
# 1. Get the two pictures and show them
file = pickAFile()
picture1 = makePicture(file)
file = pickAFile()
picture2 = makePicture(file)
show(picture1)
show(picture2)
#2. Test the size of the pictures - picture 2 needs to fit inside
# picture 1 (so check width and height)
if getWidth(picture2) > getWidth(picture1):
errorMessageWidth = "ERROR: The width of picture2 is greater than picture1"
print(errorMessageWidth)
elif getHeight(picture2) > getHeight(picture1):
errorMessageWidth = "ERROR: The width of picture2 is greater than picture1"
print(errorMessageWidth)
else: #do the overlay and show the result
#work out the position for overlaying first
xMargin = (getWidth(picture1) - getWidth(picture2)) / 2
yMargin = (getHeight(picture1) - getHeight(picture2)) / 2
proportion = 60 #try 60% transparency
picture3 = transparency(picture1, picture2, xMargin, yMargin, proportion)
show(picture3)
def transparency(picture1, picture2, xStart, yStart, proportion):
# overlays picture 2 in the centre of picture 1 with
# a transparency of proportion (%)
# returns a new picture - copy of picture 1 with the overlay
# (there are no side effects)
picture3 = duplicatePicture(picture1)
# calculate the proportions of overlay and the base image
# keep any decimal places until after calculations
overlay = (proportion) / 100.0 # convert overlay from percentage to a fraction
# eg. 60% is 60/100 which is 0.6
base = (100-proportion) / 100.0 # convert base amount from percentage to a fraction
# eg. if overlap is 60% is (100-60) / 100 which is 0.5
#Copy all the pixels from picture 2 onto the new picture 3
for x in range(0, getWidth(picture2)):
for y in range(0, getHeight(picture2)):
pixel2 = getPixel(picture2, x, y) # pixel to copy (and colours)
pixel2Red = getRed(pixel2)
pixel2Green = getGreen(pixel2)
pixel2Blue = getBlue(pixel2)
pixel3 = getPixel(picture3, x+xStart, y+yStart) # overlap pixel (and colours)
pixel3Red = getRed(pixel3)
pixel3Green = getGreen(pixel3)
pixel3Blue = getBlue(pixel3)
#combine the colours in correct proportions - convert back to integers
newRed = int(pixel2Red * overlay) + int(pixel3Red * base)
newGreen = int(pixel2Green * overlay) + int(pixel3Green* base)
newBlue = int(pixel2Blue * overlay) + int(pixel3Blue * base)
#set the new colours for picture3
setRed(pixel3, newRed)
setGreen(pixel3, newGreen)
setBlue(pixel3, newBlue)
return picture3
def刻度(图片、系数):
newHeight=int(系数*getHeight(图片))+1
newWidth=int(系数*getWidth(图片))+1
returnPic=makeEmptyPicture(int(newWidth)、int(newHeight))
sx=0
对于范围内的发送(0,新宽度):
sy=0
对于范围内的ty(0,新高度):
如果(int(sx)getWidth(图1):
errorMessageWidth=“错误:图片2的宽度大于图片1”
打印(错误消息宽度)
elif getHeight(图2)>getHeight(图1):
errorMessageWidth=“错误:图片2的宽度大于图片1”
打印(错误消息宽度)
否则:#进行叠加并显示结果
#首先确定叠加的位置
xMargin=(getWidth(图片1)-getWidth(图片2))/2
yMargin=(getHeight(图1)-getHeight(图2))/2
比例=60#尝试60%的透明度
picture3=透明度(picture1、picture2、xMargin、yMargin、比例)
表演(图3)
def透明度(图片1、图片2、xStart、yStart、比例):
#将图片1中央的图片2与
#a透明度比例(%)
#返回一个新图片-图片1的副本和覆盖
#(没有副作用)
图片3=重复图片(图片1)
#计算覆盖和基础图像的比例
#保留任何小数位,直到计算完成
叠加=(比例)/100.0#将叠加从百分比转换为分数
#60%是60/100,即0.6
基数=(100比例)/100.0#将基数从百分比转换为分数
#例如,如果重叠为60%,则为(100-60)/100,即0.5
#将图片2中的所有像素复制到新图片3上
对于范围(0,getWidth(picture2))中的x:
对于范围(0,getHeight(图2))中的y:
pixel2=getPixel(图片2,x,y)#要复制的像素(和颜色)
pixel2Red=getRed(pixel2)
pixel2Green=getGreen(pixel2)
pixel2Blue=getBlue(pixel2)
pixel3=getPixel(图片3,x+x开始,y+y开始)#重叠像素(和颜色)
pixel3Red=getRed(pixel3)
pixel3Green=getGreen(pixel3)
pixel3Blue=getBlue(pixel3)
#按正确的比例组合颜色-转换回整数
newRed=int(像素2red*叠加)+int(像素3red*基)
newGreen=int(像素2绿色*叠加)+int(像素3绿色*基础)
新蓝=整数(像素2蓝*叠加)+整数(像素3蓝*基)
#为图片3设置新颜色
设置红色(像素3,新红色)
设置绿色(像素3,新绿色)
退蓝(像素3,新蓝)
返回图片3
这些都是我写的代码,但我不确定如何将它们组合在一起,使其作为一个整体工作。如果这有意义的话
谢谢。首先,我将引用您请求帮助的INFT1004作业 “特别是,您应该尽量不要使用来自外部来源的代码或算法,也不要向讲师以外的人寻求帮助,因为这可能会妨碍您掌握这些概念。” 在本作业中明确指出,你不应该在网上询问或使用你在网上找到或要求的代码,这违反了纽卡素大学学术诚信准则——你知道在开始课程之前你做了一个模块。
这篇文章的副本将被发送给课程讲师。您到底需要什么帮助?你写过任何代码吗?是的,我写过,我写过灰度代码,将图像混合到中心,透明度强度和缩放图像,但我尝试将它们全部放在一起,但似乎没有完成任务…它停止很短…好像我能够在中心覆盖图像并对图像进行灰度缩放,但似乎就是这样,它不会放大或缩小图像,透明度也不会放大或缩小。除非你提供一个。我们帮不上忙。好的,我已经提供了我写的所有代码,我只是不确定如何将它们组合在一起,使其作为一个整体工作。哦,我明白了,但显然我没有得到任何答案