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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| from PIL import Image, ImageDraw, ImageFont import os import string
# Ensure the output directory exists output_dir = './mosaic_chars' if not os.path.exists(output_dir): os.makedirs(output_dir)
flag = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
H = 68 W = 30
canvas = Image.new('RGB', (W * len(flag), H), (255, 255, 255)) # Ensure the font path is correct for your system font = ImageFont.truetype('C:/Users/miaoaixuan/Desktop/JetBrainsMono-Regular.ttf', 50, encoding='utf-8') pen = ImageDraw.Draw(canvas) pen.text((0, 0), flag, 'black', font)
def mosaic_img(img: Image.Image, L, H, R, D): w, h = R - L, D - H a = [0, 0, 0] cnt = 0 for x in range(w): for y in range(h): j = img.getpixel((L+x, H+y)) for ch in range(len(a)): a[ch] += j[ch] cnt += 1 b = [k//cnt for k in a] mosaic = Image.new('RGB', (w, h), tuple(b)) img.paste(mosaic, (L, H, R, D))
for i in range(len(flag)): char = canvas.crop((W * i, 0, W * (i+1), H)) if 0 <= i < len(flag) : # Apply mosaic to specified characters mosaic_img(canvas, W*i, 0, W*i+W, H//2) mosaic_img(canvas, W*i, H//2, W*i+W, H) # Save the character image after potentially applying mosaic char = canvas.crop((W * i, 0, W * (i+1), H)) # Recrop to get the updated character char.save(f'{output_dir}/{flag[i]}_{char.tobytes().__len__()}.png')
canvas.save('flag_censored.png', format='png') import os
def files_are_identical(file1_path, file2_path): """逐字节比较两个文件,检查它们是否完全相同""" with open(file1_path, 'rb') as file1, open(file2_path, 'rb') as file2: while True: chunk1 = file1.read(4096) chunk2 = file2.read(4096) if chunk1 != chunk2: return False if not chunk1: # 如果chunk1为空,意味着文件已到末尾 break return True
def compare_file_with_folder(target_file_path, folder_path): """比较指定文件与文件夹内所有文件,找出内容完全相同的文件""" identical_files = [] for root, _, files in os.walk(folder_path): for filename in files: file_path = os.path.join(root, filename) if files_are_identical(target_file_path, file_path): identical_files.append(file_path) return identical_files
def print_identical_files(identical_files): """打印所有相同文件的路径""" if identical_files: print("找到以下相同的文件:") for file_path in identical_files: print(file_path) else: print("在文件夹中没有找到相同的文件。")
target_file_path = 'C:/Users/miaoaixuan/Desktop/1/char_23.png' folder_path = 'C:/Users/miaoaixuan/mosaic_chars' identical_files = compare_file_with_folder(target_file_path, folder_path) print_identical_files(identical_files)
|