資源簡介
信息隱藏作業。使用python實現幻方置亂,里面的幻方函數生成是從借鑒的別人的,壓縮包的內容是PYcharm的工程文件。其中,TPYCL.py是運行的程序,里面的幻方生成為奇數,若想要奇偶均可,可以使用里面另外一個幻方生成.py文件進行替換。注意,里面的a.jpg不能太大,不然耗時會特別長;TPYCL.py里面對圖片僅進行了20次置亂,你可以隨意更改。置亂n2次后,圖片恢復到原來的狀態。

代碼片段和文件信息
#!/usr/bin/python3
from?PIL?import?Image
import?numpy?as?np
import?numpy
import?time
import?math
#列表循環向左移offset位
def?shift_left(lst?offset):
????return?[lst[(i+offset)%len(lst)]?for?i?in?range(len(lst))]
#列表循環向右移offset位
def?shift_right(lst?offset):
????return?[lst[i-offset]?for?i?in?range(len(lst))]
#構造奇數階幻方函數
def?magic_of_odd_order(n):
????p?=?(int)((n-1)/2)
????#創建矩陣1
????initial_lst1?=?list(range(p+1n))+list(range(p+1))
????initial_mat1?=?[]
????for?i?in?range(n):
????????initial_mat1.append(shift_left(initial_lst1?i))
????mat1?=?np.array(initial_mat1)
????#創建矩陣2
????initial_lst2?=?list(range(p-1-1))+list(range(2*pp-1))
????initial_mat2?=?[]
????for?i?in?range(n):
????????initial_mat2.append(shift_right(initial_lst2?i))
????mat2?=?np.array(initial_mat2)
????#創建矩陣3即元素全為1的矩陣
????mat3=?np.ones((nn)dtype=np.int)
????#構造幻方
????magic?=?n*mat2+mat1+mat3
????return?magic
#主函數
def?main():
????a?=?Image.open(“a.jpg“)??#?打開圖像
????b?=?numpy.array(a)??#?圖片轉數組
????print(b)??#?輸出圖片數組
????image_shape?=?[]??#?定義一個list
????image_shape?=?b.shape??#?將圖片像素大小傳入list中
????print(image_shape[1])??#?測試,輸出圖片的寬度
????print(b.shape)??#?測試,輸出圖片的大小(3753753)
????c?=?Image.fromarray(b)??#?數組轉圖片
????c.show()??#?顯示圖片
????order?=?image_shape[1]
????if?order%2?==1:
????????magic?=?magic_of_odd_order(order)
????else:
????????print(“圖片不合法!“)
????for?row?in?magic:??#在magic的每行循環
????????for?col?in?row:#在每行中循環輸出每個元素
????????????print(col?end=‘\t‘)
????????print()
????#開始置換幻方矩陣和圖片矩陣(一次)
????for?change_num?in?range(020):
????????for?i?in?range(0image_shape[0]):
????????????for?j?in?range(0image_shape[0]):
????????????????#?判斷magic[i][j]是否為幻方矩陣的最大值,如果是最大值,則將其置為1,將等于1的那一位置為0
????????????????if?magic[i][j]?==?math.pow(image_shape[0]2):
????????????????????for?i_?in?range(0?image_shape[0]?2):
????????????????????????for?j_?in?range(0?image_shape[0]?2):
????????????????????????????if?magic[i_][j_]?==?1?:
????????????????????????????????t?=?magic[i][j]
????????????????????????????????magic[i][j]?=?magic[i_][j_]
????????????????????????????????magic[i_][j_]?=?t
????????????????????????????????#交換圖片數組中對應的元素
????????????????????????????????temp_?=?b[i][j]
????????????????????????????????b[i][j]?=?b[i_][j_]
????????????????????????????????b[i_][j_]?=?temp_
????????????????else:
????????????????????for?i_?in?range(0?image_shape[0]?2):
????????????????????????for?j_?in?range(0?image_shape[0]?2):
????????????????????????????if?magic[i_][j_]?==?(magic[i][j]?-?1)?:
????????????????????????????????t?=?magic[i][j]
????????????????????????????????magic[i][j]?=?magic[i_][j_]
????????????????????????????????magic[i_][j_]?=?t
????????????????????????????????#?交換圖片數組中對應的元素
????????????????????????????????temp?=?b[i][j]
????????????????????????????????b[i][j]?=?b[i_][j_]
????????????????????????????????b[i_][j_]?=?temp
????c?=?Image.fromarray(b)??#?數組轉圖片
????c.show()??#?顯示圖片
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-05-23?16:29??.idea\
?????文件?????????213??2018-05-12?10:41??.idea\misc.xm
?????文件?????????282??2018-05-12?10:41??.idea\modules.xm
?????文件???????24855??2018-05-23?16:29??.idea\workspace.xm
?????文件?????????398??2018-05-12?10:41??.idea\鍥劇墖棰勫鐞?iml
?????文件????????6300??2018-05-23?15:47??a.jpg
?????文件???????????7??2018-05-11?15:54??cmd.cmd
?????文件????????3452??2018-05-23?15:42??TPYCL.py
?????文件????????3216??2018-05-11?16:10??騫繪柟鏁扮粍鐨勭敓鎴?py
- 上一篇:train_loss_acc.py
- 下一篇:Python標準庫源代碼.zip
評論
共有 條評論