Skip to content

Instantly share code, notes, and snippets.

@DarkBlueStealth
Last active February 9, 2024 23:51
Show Gist options
  • Save DarkBlueStealth/5496de349ccbb7d247fffbeee7144d5f to your computer and use it in GitHub Desktop.
Save DarkBlueStealth/5496de349ccbb7d247fffbeee7144d5f to your computer and use it in GitHub Desktop.
Optimized version of BK Binary's Minecraft inside Terraria, reaches up to 20 FPS on my computer. I noticed a bug, if I switched into minecraft it would give off an error. Also another unknown bug might happen, if anyone finds the fixes please let me know!
# Reduced runtime from 5 FPS to 20 FPS.
import time,dxcam
from PIL import Image, ImageGrab
import pygetwindow as gw
import pyautogui
# Defining the camera
Camera = dxcam.create()
def png_to_2d_array(file_path):
# Commenting out the image disk fetcher
#img = Image.open(file_path)
width, height = file_path.size
pixel_data = list(file_path.getdata())
rgba_array = [pixel_data[i:i + width] for i in range(0, len(pixel_data), width)]
return rgba_array
def create_reformatted_cum(output_folder, width, height, rgba_values, index):
try:
import os
os.makedirs(output_folder, exist_ok=True)
output_file_path = os.path.join(output_folder, "reformatted" + str(index) + ".chum")
with open(output_file_path, 'wb') as file:
file.write(width.to_bytes(2,'big'))
file.write(height.to_bytes(2,'big'))
for i in range(len(rgba_values)):
for j in range(len(rgba_values[i])):
file.write(rgba_values[i][j][0].to_bytes(1,'big'))
file.write(rgba_values[i][j][1].to_bytes(1,'big'))
file.write(rgba_values[i][j][2].to_bytes(1,'big'))
#print(f"Reformatted file '{output_file_path}' created successfully.")
except Exception as e:
print(f"Error creating reformatted file: {e}")
def list_window_titles():
# Get all open windows
windows = gw.getAllTitles()
if not windows:
print("No open windows found.")
return
print("Open Windows:")
for window_title in windows:
print(f"- {window_title}")
def take_screenshot(window_title, output_file):
target_window = gw.getWindowsWithTitle(window_title)
if not target_window:
print(f"Window with title '{window_title}' not found.")
return
# Get the coordinates of the window
window_coords = (target_window[0].left, target_window[0].top, target_window[0].right, target_window[0].bottom)
# Capture the screenshot of the specified window
print(window_coords)
screenshot = Image.fromarray(Camera.grab(region=window_coords))
# Save the screenshot to the specified output file
# Commented this out because it's better to keep the picture in RAM rather than storing it to disk and then fetching it
#screenshot.save(output_file)
return screenshot
#print(f"Screenshot saved to '{output_file}'.")
# Resize function unused due to being a performance waster
def resize_image(input_file, output_file, target_size=(160, 90)):
try:
# Open the input image
original_image = Image.open(input_file)
# Resize the image
resized_image = original_image.resize(target_size)
# Save the resized image
resized_image.save(output_file)
#print(f"Image resized and saved to '{output_file}'.")
except Exception as e:
print(f"Error: {e}")
#Window to capture, can see titles with the list_window_titles def
list_window_titles()
window_title_to_capture = "Minecraft"
#where screenshots are saved
screenshot_file_path = r""
#Where chum files are saved
chum_folder = r""
num_screenshot = 0
Frames=[]
while(True):
Time=time.time()
screenshot = take_screenshot(window_title_to_capture, screenshot_file_path).resize((160,90))
# Commenting this out because it is easily replaced
#resize_image(screenshot_file_path, screenshot_file_path)
# Replacing the file path with the screenshot to lessen disk usage
rgba_array = png_to_2d_array(screenshot)
create_reformatted_cum(chum_folder, len(rgba_array[0]), len(rgba_array), rgba_array, num_screenshot)
num_screenshot += 1
Frames.append(time.time()-Time)
print("FPS: {}".format(1/(sum(Frames)/len(Frames))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment