1

我在一个程序上工作,它打开一个图像,将它的字节读取到一个字节数组,然后交换随机字节并让你保存它。这样可行。但是,我遇到了一个奇怪的错误,据我所知,它不应该存在。

如果我运行它resave()并用 , 取消注释该行glitchedbytesim.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)
4

1 回答 1

2

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

于 2013-08-28T17:19:50.713 回答