python隐藏乌龟_python - 乌龟形状的图像处理后,Python龟图章神秘地消失 - 堆栈内存溢出...

取向:

我创建了以下函数以允许用户将乌龟更改为他/她选择的图像,然后在任何时候将其标记到画布:

def TurtleShape():

try:

# Tkinter buttons related to turtle manipulation

manipulateimage.config(state = NORMAL)

flipButton.config(state = NORMAL)

mirrorButton.config(state = NORMAL)

originalButton.config(state = NORMAL)

resetturtle.config(state = NORMAL)

rotateButton.config(state = NORMAL)

# Ask user for file name from tkinter file dialog, and return file name as `klob`

global klob

klob = filedialog.askopenfilename()

global im

# Open `klob` and return as `im`

im = Image.open(klob)

# Append `im` to pictures deque

pictures.append(im)

# Clear `edited` deque

edited.clear()

# Save `im` as an image, then register image as shape, and finally set image as turtle shape

im.save(klob + '.gif', "GIF")

register_shape(klob + '.gif')

shape(klob + '.gif')

update()

except:

# If user selects cancel in file dialog, then pass

pass

def StampPic():

stamp()

draw_space() # Go forward 100 pixels with pen up after every stamp

update()

图像也可以通过以下其他功能操作给用户选择:

调整大小功能 - 此功能可用作第一或第二功能。 首先意味着它最初被调用,而次要意味着它编辑已编辑的图像。 因此,如果仅首先调用此函数,则此函数将附加到pictures deque的pictures ,调整其大小,并将编辑后的图像输出为.gif图像,这将是乌龟的新形状。 但是,如果连续调用两次或更多次,由于多次调整同一图片大小会导致图像失真的问题,我不得不创建另一个deque jiop ,它可以保存pictures双端的原始项目,每当此函数连续多次调用,每次调整原始图像的大小,而不是每次调整相同的图像。 但是,如果仅作为辅助函数调用,则该函数将简单地从edited双端队列中获取当前图像,调整该图像的大小,然后将其设置为乌龟的新形状:

def TurtleImageResize():

if not hasattr(TurtleImageResize, "counter"):

TurtleImageResize.counter = 0

TurtleImageResize.counter += 1

# width = original size of image

width = im.size[0]

# height = original height of image

height = im.size[1]

# Allow user to enter new width for image

NewOne2 = numinput('Width of Image', 'Set the width of the image: ', minval = 1)

# Allow user to enter new height for image

NewOne = numinput('Height of Image', 'Set the height of your image: ', minval = 1)

# Set width to user input if user input is NOT nothing. Otherwise, use `width` as picture width.

Picwidth = NewOne2 if NewOne2 != None else width

# Set height to user input if user input is NOT None. Otherwise, use `height` as picture height.

Picheight = NewOne if NewOne != None else height

try:

# Secondary Step: Take ORIGINAL image appended to `jiop` (from `except:` code block succeeding `try:` code block) and resize THAT image each time this function is called twice in a row. Otherwise, if ONLY called as a secondary step, take previously edited image from `edited` deque, resize that, and append newly edited image to the `edited` deque.

try:

# `jiop` is a deque

hye = jiop.pop()

jiop.append(hye)

print("Jiop")

except:

hye = edited.pop()

jiop.append(hye)

print("Edited")

# Resize Image to Picwidth and Picheight

editpic = hye.resize((int(Picwidth), int(Picheight)), Image.ANTIALIAS)

edited.append(editpic)

print("Hooplah!")

except:

# Intial step: Take image appended to `pictures` deque from `TurtleShape` function, then edit that and append newly edited image to both `editpic` and `pictures`

geer = pictures.pop()

# Resize Image to Picwidth and Picheight

editpic = geer.resize((int(Picwidth), int(Picheight)), Image.ANTIALIAS)

jiop.append(geer)

edited.append(editpic)

pictures.append(editpic)

print("Normal")

# Save image as `.gif`

