简体   繁体   中英

How can I save an image with negative pixel values in python?

Before you start reading, I want to apologize; I am new to Python, so I'm sorry if this question seems simple.

I am trying to take in a set of images that I have and normalize them for use in deep network training (divide each pixel of the image by the standard deviation and then subtract the mean value of the pixels) and then save the resulting images separately using Python. I have been able to code making the images normalized, but my issue lies in saving the images as they have negative pixel values.

I have seen this question and read through the answers, but my issue is one that user Swaroop mentioned in the comments. The answer provided doesn't work for the type of images that I am using (images with three channels, or 'RGB')

Here is the entirety of my code:

import matplotlib.pyplot as plt
import numpy as np
import copy
import math
from PIL import Image

# Setup Important Variables
basic_path = 'Censored_for_privacy_reasons'
mins = 0

# Setup Standard Deviation Function
def stDevImage(imageArray, plane):
    stDevArray = []
    stDevAdd = 0
    for row in range(0,imageArray.shape[0]):
        for column in range(0,imageArray.shape[1]):
            stDevArray.append(copy.deepcopy(imageArray[row][column][plane]))
    meanForArray = np.mean(stDevArray)
    for pixelValue in range(0,len(stDevArray)):
        stDevAdd += pow(stDevArray[pixelValue]-meanForArray,2)
    stDev = math.sqrt(stDevAdd/len(stDevArray))
    return [stDev, meanForArray]

img = plt.imread(basic_path+'25/NS0/NS0_1.png')
imgArray = np.asarray(img)
normImage = copy.deepcopy(imgArray)

rPlaneSt = stDevImage(imgArray, 0)
gPlaneSt = stDevImage(imgArray, 1)
bPlaneSt = stDevImage(imgArray, 2)
for row in range(0, imgArray.shape[0]):
    for column in range(0,imgArray.shape[1]):
        normImage[row][column][0] = copy.deepcopy(normImage[row][column][0]/rPlaneSt[0]-rPlaneSt[1])
        normImage[row][column][1] = copy.deepcopy(normImage[row][column][1]/gPlaneSt[0]-gPlaneSt[1])
        normImage[row][column][2] = copy.deepcopy(normImage[row][column][2]/bPlaneSt[0]-bPlaneSt[1])
        if normImage[row][column][0] < mins:
            mins = copy.deepcopy(normImage[row][column][0])
imgFinal = Image.fromarray(normImage, 'I')
imgFinal.save("/Users/Censored/Desktop/test.tif")

The function labeled as stDevImage is the function that provides the standard deviation of the plane in the image that it takes in. It returns both the standard deviation of that plane as well as the mean.

As you can see in the last two lines of the code, I am trying out what the answer to the question mentioned above said to do, but to no avail.

Here is an image that represents the type of image that I am working with (it is also the image referenced at the line in the code that reads img = plt.imread(basic_path+'25/NS0/NS0_1.png') ).

The image is located here

Any and all help would be greatly appreciated!

I would suggest you try the following packages for accessing floating point TIFF files in Python:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM