JPEG DCT转换问题

时间:2018-12-04 12:05:33

标签: python-3.x jpeg dct

我尝试用python编程JPEG转换, 到目前为止,我已经能够进行所有步骤,在那里我需要将数据转换回新图像。 这是我的代码,请注意,我对变量的使用非常差。我没有很多时间来编写此代码,而我仍在学习如何在python中进行编码。

from PIL import Image
import numpy as np
import sympy as sp
import skimage.util
from scipy import fftpack

#
#ALLEMAAL INFORMATIE GEEN BEREKENINGEN
img = Image.open('C:\\Users\\dbe\\Documents\\KULAK\\2018-2019\\Semester 1\\Python\\Python FFt\\mario4.png')  
img = img.convert('L') # converteren naar monochrome
img.show()
pixels = list(img.getdata())
pixelArray = np.array(img.getdata())
grootte = list(img.size)
kolommen, rijen = img.size

#tot hier allemaal informatie

  #PixelMatrix
pixelMatrix = []
while pixels != []:
    pixelMatrix.append(pixels[:kolommen])
    pixels = pixels[kolommen:]

pixelMatrix = np.array(pixelMatrix)


# 8 op 8 
foto_dct = skimage.util.view_as_blocks(pixelMatrix, block_shape=(8, 8))
print("Voor aftrekking:","\n",foto_dct[3,3])




    # (1) -128
for x in foto_dct:
    x = x - 128 # pixel 8 op 8 matrix -128
print("Voor DCT","\n",foto_dct[3,3])

    # (2) DCT 2x
for x in foto_dct:
    x = fftpack.dct(fftpack.dct(x.T,norm = 'ortho').T,norm ='ortho') # 2x dct
print("Voor de deling",'\n',foto_dct[3,3])

    # (3) quantisatie ( /Q)

Q = np.array([[16,11,10,16,24,40,51,61],[12,12,14,19,26,58,60,55],[14,13,16,24,40,57,69,56],[14,17,22,29,51,87,80,62],[18,22,37,56,68,109,103,77],[24,35,55,64,81,104,113,92],[49,64,78,87,103,121,120,101],[72,92,95,98,112,100,103,99]]) # quantisatie matrix

for x in foto_dct:
        x = np.divide(x,Q)
print("Na de deling:","\n",foto_dct[3,3])

    # (4) *Q
for x in foto_dct:
    x = np.multiply(x,Q)
print("Na vermenigvuldiging terug:","\n",foto_dct[3,3])

    # (5) idct 2x 
for x in foto_dct:
        x = fftpack.idct(fftpack.idct(x.T,norm = 'ortho').T,norm ='ortho')
print("Na IDCT:","\n",foto_dct[3,3])

    # (6) +128
for x in foto_dct:
        x = (x + 128 ).clip(0,255) 
print("Na optelling","\n",foto_dct[3,3])
print("test","\n",foto_dct[3,4])


foto_dct = np.reshape(foto_dct,(kolommen,rijen)) 

flatArray = np.array(foto_dct.flatten())

finalim = Image.new('L',(rijen,kolommen))
finalim.putdata(flatArray)
finalim.show()

或者这是屏幕截图的链接: Screenshot of my code 现在的问题是,当我将8x8矩阵转换回128x128(rijen x kolommen)矩阵时,订单被弄乱了,原始图片如下: Original picture 现在看起来像这样: JPEG picture 谁能帮我解决为什么这不正确吗?

其他信息:foto_dct.shape为(16,16,8,8)                   :当我在#(1)之前取一个8x8 pixelmatrix时,以及在操作后我取相应的8x8 pixelmatrix时,在#(6)之后我打印它们时它们是正确的。

foto_dct [5,7]:BEFORE TRANSFORMATION

foto_dct [5,7]:AFTER TRANSFORMATION 尽管我无法将新制作的矩阵重新组合成图像。 此致。

0 个答案:

没有答案