editpic.save(klob + str(TurtleImageResize.counter) + '.gif', 'GIF')

# Register image as a shape, and use it as shape of turtle

register_shape(klob + str(TurtleImageResize.counter) + '.gif')

shape(klob + str(TurtleImageResize.counter) + '.gif')

update()

翻转,旋转和镜像功能 - 这些功能比上面的调整大小功能更简单。 如果最初调用它们,它们将从pictures双端队列中获取图像,操纵它,将编辑后的图像附加到edited双端队列,然后将乌龟“形状”更改为该新图像。 但是,如果调用第二个,它们将从edited双端队列中获取图像,操纵它,将操纵的图像重新附加回edited双端队列,然后将其设置为乌龟的新“形状”。 这些功能如下所示:

def flippic():

if not hasattr(flippic, "counter"):

flippic.counter = 0

flippic.counter += 1

try:

# Secondary step: Take previously edited image from `edited` deque, manipulate that, and append newly edited image to the `edited` deque

jiop.clear()

ghy = edited.pop()

# Flip image over horizontal line

kpl = ImageOps.flip(ghy)

edited.append(kpl)

print("Jlop")

except:

# Initial step: Take image appended to `pictures` deque from `TurtleShape` function, then edit that and append newly edited image to both `editpic` and `pictures`

neer = pictures.pop()

# Flip image over horizontal line

kpl = ImageOps.flip(neer)

pictures.append(kpl)

edited.append(kpl)

print("Yup")

# Save image as `.gif`

kpl.save(klob + str(flippic.counter) + '.gif', "GIF")

# Register image as a shape, and use it as shape of turtle

register_shape(klob + str(flippic.counter) + '.gif')

shape(klob + str(flippic.counter) + '.gif')

update()

def mirror():

if not hasattr(mirror, "counter"):

mirror.counter = 0

mirror.counter += 1

try:

jiop.clear()

jui = edited.pop()

# Flip image over vertical line

fgrt = ImageOps.mirror(jui)

edited.append(fgrt)

except:

bbc = pictures.pop()

# Flip image over vertical line

fgrt = ImageOps.mirror(bbc)

pictures.append(fgrt)

edited.append(fgrt)

fgrt.save(klob + str(mirror.counter) + ".gif")

register_shape(klob + str(mirror.counter) + ".gif")

shape(klob + str(mirror.counter) + ".gif")

update()

def rotatePic():

if not hasattr(rotatePic, "counter"):

rotatePic.counter = 0

rotatePic.counter += 1

try:

jiop.clear()

lmcb = edited.pop()

# Rotate image 90º right

fetch = lmcb.rotate(-90, expand = True)

edited.append(fetch)

except:

bolt = pictures.pop()

# Rotate image 90º right

fetch = bolt.rotate(-90, expand = True)

pictures.append(fetch)

edited.append(fetch)

fetch.save(klob + str(rotatePic.counter) + ".gif")

register_shape(klob + str(rotatePic.counter) + ".gif")

shape(klob + str(rotatePic.counter) + ".gif")

update()

这样,所有编辑功能在基本相同的基本图像上一起工作。

问题:

现在,考虑用户想要拍摄乌龟图像,然后将其大小调整为大小,例如800x400,并将其标记到画布上的特定位置。 之后,用户决定将乌龟图像移动到画布上的另一个位置,翻转图像,然后在那里标记图像。 现在应该有两张图片吧? 一个盖章,另一个翻转? 但是,由于某些原因,我的程序并非如此 。 相反,标记的图像在用户翻转乌龟图像时消失 ,即使在任何地方都没有找到clear()函数(告诉你我的意思,请参阅下面的编辑 )。 显然,这个问题只发生在调用TurtleImageResize函数之后。

我的TurtleImageResize函数有什么问题导致了这个问题? 我已经完全改变了龟形状的图像管理过程到现在的状态,希望它能解决我在以前的设置中遇到的这个问题,但显然,情况并非如此。 因此,非常感谢任何有关此问题的帮助!

