質問

I'm making a program in Python using Pygame that will load an image to the screen, open the raw data (as in, the characters you would see if you opened the jpg as a text file), throw some random characters in with the data, and then resave it as a jpg to load into pygame again. This results in a cool looking glitch effect.

I am not having any problems with the desired glitches, but I was finding that despite what kind of random character was placed where, for certain images every time the image went through my glitch function I ended up with a grey bar on the bottom of the image. I simplified my function so that all it did was load the image, open the image as a read binary (even though I'm on a mac), save a string of the raw data, write a new file based on this string and then load that file. The image was not purposefully glitched in any way, and the data was supposedly untouched but I still encountered this grey bar.

Here is the relevant code:

def initializeScreen(x, y):
    pygame.display.set_mode((x,y))
    return pygame.display.get_surface()

def importImage(fileName):
    imgText = open(fileName, 'rb')
    imgTextStr = imgText.read()
    imgText.close()
    return imgTextStr

screenSurf = initializeScreen(800,600)
textOfImg = importImage('/Users/Amoeba/Desktop/GlitchDriving/Clouds.jpg')
newFile = open('/Users/Amoeba/Desktop/GlitchDriving/tempGlitchFile.jpg', 'wb')
newFile.write(textOfImg)
newimgSurf = pygame.image.load('/Users/Amoeba/Desktop/GlitchDriving/tempGlitchFile.jpg')
screenSurf.blit(newimgSurf, (0,0))
pygame.display.flip()

Here is an example of one of the images before and after passing through my function:

enter image description here

It is worth noting that the size of the grey bar depends on the picture. Some pictures even pass through my function visibly unchanged, as they should be. Also, if I open the new version of the jpg written by my program with image viewing software like preview, the grey bar does not appear. My suspicion is that it is a quirk of the pygame image load function or that there is some strange character (or possibly white space) that is being dropped in my conversion from jpg to string or vice-versa. I did compare two of the text files (one with grey bar and one without) and found no difference while using an online "difference finder".

This is my first post here, but I've lurked for answers dozens of times. Any help is greatly appreciated.

役に立ちましたか?

解決

You never close the file object you create with open, so probably not all data gets written back (flushed) to your new file.

Either close the file object before trying to read the file again, or better start using the with statement (which will close the file for you) whenever you deal with files:

def importImage(fileName):
    with open(fileName, 'rb') as imgText:
        return imgText.read()

screenSurf = initializeScreen(800,600)
textOfImg = importImage(r'path/to/file')

with open(r'path/to/otherfile', 'wb') as newFile:
    newFile.write(textOfImg)

newimgSurf = pygame.image.load(r'path/to/otherfile')
screenSurf.blit(newimgSurf, (0,0))
pygame.display.flip()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top