gpt4 book ai didi

Python 字节数组 : Function changes other Instance?

转载 作者:行者123 更新时间:2023-12-01 05:37:47 25 4
gpt4 key购买 nike

我正在开发一个程序,该程序打开图像,将其字节读取到字节数组,然后交换随机字节并让您将其保存出来。这样可行。然而,我遇到了一个奇怪的错误,根据我的理解,这个错误不应该出现。

如果我运行 resave() 并取消注释 glitchedbytes 行,im.save() 将保存有问题的图像,尽管它应该清楚地保存从原始字节数组创建的图像。

def resave(path, ipath, rstart, rend, bytemin, bytemax):
bytes = readimage(path)
# uncomment the following line and bytes will become glitchedbytes???!
#glitchedbytes = processimage(bytes, rstart, rend, bytemin, bytemax)
#Note: I am not assigning glitchedbytes but bytes
im = Image.open(io.BytesIO(bytes))
im.save(ipath)

一定是我的 processimage() 我想的。但我认为这没有什么问题。还是我根本就是个傻子?

def processimage(bytes, rstart, rend, bytemin, bytemax):
bytecopy = bytes
scramblestart = 10
scrambleend = len(bytes)
nreplacements = random.randint(rstart,rend)

for i in range(0, nreplacements):
posA = random.randint(scramblestart, scrambleend)
posB = random.randint(scramblestart, scrambleend)
outputbytes = swapbytes(bytecopy, posA, posB, random.randint(bytemin,bytemax))
return outputbytes

整个代码:

import random, os, io
import Image
from array import array
from Tkinter import Tk
from tkFileDialog import *


def readimage(path):
count = os.stat(path).st_size / 2
with open(path, "rb") as f:
return bytearray(f.read())

def stellen(number):
return len(str(number).replace('.', ''))

def getnames(path):
path, extension = os.path.splitext(path)
name = os.path.basename(path)
return path, extension, name

def openfile(filetypes):
loadpath = askopenfilename(title="Open image to glitch", filetypes=filetypes)
return getnames(loadpath)

def savefile(number, filetypes):
if number > 1:
savepath = asksaveasfilename(title="Save "+str(number)+" glitched images", filetypes=filetypes, initialfile=name+"_glitched"+extension, defaultextension=extension)
else:
savepath = asksaveasfilename(title="Save glitched image", filetypes=filetypes, initialfile=name+"_glitched"+extension, defaultextension=extension)
return getnames(savepath)

def resave(path, ipath, rstart, rend, bytemin, bytemax):
bytes = readimage(path)
# uncomment the following line and bytes will become glitchedbytes???!
glitchedbytes = processimage(bytes, rstart, rend, bytemin, bytemax)
im = Image.open(io.BytesIO(bytes))
im.save(ipath)

def processimage(bytes, rstart, rend, bytemin, bytemax):
bytecopy = bytes
scramblestart = 10
scrambleend = len(bytes)
nreplacements = random.randint(rstart,rend)

for i in range(0, nreplacements):
posA = random.randint(scramblestart, scrambleend)
posB = random.randint(scramblestart, scrambleend)
outputbytes = swapbytes(bytecopy, posA, posB, random.randint(bytemin,bytemax))

return outputbytes

def swapbytes(bytecopy, posA, posB, leng):
try:
for i in range(0,leng):
tmp = bytecopy[posA+i]
bytecopy[posA+i] = bytecopy[posB+i]
bytecopy[posB+i] = tmp
except:
pass
return bytecopy

# Hide Base Window
Tk().withdraw()

filetypes = [("PNG","*.png"), ("BMP","*.bmp"), ("JPEG", "*.jpg"), ("JPEG", "*.jpeg"),
("GIF", "*.gif"), ("All", "*.png"),("All", "*.jpg"),("All", "*.jepg"),("All", "*.gif"),("All", "*.bmp")]

startnumber=0

# How many files should be made?
number = 10

# Calculate amount of leading Zeros
zfill = stellen(number)

# Get the path for the file to glitch and get a savepath
path, extension, name = openfile(filetypes)
savepath, saveextension, savename = savefile(number, filetypes)
originalpath = path+extension

bytes = readimage(path+extension)

if len(bytes) > 1:
if number > 1:
for i in range(startnumber+1, startnumber+number+1):
isavepath = savepath+str(i).zfill(zfill)+saveextension
resave(originalpath, isavepath, 1, 1, 1, 1)
else:
resave(originalpath, savepath, 1, 1, 1, 1)

最佳答案

bytecopy = bytes 不会复制 bytes。它只是创建一个指向同一个对象的新名称。要制作副本,请使用bytecopy = bytearray(bytes)

关于Python 字节数组 : Function changes other Instance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18494285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com