Python 字节数组 : Function changes other Instance?

标签 python arrays instance

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

如果我运行 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/

相关文章:

java - 输出数组中顺序错误的对数?

python - 删除用作 dict 中 key 的对象不会删除 dict 中对应的 key

python - 如何在python中使用像matlab 'fread'这样的函数?

python - 服务器如何启动与客户端的连接

python - 模块 'lxml.etree' 的 RuntimeWarning : compiletime version 2. 6 与运行时版本 2.7 不匹配

c# - 如果我在每个循环之间按 ENTER 键,为什么随机生成和数组会一起工作?

python - 在不停止部分程序的情况下在 Python 2.7 中捕获警告

c# - 如何使用 Linq to XML 获取数组的数组?

javascript - 需要的javascript代码解释

java - 我想让打印机打印,直到帐户中的余额不足以打印作业为止