【问题标题】:Creating a copy of image file while processing in python在 python 中处理时创建图像文件的副本
【发布时间】:2016-01-27 14:52:10
【问题描述】:

我正在处理一个 BMP 文件,目前我正在将其转换为灰度文件。但是,我正在使用rb+,它写入同一个文件并将原始文件保存为已处理。如何处理实际复制原件并处理它而不是破坏原件的图像文件?

这里是代码

from io import SEEK_CUR

def main():
    filename = input("Please enter the file name: ")

    # Open as a binary file for reading and writing
    imgFile = open(filename, "rb+")

    # Extract the image information.
    fileSize = readInt(imgFile, 2)
    start = readInt(imgFile, 10)
    width = readInt(imgFile, 18)
    height = readInt(imgFile, 22)

    # Scan lines must occupy multiples of four bytes.
    scanlineSize = width * 3
    if scanlineSize % 4 == 0:
        padding = 0
    else :
        padding = 4 - scanlineSize % 4

    # Make sure this is a valid image.
    if fileSize != (start + (scanlineSize + padding) * height):
        exit("Not a 24-bit true color image file.")

    # Move to the first pixel in the image.
    imgFile.seek(start)# Process the individual pixels.
    for row in range(height): #For each scan line
        for col in range(width): #For each pixel in the line
            processPixel(imgFile)

        # Skip the padding at the end.
        imgFile.seek(padding, SEEK_CUR)

    imgFile.close()## Processes an individual pixel.#@param imgFile the binary file containing the BMP image#

def processPixel(imgFile) :
#  Read the pixel as individual bytes.
    theBytes = imgFile.read(3)
    blue = theBytes[0]
    green = theBytes[1]
    red = theBytes[2] #Read the pixel as individual bytes.
    # Process the pixel
    newBlue = 255 - blue
    newGreen = 255 - green
    newRed = 255 - red
    # Process the pixel.



    # Write the pixel.
    imgFile.seek(-3, SEEK_CUR)# Go back 3 bytes to the start of the pixel.

    imgFile.write(bytes([newBlue, newGreen, newRed]))## Gets an integer from a binary file.#@param imgFile the file#@ param offset the offset at which to read the integer#@
##  Gets an integer from a binary file.
# @param imgFile  the file
# @param offset  the offset at which to read the integer
# @return  the integer starting at the given offset
#

def readInt(imgFile, offset): #Move the file pointer to the given byte within the file.
    imgFile.seek(offset)

    # Read the 4 individual bytes and build an integer.
    theBytes = imgFile.read(4)
    result = 0
    base = 1
    for i in range(4):
        result = result + theBytes[i] * base
        base = base * 256

    return result# Start the program.
main()

【问题讨论】:

  • 请出示您的代码
  • 这是一个学校作业,由于抄袭,我被告知不要在任何地方发帖。我的代码有效,我只想复制图像。
  • 你不能只显示有问题的几行:你如何打开和保存文件吗?
  • 好的,我已经添加了一些代码
  • 只需打开一个新文件并复制到其中,而不是覆盖现有文件!如果您正在处理图像,请使用例如 scipy.misc.imsave...

标签: python file python-3.x image-processing binary


【解决方案1】:

我建议先复制文件,然后再写入该副本。

您可以使用shutil 模块中的copyfile 函数来复制文件。 这是一个如何复制文件的演示:

import shutil  
shutil.copyfile('/path/to/src/file', '/path/to/dest/file')  

【讨论】:

  • 听起来不错。如果有帮助,请投票/标记为答案的解决方案。
  • 哦,有什么方法可以让我不必写文件誓言,它会在同一目录中创建图像的副本。我将它提交给我的教授,他不会和我有相同的目录。
  • 如果文件名在当前工作目录中,只需包含文件名。例如: import shutil shutil.copyfile('file.extension', 'new_file.extension')
  • 好吧,我无法让它工作。我现在已经发布了我的代码。你能实现它吗?谢谢。
猜你喜欢
  • 2015-02-15
  • 2020-05-20
  • 2021-08-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多