202212-3 JPEG decoding (python100 points):

202212-3 JPEG decoding (40 points):
The quadrennial World Cup is coming to an end. In this World Cup, the application of Video Assistant Referee (VAR) has shone brightly. VAR uses video replay technology to help the referee make correct penalty decisions. A VAR device has also been introduced to the West West Iver Island Football League. As the technical director of the technology supplier, Xiao C needs to store and encode the image data generated by VAR. Little C’s analysis and comparison found that the JPEG encoding algorithm can achieve better compression results, and the quality loss is acceptable. Therefore, Little C decided to use the JPEG encoding algorithm to store and transmit image data. JPEG is a commonly used lossy compression algorithm for images. It has a high compression rate, but the quality of the compressed images drops significantly. The compression ratio of JPEG images is generally between 10:1 and 20:1, and is generally used to store photos and other scenes where image quality is not high.

import math
q=[]
for i in range(8):
    temp=list(map(int,input().split()))
    q.append(temp)
n=int(input())
T=int(input())
list_m=list(map(int,input().split()))

# n=64
# T=0
# list_m=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63]
# print(list_m)
# q=[[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], [14, 13, 16, 24, 40, 57, 69, 56], [24, 35, 55, 64, 81, 104, 113, 92], [24, 35, 55, 64, 81, 104, 113, 92], [72, 92, 95, 98, 112, 100, 103, 99]]

m=[]
for i in range(8):
    m.append([0,0,0,0,0,0,0,0])

def fun_guide():
    ls=[]

    move1=[[0,1],[1,-1],[1,0],[-1,1]]
    move2=[[-1,1],[1,0],[1,-1],[0,1]]
    
    move_way=0
    tem_location=[0,0]
    for i in range(0,36):
        ls.append(list(tem_location))
        tem_location[0]=tem_location[0]+move1[move_way][0]
        tem_location[1]=tem_location[1]+move1[move_way][1]
        if 0 in tem_location:
            move_way=(move_way+1)%4
    
    # print("-------")
    move_way=0
    tem_location=[7,1]
    for i in range(36,64):
        ls.append(list(tem_location))
        tem_location[0]=tem_location[0]+move2[move_way][0]
        tem_location[1]=tem_location[1]+move2[move_way][1]
        if 7 in tem_location:
            move_way=(move_way+1)%4
    return ls


ls=fun_guide()

for index,value  in enumerate(list_m) :
    y,x=ls[index][0],ls[index][1]
    m[y][x]=value
    

if T==0:
    for i in range(8):
        print(*m[i])
elif T==1:        
    for x in range(8):
        for y in range(8):
            m[x][y]=m[x][y]*q[x][y]
    for i in range(8):
        print(*m[i])
        
elif T==2: 
    for x in range(8):
        for y in range(8):
            m[x][y]=m[x][y]*q[x][y]
    MM = [[0]*8 for i in range(8)]
    
    for i,j in ls:
        SU = 0
        for u in range(8):
            sumv=0
            au = pow(1/2,1/2) if u==0 else 1
            for v in range(8):
                av = pow(1/2,1/2) if v==0 else 1
                sumv +=au*av*m[u][v]*math.cos(math.pi/8*(i+0.5)*u)*math.cos(math.pi*v*(j+1/2)/8)
            SU += sumv
        MM[i][j] = int(SU/4+128+0.5)
        if MM[i][j] > 255:
            MM[i][j] = 255
        elif MM[i][j] < 0:
            MM[i][j] = 0
    for i in range(8):
        print(*MM[i])
		
		
  
    
    

Guess you like

Origin blog.csdn.net/X131644/article/details/129646834