Created
March 30, 2023 18:36
-
-
Save Forenard/efc3f8032864a3cc1d340947fce59c07 to your computer and use it in GitHub Desktop.
Audacityでbmpファイルにmisalignment glitchを起こす際に壊れたヘッダを修復するスクリプト
This file contains hidden or 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
import tkinter as tk | |
from tkinterdnd2 import DND_FILES, TkinterDnD | |
class App(tk.Frame): | |
def __init__(self, master=None): | |
super().__init__(master) | |
self.create_widgets() | |
def create_widgets(self): | |
self.place(relx=0.0, rely=0.0, relheight=1.0, | |
relwidth=1.0, anchor=tk.NW) | |
# D&D用のフレーム | |
self.frame_drag_drop_input = frameDragAndDrop(self, '正常なbmpファイルをD&D') | |
self.frame_drag_drop_output = frameDragAndDrop(self, '修正するbmpファイルをD&D') | |
self.frame_drag_drop_input.place( | |
relx=0.0, rely=0.0, relheight=0.5, relwidth=0.5, anchor=tk.NW) | |
self.frame_drag_drop_output.place( | |
relx=0.5, rely=0.0, relheight=0.5, relwidth=0.5, anchor=tk.NW) | |
# ボタン | |
self.button_fix = tk.Button( | |
self, text='修正', command=self.funcButtonFix) | |
self.button_fix.place( | |
relx=0.5, rely=0.75, relheight=0.1, relwidth=0.5, anchor=tk.CENTER) | |
def funcButtonFix(self): | |
src_filename = self.frame_drag_drop_input.path | |
dst_filename = self.frame_drag_drop_output.path | |
with open(src_filename, 'rb') as sf, open(dst_filename, 'rb+') as df: | |
# ファイル先頭から画像データまでのオフセットを取得 | |
sf.seek(0x0000000A) | |
offset = int.from_bytes(sf.read(4), byteorder='little') | |
# ファイル先頭から画像データまでのバイト列を読み込んで上書きする | |
sf.seek(0) | |
df.seek(0) | |
df.write(sf.read(offset)) | |
class frameDragAndDrop(tk.LabelFrame): | |
def __init__(self, parent, default_txt): | |
super().__init__(parent) | |
self.default_text = default_txt | |
self.path = '' | |
self.textbox = tk.Text(self) | |
self.textbox.insert(0.0, self.default_text) | |
self.textbox.configure(state='disabled') | |
# ドラッグアンドドロップ | |
self.textbox.drop_target_register(DND_FILES) | |
self.textbox.dnd_bind('<<Drop>>', self.funcDragAndDrop) | |
self.textbox.place(relx=0.0, rely=0.0, relheight=1.0, | |
relwidth=1.0, anchor=tk.NW) | |
def funcDragAndDrop(self, e): | |
self.path = e.data | |
message = f'{self.default_text}\n{e.data}' | |
self.textbox.configure(state='normal') | |
self.textbox.delete(0.0, tk.END) | |
self.textbox.insert(0.0, message) | |
self.textbox.configure(state='disabled') | |
def main(): | |
width = 400 | |
height = 200 | |
root = TkinterDnD.Tk() | |
root.title(f'BMPファイルを修正するツール') | |
root.geometry(f'{width}x{height}') | |
root.resizable(False, False) | |
App(root) | |
root.mainloop() | |
if __name__ == "__main__": | |
main() | |
# pyinstaller fix_bmp_gui.py --onefile --noconsole --collect-data tkinterdnd2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment