-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
from PIL import Image | ||
import numpy as np | ||
import torch | ||
|
||
|
||
|
||
# Set the input and output directories | ||
input_dir = './' | ||
output_dir = './' | ||
|
||
# Create the output directory if it doesn't already exist | ||
if not os.path.exists(output_dir): | ||
os.makedirs(output_dir) | ||
|
||
# Loop through each file in the input directory | ||
for filename in os.listdir(input_dir): | ||
# Open the image file using PIL | ||
img_path = os.path.join(input_dir, filename) | ||
img = Image.open(img_path) | ||
img_gray = img.convert('L') | ||
|
||
# Convert the image to a NumPy array | ||
img_np = np.array(img_gray) | ||
|
||
# Convert the NumPy array to a PyTorch tensor | ||
img_tensor = torch.from_numpy(img_np) | ||
|
||
# Replace values below 200 with 0, and set all other values to 1 | ||
img_tensor[img_tensor <= 100] = 3 | ||
img_tensor[img_tensor > 100] = 1 | ||
|
||
# Reshape the tensor from 64 x 64 to 4096 x 1 | ||
# img_tensor = img_tensor.reshape((64, 64)) | ||
img_tensor = img_tensor.t() | ||
img_tensor = img_tensor.reshape((4096,1)) | ||
img = torch.flatten(img_tensor, 0, -1) | ||
|
||
# Save the altered tensor as a text file | ||
tensor_name = os.path.splitext(filename)[0] + '.txt' | ||
tensor_path = os.path.join(output_dir, tensor_name) | ||
np.savetxt(tensor_path, img_tensor.numpy(), fmt='%d') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from PIL import Image | ||
import os | ||
|
||
file_dir = './' | ||
|
||
|
||
def get_file_list(file_dir): | ||
for root, dirs, files in os.walk(file_dir): | ||
pass | ||
return files | ||
|
||
|
||
data_list = get_file_list(file_dir) | ||
|
||
|
||
def convert_array2fig(filename: str): | ||
with open("2000test/" + filename) as f: | ||
img = Image.new("RGB", (64, 64), (255, 255, 255)) | ||
data = f.readlines() | ||
for i in range(len(data)): | ||
if int((data[i].strip())) == 3: | ||
row = i // 64 | ||
col = i % 64 | ||
img.putpixel((col,row), (0, 0, 0)) | ||
|
||
img.rotate(90).transpose(Image.FLIP_TOP_BOTTOM).save("./" + filename.split(".")[0] + ".png", dpi=(600, 600)) | ||
# img.show() | ||
|
||
|
||
|
||
|
||
for name in data_list: | ||
convert_array2fig(name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
import matplotlib.ticker as ticker | ||
from mpl_toolkits.axes_grid1.inset_locator import inset_axes | ||
from PIL import Image | ||
|
||
dataset = pd.read_csv('.csv',header=None) | ||
|
||
|
||
num_columns = dataset.shape[1] | ||
submatrix_width = 5 | ||
num_submatrices = num_columns // submatrix_width | ||
|
||
for i in range(num_submatrices): | ||
start_col = i * submatrix_width | ||
end_col = start_col + submatrix_width | ||
|
||
submatrix = dataset[:, start_col:end_col] | ||
|
||
np.savetxt(f'submatrix_{i + 1}.txt', submatrix, delimiter='\t', fmt='%.6f') # Save as .txt | ||
|
||
print(f'Saved submatrix_{i + 1}.txt') | ||
|
||
|
||
|
||
# fig, ax1 = plt.subplots() | ||
# c = ax1.pcolor(dataset1,cmap='viridis') | ||
# fig.tight_layout() | ||
# plt.axis('off') | ||
# plt.show() |