Python Bytearray:函数更改其他实例?

时间:2013-08-28 17:18:15

标签: python bytearray instance

我处理一个打开图像的程序,将其字节读取到bytearray然后交换随机字节并让你保存它。这样可行。但是我遇到了一个奇怪的错误,根据我的理解,不应该在那里。

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

1 个答案:

答案 0 :(得分:2)

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