只是分享一种对角镜像方式,从左下角到右上角:

如果他需要从“右上角到左下角”镜像或使用相反的对角线,学生负责适应。

# !! Works only with squared pictures... !!

def diagMirrorPicture(picture):

height = getHeight(picture)

width = getWidth(picture)

if (height != width):

printNow("Error: The input image is not squared. Found [" + \

str(width) + " x " + str(height) + "] !")

return None

newPicture = makeEmptyPicture(width, height)

mirrorPt = 0

for x in range(0, width, 1):

for y in range(mirrorPt, height, 1):

sourcePixel = getPixel(picture, x, y)

color = getColor(sourcePixel)

# Copy bottom-left as is

targetPixel = getPixel(newPicture, x, y)

setColor(targetPixel, color)

# Mirror bottom-left to top right

targetPixel = getPixel(newPicture, y, x)

# ^^^^ (simply invert x and y)

setColor(targetPixel, color)

# Here we shift the mirror point

mirrorPt += 1

return newPicture

file = pickAFile()

picture = makePicture(file)

picture = diagMirrorPicture(picture)

if (picture):

writePictureTo(picture, "/home/mirror-diag2.png")

show(picture)注意:这表现为我们独立地为每行像素操作垂直镜像,沿垂直轴传递mirrorPt点。

输出(由Antoni Tapies绘画):

.................

.................

.............. ....

以下是实验性的,只是为了好玩......

# Draw point, with check if the point is in the image area

def drawPoint(pic, col, x, y):

if (x >= 0) and (x < getWidth(pic)) and (y >= 0) and (y < getHeight(pic)):

px = getPixel(pic, x, y)

setColor(px, col)

# Draw line segment given two points

# From Bresenham's line algorithm :

# http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

def drawLine(pic, col, x0, y0, x1, y1):

dx = abs(x1-x0)

dy = abs(y1-y0)

sx = sy = 0

#sx = 1 if x0 < x1 else -1

#sy = 1 if y0 < y1 else -1

if (x0 < x1):

sx = 1

else:

sx = -1

if (y0 < y1):

sy = 1

else:

sy = -1

err = dx - dy

while (True):

drawPoint(pic, col, x0, y0)

if (x0 == x1) and (y0 == y1):

break

e2 = 2 * err

if (e2 > -dy):

err = err - dy

x0 = x0 + sx

if (x0 == x1) and (y0 == y1):

drawPoint(pic, col, x0, y0)

break

if (e2 < dx):

err = err + dx

y0 = y0 + sy

# Works only with squared cropped areas :

# i.e. in [(x0, y0), (x1, y1)], abs(x1-x0) must be equal to abs(y1-y0)

#

# USAGE :

# * To get bottom reflected to top use x0 > x1

# * To get top reflected to bottom use x0 < x1

def diagCropAndMirrorPicture(pic, startPt, endPt):

w = getWidth(pic)

h = getHeight(pic)

if (startPt[0] < 0) or (startPt[0] >= w) or \

(startPt[1] < 0) or (startPt[1] >= h) or \

(endPt[0] < 0) or (endPt[0] >= w) or \

(endPt[1] < 0) or (endPt[1] >= h):

printNow("Error: The input points must be in the image range !")

return None

new_w = abs(startPt[0] - endPt[0])

new_h = abs(startPt[1] - endPt[1])

if (new_w != new_h):

printNow("Error: The input points do not form a square !")

return None

printNow("Given: (" + str(startPt[0]) + ", " + str(endPt[0]) + ") and (" \

+ str(startPt[1]) + ", " + str(endPt[1]) + ")")

newPicture = makeEmptyPicture(new_w, new_h)

if (startPt[0] < endPt[0]):

offsetX = startPt[0]

switchX = False

switchTB = True

else:

offsetX = endPt[0]

switchX = True

switchTB = False

if (startPt[1] < endPt[1]):

offsetY = startPt[1]

switchY = False

else:

offsetY = endPt[1]

switchY = True

# (switchX XOR switchY)

changeDiag = (switchX != switchY)

mirror_pt = 0

for x in range(0, new_w, 1):

for y in range(mirror_pt, new_h, 1):

#for y in range(0, new_h, 1):

oldX = x

oldY = y

if (switchTB):

sourcePixel = getPixel(picture, offsetX+new_w-1- oldX, offsetY+new_h-1- oldY)

else:

sourcePixel = getPixel(picture, offsetX+oldX, offsetY+oldY)

color = getColor(sourcePixel)

if (changeDiag):

newX = new_w-1 - x

newY = new_h-1 - y

#printNow("Change Diag !")

else:

newX = x

newY = y

# Copied half

if (switchTB):

targetPixel = getPixel(newPicture, new_w-1- x, new_h-1- y)

else:

targetPixel = getPixel(newPicture, x, y)

setColor(targetPixel, color)

# Mirror half (simply invert x and y)

if (switchTB):

targetPixel = getPixel(newPicture, new_h-1- newY, new_w-1- newX)

else:

targetPixel = getPixel(newPicture, newY, newX)

setColor(targetPixel, color)

# Here we shift the mirror point

if (not changeDiag):

mirror_pt += 1

return newPicture

file = pickAFile()

pic = makePicture(file)

picture = makePicture(file)

# Draw working area

drawLine(pic, white, 30, 60, 150, 180)

drawLine(pic, white, 30, 180, 150, 60)

