Python 缩放图片的一部分
我想放大图片的一部分,在这个例子中是鼻子 我有一个功能来选择我想放大的那部分图片Python 缩放图片的一部分,python,jython,image-scaling,jes,Python,Jython,Image Scaling,Jes,我想放大图片的一部分,在这个例子中是鼻子 我有一个功能来选择我想放大的那部分图片 def copyAndPaste(picture): height = getHeight(picture) width = getWidth(picture) newPicture = makeEmptyPicture(width, height) for x in range(width): for y in range(height): pxl = getPixel(pic
def copyAndPaste(picture):
height = getHeight(picture)
width = getWidth(picture)
newPicture = makeEmptyPicture(width, height)
for x in range(width):
for y in range(height):
pxl = getPixel(picture,x,y)
if (x>48 and x<59) and (y>58 and y<71):
newPxl =getPixel(newPicture, #?,#?)
else:
newPxl = getPixel(newPicture, x,y)
color = getColor(pxl)
setColor(newPxl,color)
return newPicture
def d():
f=pickAFile()
picture=makePicture(f)
newPicture = copyAndPaste(picture)
writePictureTo(newPicture, r"D:\FOLDER\0Pic4.jpg")
explore (newPicture)
例如。
发件人:
致:
我已经尝试了很多方法,但无法解决如何将两者结合起来放大图片的一部分,而让图片的其余部分保持原样
这就是最终的画面应该看起来很可笑的样子
我一直在小图像上练习,因为程序可能需要很长时间才能执行,在这个阶段,处理较大的图像是不可行的,这意味着结果是粗略的,但至少会显示它是否有效。我仍然不确定我是否理解您试图做的事情,但我认为是这样的:你想要复制粘贴鼻子,而不是剪切粘贴,你想要粘贴的副本以与第二个例子相同的特殊方式加倍 所以,面部中部会有一个10x10的鼻孔,再加上20x20的鼻子被冲到右下角。 首先,要复制和粘贴,您只需将像素复制到新旧位置,而不是仅复制到新位置:
def copyAndPaste(picture):
height = getHeight(picture)
width = getWidth(picture)
newPicture = makeEmptyPicture(width+100, height+100)
for x in range(width):
for y in range(height):
pxl = getPixel(picture,x,y)
color = getColor(pxl)
if (x>48 and x<59) and (y>58 and y<71):
newPxl =getPixel(newPicture, x+100,y+100)
setColor(newPxl,color)
newPxl = getPixel(newPicture, x,y)
setColor(newPxl,color)
现在,要放大新粘贴的副本,只需将偏移量加倍。换句话说,49,59处的第一个像素变为149159,但50,60处的像素变为151161,51,61处的像素变为153163,依此类推
所以,你想要的是得到49,59的距离,加倍,再加回到49,59,然后移动100100:
if (x>48 and x<59) and (y>58 and y<71):
newPxl =getPixel(newPicture, (x-49)*2+49+100,(y-59)*2+59+100)
setColor(newPxl,color)
我仍然不确定我是否理解你想做什么,但我认为是这样的:你想复制并粘贴鼻子,而不是剪切和粘贴,你希望粘贴的副本以与第二个示例相同的特殊方式加倍 所以,面部中部会有一个10x10的鼻孔,再加上20x20的鼻子被冲到右下角。 首先,要复制和粘贴,您只需将像素复制到新旧位置,而不是仅复制到新位置:
def copyAndPaste(picture):
height = getHeight(picture)
width = getWidth(picture)
newPicture = makeEmptyPicture(width+100, height+100)
for x in range(width):
for y in range(height):
pxl = getPixel(picture,x,y)
color = getColor(pxl)
if (x>48 and x<59) and (y>58 and y<71):
newPxl =getPixel(newPicture, x+100,y+100)
setColor(newPxl,color)
newPxl = getPixel(newPicture, x,y)
setColor(newPxl,color)
现在,要放大新粘贴的副本,只需将偏移量加倍。换句话说,49,59处的第一个像素变为149159,但50,60处的像素变为151161,51,61处的像素变为153163,依此类推
所以,你想要的是得到49,59的距离,加倍,再加回到49,59,然后移动100100:
if (x>48 and x<59) and (y>58 and y<71):
newPxl =getPixel(newPicture, (x-49)*2+49+100,(y-59)*2+59+100)
setColor(newPxl,color)
这只是为了记录和娱乐,不是答案 但如前所述,abarnert你确定他们只希望你为每个复制的像素保留3个白色像素,而不是将同一像素复制4次吗?这是一个非常荒谬的缩放算法 更有趣但最基本的缩放图像的方法是 徖 其他有趣的算法 以下是Eagle算法的一个基本实现,它适用于不同颜色数量较少的图像:
def EnlargeEagle(picture):
w = getWidth(picture)
h = getHeight(picture)
w2 = getWidth(picture)*2
h2 = getHeight(picture)*2
newPicture = makeEmptyPicture(w2, h2)
x2 = 0
for x in range(1, w-1):
y2 = 0
for y in range(1, h-1):
oldPxS = getPixel(picture, x-1, y-1)
oldPxT = getPixel(picture, x, y-1)
oldPxU = getPixel(picture, x+1, y-1)
oldPxV = getPixel(picture, x-1, y)
oldPxC = getPixel(picture, x, y)
oldPxW = getPixel(picture, x+1, y)
oldPxX = getPixel(picture, x-1, y+1)
oldPxY = getPixel(picture, x, y+1)
oldPxZ = getPixel(picture, x+1, y+1)
newPx1 = getPixel(newPicture, x2, y2)
newPx2 = getPixel(newPicture, x2+1, y2)
newPx3 = getPixel(newPicture, x2, y2+1)
newPx4 = getPixel(newPicture, x2+1, y2+1)
# Step 1
c = getColor(oldPxC)
setColor(newPx1, c)
setColor(newPx2, c)
setColor(newPx3, c)
setColor(newPx4, c)
# Step 2
if (getColor(oldPxV) == getColor(oldPxS)) and (getColor(oldPxS) == getColor(oldPxT)):
setColor(newPx1, getColor(oldPxS))
if (getColor(oldPxT) == getColor(oldPxU)) and (getColor(oldPxU) == getColor(oldPxW)):
setColor(newPx2, getColor(oldPxU))
if (getColor(oldPxV) == getColor(oldPxX)) and (getColor(oldPxX) == getColor(oldPxY)):
setColor(newPx3, getColor(oldPxX))
if (getColor(oldPxW) == getColor(oldPxZ)) and (getColor(oldPxZ) == getColor(oldPxY)):
setColor(newPx4, getColor(oldPxZ))
y2 += 2
x2 += 2
原件:
最近邻居:
鹰:
享受吧 这只是为了记录和娱乐,不是答案 但如前所述,abarnert你确定他们只希望你为每个复制的像素保留3个白色像素,而不是将同一像素复制4次吗?这是一个非常荒谬的缩放算法 更有趣但最基本的缩放图像的方法是 徖 其他有趣的算法 以下是Eagle算法的一个基本实现,它适用于不同颜色数量较少的图像:
def EnlargeEagle(picture):
w = getWidth(picture)
h = getHeight(picture)
w2 = getWidth(picture)*2
h2 = getHeight(picture)*2
newPicture = makeEmptyPicture(w2, h2)
x2 = 0
for x in range(1, w-1):
y2 = 0
for y in range(1, h-1):
oldPxS = getPixel(picture, x-1, y-1)
oldPxT = getPixel(picture, x, y-1)
oldPxU = getPixel(picture, x+1, y-1)
oldPxV = getPixel(picture, x-1, y)
oldPxC = getPixel(picture, x, y)
oldPxW = getPixel(picture, x+1, y)
oldPxX = getPixel(picture, x-1, y+1)
oldPxY = getPixel(picture, x, y+1)
oldPxZ = getPixel(picture, x+1, y+1)
newPx1 = getPixel(newPicture, x2, y2)
newPx2 = getPixel(newPicture, x2+1, y2)
newPx3 = getPixel(newPicture, x2, y2+1)
newPx4 = getPixel(newPicture, x2+1, y2+1)
# Step 1
c = getColor(oldPxC)
setColor(newPx1, c)
setColor(newPx2, c)
setColor(newPx3, c)
setColor(newPx4, c)
# Step 2
if (getColor(oldPxV) == getColor(oldPxS)) and (getColor(oldPxS) == getColor(oldPxT)):
setColor(newPx1, getColor(oldPxS))
if (getColor(oldPxT) == getColor(oldPxU)) and (getColor(oldPxU) == getColor(oldPxW)):
setColor(newPx2, getColor(oldPxU))
if (getColor(oldPxV) == getColor(oldPxX)) and (getColor(oldPxX) == getColor(oldPxY)):
setColor(newPx3, getColor(oldPxX))
if (getColor(oldPxW) == getColor(oldPxZ)) and (getColor(oldPxZ) == getColor(oldPxY)):
setColor(newPx4, getColor(oldPxZ))
y2 += 2
x2 += 2
原件:
最近邻居:
鹰:
享受吧