编辑:下面是一个最小,完整,可验证的方式来重现我遇到的问题(必须安装PIL(或Pillow)和GhostScript才能使其工作) :

import os,shutil,subprocess, sys

her = sys.platform

if her == "win32":

print("Windows is your Operating System")

win_gs = ["gs","gswin32c","gswin64c"]

if all( shutil.which(gs_version) is None for gs_version in win_gs ):

paths = ["C:\\Program Files\\gs\\gs9.18\\bin","C:\\Program Files (x86)\\gs\\gs9.18\\bin"]

for path in (x for x in paths if os.path.exists(x)):

os.environ["PATH"] += ";" + path

break

if any( shutil.which(gs_version) for gs_version in win_gs ):

print("GhostScript 9.18 for Windows found and utilized")

else:

print("You do not have GhostScript 9.18 installed for Windows. Please install it.")

sys.exit(0)

else:

print("GhostScript 9.18 for Windows found and utilized")

elif her == 'darwin':

print("Macintosh is your Operating System")

if shutil.which("gs") is None:

os.environ["PATH"] += ":/usr/local/bin"

if shutil.which("gs") is None:

print("You do not have GhostScript installed for Macintosh. Please install it.")

sys.exit(0)

else:

print("GhostScript for Macintosh found and utilized")

from turtle import *

from tkinter import *

try:

import tkinter.filedialog as filedialog

except ImportError:

pass

import collections

from PIL import Image, ImageEnhance, ImageOps

jiop = collections.deque()

pictures = collections.deque()

edited = collections.deque()

picwidth = collections.deque()

picheight = collections.deque()

def draw_space():

# Draw a space 200 pixels wide.

penup()

forward(200)

pendown()

def TurtleShape():

try:

manipulateimage.config(state = NORMAL)

flipButton.config(state = NORMAL)

mirrorButton.config(state = NORMAL)

rotateButton.config(state = NORMAL)

global klob

klob = filedialog.askopenfilename()

global im

im = Image.open(klob)

pictures.append(im)

edited.clear()

im.save(klob + '.gif', "GIF")

register_shape(klob + '.gif')

shape(klob + '.gif')

update()

except AttributeError:

pass

def TurtleImageResize():

if not hasattr(TurtleImageResize, "counter"):

TurtleImageResize.counter = 0

TurtleImageResize.counter += 1

width = im.size[0]

height = im.size[1]

NewOne2 = numinput('Width of Image', 'Set the width of the image: ', minval = 1)

NewOne = numinput('Height of Image', 'Set the height of your image: ', minval = 1)

Picwidth = NewOne2 if NewOne2 != None else width

picwidth.append(Picwidth)

Picheight = NewOne if NewOne != None else height

picheight.append(Picheight)

try:

try:

hye = jiop.pop()

jiop.append(hye)

except:

hye = edited.pop()

jiop.append(hye)

editpic = hye.resize((int(Picwidth), int(Picheight)), Image.ANTIALIAS)

edited.append(editpic)

pictures.append(editpic)

except:

geer = pictures.pop()

editpic = geer.resize((int(Picwidth), int(Picheight)), Image.ANTIALIAS)

jiop.append(geer)

edited.append(editpic)

pictures.append(editpic)

editpic.save(klob + str(TurtleImageResize.counter) + '.gif', 'GIF')

register_shape(klob + str(TurtleImageResize.counter) + '.gif')

shape(klob + str(TurtleImageResize.counter) + '.gif')

update()

def flippic():

if not hasattr(flippic, "counter"):

flippic.counter = 0

flippic.counter += 1

try:

jiop.clear()

ghy = edited.pop()

kpl = ImageOps.flip(ghy)

edited.append(kpl)

pictures.append(kpl)

print("Jlop")

except:

neer = pictures.pop()

kpl = ImageOps.flip(neer)

pictures.append(kpl)

edited.append(kpl)

print("Yup")