drawLine(pic, black, 30, 60, 30, 180)

drawLine(pic, black, 30, 60, 150, 60)

drawLine(pic, black, 150, 60, 150, 180)

drawLine(pic, black, 30, 180, 150, 180)

show(pic)

writePictureTo(pic, "D:\\pic.png")

# Build cropped and mirrored areas

pic1 = diagCropAndMirrorPicture(picture, (150, 60), (30, 180))

pic2 = diagCropAndMirrorPicture(picture, (30, 180), (150, 60))

pic3 = diagCropAndMirrorPicture(picture, (150, 180), (30, 60))

pic4 = diagCropAndMirrorPicture(picture, (30, 60), (150, 180))

# Show cropped and mirrored areas

if (pic1):

writePictureTo(pic1, "D:\\pic1.png")

show(pic1)

if (pic2):

writePictureTo(pic2, "D:\\pic2.png")

show(pic2)

if (pic3):

writePictureTo(pic3, "D:\\pic3.png")

show(pic3)

if (pic4):

writePictureTo(pic4, "D:\\pic4.png")

show(pic4)

.................................................. ....

............................................. .........

.......

..........

..........

..........

.......

python曲线镜像_在Python中以对角方式镜像图像相关推荐

  1. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  2. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  3. python selenium 下拉列表_从下拉列表中选择python selenium选项

    我试图使用下面的代码使用selenium(python)从网页上的下拉列表中选择一个选项.在from selenium import webdriver from selenium.webdriver ...

  4. python 语料标注_在python中怎么标记文本?

    要运行下面的python程序,必须在系统中安装(NLTK)自然语言工具包. NLTK模块是一个庞大的工具包,旨在帮助您使用整个自然语言处理(NLP)方法. 要安装NLTK,请在终端中运行以下命令.su ...

  5. python pipeline框架_介绍Python的Django框架中的静态资源管理器django-pipeline

    django-pipeline 是一个 Django 下非常方便的静态资源管理 app,尤其是 1.2 版本之后,利用 django-staticfiles 的collectstatic 命令,在开发 ...

  6. python中立方表示_在Python中表示一个对象的方法

    在 Python 中一切都是对象.如果要在 Python 中表示一个对象,除了定义 class 外还有哪些方式呢?我们今天就来盘点一下. 0x00 dict 字典或映射存储 KV 键值对,它对查找.插 ...

  7. python临床数据_从临床试验中获取数据

    我正在开发一个小Python函数来从clinicalTrials.gov中获取数据.从每个研究记录中,我想从中找出研究的目标条件.例如,对于this研究记录,我需要以下内容:conditions = ...

  8. python注入进程_向进程中注入Python代码

    我想把Python代码注入到一个进程中,当它注入时,它似乎会使我的进程崩溃.我没有在我自己的程序中得到任何错误,但目标进程停止工作.被调用的非托管api没有给我任何错误,并且似乎已经正确地执行了它们的 ...

  9. python创建矩阵_在Python中创建矩阵的Python程序

    python创建矩阵 There is no specific data type in Python to create a matrix, we can use list of list to c ...

  10. python预定义_【Python】python类中方法的预定义

    知乎问题: 像这个图片里面显示的,self.prediction首先声明了下然后下面给出了定义,然后optimize又调用这个函数但是没有用self.prediction()这样的方式而是像用变量一样 ...

最新文章

  1. python之互斥锁
  2. getchar的利用
  3. 为什么博导都希望,自己的博士生毕业也去高校当老师?
  4. Python【02】【基础部分】- B
  5. Java笔记-SSLSocket双向认证实例
  6. 美团将主办 ICDAR2019「中文门脸招牌文字识别」比赛,并公开首个真实场景招牌图像数据集...
  7. vue引入id3_使用决策树ID3算法,预测收入是否大于50k
  8. 微软提出Layout,多模态任务也有预训练模型啦
  9. java 面试基础总结(二)---多线程
  10. mysql的关系表_MySQL表关系总结
  11. 铁打的春晚,流水的流量
  12. 优雅代码之巧用 Ramda
  13. 【Unity3D】【UI】Cannot restructure Prefab instance.
  14. 名企面试题个人总结——蘑菇街2017校园招聘(特殊交换)
  15. c语言line函数编写画六边形,canvas 画六边形
  16. 计算机互联网职业高中排名,职业高中有哪些热门专业可选择
  17. 数据结构 插入排序(InsertionSort Sort) 详解 附C++代码实现:
  18. PostGIS教程二:PostGIS的安装
  19. 2021年金属非金属矿山(露天矿山)安全管理人员考试题及金属非金属矿山(露天矿山)安全管理人员考试技巧
  20. 2018-2019-2 网络对抗技 20165328 Exp7: 网络欺诈防范

热门文章

  1. 开源GIS(八)——开源GIS简史
  2. java程序员表情包_听说,这些表情包只有程序员才懂
  3. c++ 调用批处理 bat 清理浏览器缓存。
  4. 详解数字音频接口DAI
  5. arcgis投影坐标转经纬度
  6. vs2010英文版变中文版
  7. html视频全套教程,用Dreamweaver讲解html全套视频教程分享
  8. 安徽省计算机二级水平考试试卷,安徽省计算机二级考试理论试题(附答案)
  9. 手机投屏电视html,手机怎么投屏,怎么把手机里的电影投屏到电视上
  10. sublime php code sniffer,Sublime插件CodeSniffer配置