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 に答える