python - 在 python 中处理时创建图像文件的副本

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

我正在处理 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()

最佳答案

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

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

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

关于python - 在 python 中处理时创建图像文件的副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33380769/

相关文章:

python - 通过 Tkinter GUI 打开 txt 文件

c++ - 大量文件的原子删除

javascript - Paypal Checkout 客户端集成 - 返回订单 ID promise 的问题

python - 用于生成 NumPy 矩阵的矢量化解决方案

python - 将任意十六进制序列转换为字节

linux - 在文件系统中创建文件

python - 调用函数时更改 Python 3 sys.argv

python - Python中的for循环并从文件中读取输入

python - 为什么 Google App Engine 找不到我的模块 (Python 3)?

python - 使用 QTextCharFormat 更改选择颜色