kpl.save(klob + str(flippic.counter) + '.gif', "GIF")

register_shape(klob + str(flippic.counter) + '.gif')

shape(klob + str(flippic.counter) + '.gif')

update()

def mirror():

if not hasattr(mirror, "counter"):

mirror.counter = 0

mirror.counter += 1

try:

jiop.clear()

jui = edited.pop()

fgrt = ImageOps.mirror(jui)

edited.append(fgrt)

pictures.append(fgrt)

except:

bbc = pictures.pop()

fgrt = ImageOps.mirror(bbc)

pictures.append(fgrt)

edited.append(fgrt)

fgrt.save(klob + str(mirror.counter) + ".gif")

register_shape(klob + str(mirror.counter) + ".gif")

shape(klob + str(mirror.counter) + ".gif")

update()

def rotatePic():

if not hasattr(rotatePic, "counter"):

rotatePic.counter = 0

rotatePic.counter += 1

try:

jiop.clear()

lmcb = edited.pop()

fetch = lmcb.rotate(-90, expand = True)

edited.append(fetch)

pictures.append(fetch)

except:

bolt = pictures.pop()

fetch = bolt.rotate(-90, expand = True)

pictures.append(fetch)

edited.append(fetch)

fetch.save(klob + str(rotatePic.counter) + ".gif")

register_shape(klob + str(rotatePic.counter) + ".gif")

shape(klob + str(rotatePic.counter) + ".gif")

update()

def StampPic():

stamp()

draw_space()

update()

def move_turtle():

# Pick up the turtle and move it to its starting location.

penup()

goto(-200, 100)

pendown()

def settings():

# Tkinter buttons

turtlepic = Button(text = "Set Turtle Image", command = TurtleShape)

turtlepic.pack(side = 'left')

stampimage = Button(text = "Stamp", command = StampPic)

stampimage.pack(side = 'left')

global manipulateimage

manipulateimage = Button(text = "Resize Turtle Image", command = TurtleImageResize, state = DISABLED)

manipulateimage.pack(side = 'left')

global flipButton

flipButton = Button(text = "Flip image", command = flippic, state = DISABLED)

flipButton.pack(side = 'left')

global mirrorButton

mirrorButton = Button(text = "Mirror Image", command = mirror, state = DISABLED)

mirrorButton.pack(side = 'left')

global rotateButton

rotateButton = Button(text = "Rotate Image", command = rotatePic, state = DISABLED)

rotateButton.pack(side = 'left')

def skip(x, y):

penup()

goto(x, y)

pendown()

update()

move_turtle()

settings()

speed(0)

tracer(0, 0)

onscreenclick(skip)

if sys.platform == 'win32':

input()

else:

pass

当/如果您的系统上同时安装了GhostScript和PIL(或Pillow),要重现我的问题,请执行以下操作( 除步骤#4 外 所需的所有步骤):

单击窗口底部的Set Turtle Image按钮,选择您想要龟的任何图像,然后按Open 。 乌龟被设置为该图像。

通过按屏幕底部的Resize turtle Image按钮,将Resize turtle Image为800x400(或任何其他大小)。 将连续弹出两个对话框。 在第一个对话框中输入宽度800(或您自己的宽度),然后在第二个对话框中输入高度400(或您自己的高度),完成后,图像将根据提供的尺寸更改大小(或者根据您是否按下取消将图像设置回原始尺寸。

选择窗口底部的Stamp按钮。 图像被标记在画布上,并且乌龟在标记图像“后面”向前移动400个像素。

可选:单击画布上的任意位置以将乌龟带到该位置。

翻转/镜像/旋转图像。

正如您所看到的,在完成所有这些操作后,就像您翻转/镜像/旋转图像一样,标记的图像就会消失 。 我的TurtleImageResize函数有什么问题导致这种情况发生?

编辑#2:为了防止这些信息有用,我在操作系统版本为10.11.2(El Capitan)的Macintosh上运行Python 3.5.1。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值