Guest User

Kohya Run Command v1

a guest
Aug 27th, 2024
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. import os
  2. import pygame
  3. import numpy as np
  4. from PIL import Image
  5.  
  6. def hex_to_rgb(hex_color):
  7. return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
  8.  
  9. def create_mask(image_path, mask_path, background_color):
  10. pygame.init()
  11.  
  12. # Get initial screen size (75% of monitor resolution)
  13. info = pygame.display.Info()
  14. initial_width, initial_height = int(info.current_w * 0.80), int(info.current_h * 0.80)
  15.  
  16. # Set up the resizable display
  17. screen = pygame.display.set_mode((initial_width, initial_height), pygame.RESIZABLE)
  18. pygame.display.set_caption("Create Mask - Left click to draw, Right click to erase, Mouse wheel to resize brush, Ctrl+wheel to zoom, Middle click to pan, Enter to save")
  19.  
  20. # Load the image
  21. original_image = pygame.image.load(image_path)
  22. image_width, image_height = original_image.get_size()
  23.  
  24. # Initialize zoom and pan variables
  25. zoom = min(initial_width / image_width, initial_height / image_height)
  26. pan_x, pan_y = 0, 0
  27.  
  28. # Create mask surface
  29. mask = pygame.Surface((image_width, image_height))
  30. mask.fill(background_color)
  31.  
  32. drawing = False
  33. erasing = False
  34. panning = False
  35. last_pan_pos = None
  36. brush_size = 50
  37.  
  38. clock = pygame.time.Clock()
  39.  
  40. def draw_image():
  41. # Calculate the scaled image size
  42. scaled_width = int(image_width * zoom)
  43. scaled_height = int(image_height * zoom)
  44.  
  45. # Scale the image and mask
  46. scaled_image = pygame.transform.smoothscale(original_image, (scaled_width, scaled_height))
  47. scaled_mask = pygame.transform.smoothscale(mask, (scaled_width, scaled_height))
  48.  
  49. # Calculate position to center the image
  50. pos_x = (screen.get_width() - scaled_width) // 2 + pan_x
  51. pos_y = (screen.get_height() - scaled_height) // 2 + pan_y
  52.  
  53. # Draw the image and mask
  54. screen.fill((128, 128, 128)) # Fill with gray to show image bounds
  55. screen.blit(scaled_image, (pos_x, pos_y))
  56. screen.blit(scaled_mask, (pos_x, pos_y), special_flags=pygame.BLEND_RGBA_MULT)
  57.  
  58. def screen_to_image(x, y):
  59. # Convert screen coordinates to image coordinates
  60. scaled_width = image_width * zoom
  61. scaled_height = image_height * zoom
  62. offset_x = (screen.get_width() - scaled_width) / 2 + pan_x
  63. offset_y = (screen.get_height() - scaled_height) / 2 + pan_y
  64. image_x = (x - offset_x) / zoom
  65. image_y = (y - offset_y) / zoom
  66. return image_x, image_y
  67.  
  68. def image_to_screen(x, y):
  69. # Convert image coordinates to screen coordinates
  70. scaled_width = image_width * zoom
  71. scaled_height = image_height * zoom
  72. offset_x = (screen.get_width() - scaled_width) / 2 + pan_x
  73. offset_y = (screen.get_height() - scaled_height) / 2 + pan_y
  74. screen_x = x * zoom + offset_x
  75. screen_y = y * zoom + offset_y
  76. return screen_x, screen_y
  77.  
  78. running = True
  79. while running:
  80. for event in pygame.event.get():
  81. if event.type == pygame.QUIT:
  82. running = False
  83. elif event.type == pygame.VIDEORESIZE:
  84. # Window has been resized, update the display
  85. screen = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  86. elif event.type == pygame.MOUSEBUTTONDOWN:
  87. if event.button == 1: # Left mouse button
  88. drawing = True
  89. elif event.button == 3: # Right mouse button
  90. erasing = True
  91. elif event.button == 2: # Middle mouse button
  92. panning = True
  93. last_pan_pos = event.pos
  94. elif event.type == pygame.MOUSEBUTTONUP:
  95. if event.button == 1: # Left mouse button
  96. drawing = False
  97. elif event.button == 3: # Right mouse button
  98. erasing = False
  99. elif event.button == 2: # Middle mouse button
  100. panning = False
  101. last_pan_pos = None
  102. elif event.type == pygame.MOUSEMOTION:
  103. if panning and last_pan_pos:
  104. dx, dy = event.pos[0] - last_pan_pos[0], event.pos[1] - last_pan_pos[1]
  105. pan_x += dx
  106. pan_y += dy
  107. last_pan_pos = event.pos
  108. elif event.type == pygame.MOUSEWHEEL:
  109. if pygame.key.get_mods() & pygame.KMOD_CTRL:
  110. # Zoom in/out
  111. mouse_x, mouse_y = pygame.mouse.get_pos()
  112. image_x, image_y = screen_to_image(mouse_x, mouse_y)
  113.  
  114. old_zoom = zoom
  115. zoom = max(0.1, min(5, zoom * (1.1 ** event.y)))
  116.  
  117. # Adjust pan to zoom towards mouse position
  118. new_mouse_x, new_mouse_y = image_to_screen(image_x, image_y)
  119. pan_x += mouse_x - new_mouse_x
  120. pan_y += mouse_y - new_mouse_y
  121. else:
  122. # Adjust brush size
  123. brush_size = max(1, min(120, brush_size + (event.y * 2)))
  124. elif event.type == pygame.KEYDOWN:
  125. if event.key == pygame.K_RETURN:
  126. running = False # Exit loop to save
  127.  
  128. if drawing or erasing:
  129. # Calculate the position on the original image
  130. mouse_x, mouse_y = pygame.mouse.get_pos()
  131. image_x, image_y = screen_to_image(mouse_x, mouse_y)
  132.  
  133. if 0 <= image_x < image_width and 0 <= image_y < image_height:
  134. color = (255, 255, 255) if drawing else background_color
  135. pygame.draw.circle(mask, color, (int(image_x), int(image_y)), max(1, int(brush_size / zoom)))
  136.  
  137. draw_image()
  138.  
  139. # Draw brush outline
  140. mouse_pos = pygame.mouse.get_pos()
  141. image_x, image_y = screen_to_image(mouse_pos[0], mouse_pos[1])
  142. screen_x, screen_y = image_to_screen(image_x, image_y)
  143. pygame.draw.circle(screen, (255, 0, 0), (int(screen_x), int(screen_y)), max(1, int(brush_size)), 1)
  144.  
  145. pygame.display.flip()
  146. clock.tick(60)
  147.  
  148. # Save the mask
  149. pygame.image.save(mask, mask_path)
  150. pygame.quit()
  151.  
  152. def process_images(input_dir, mask_dir, background_color):
  153. if not os.path.exists(mask_dir):
  154. os.makedirs(mask_dir)
  155.  
  156. for filename in os.listdir(input_dir):
  157. if filename.lower().endswith('.png'):
  158. image_path = os.path.join(input_dir, filename)
  159. mask_path = os.path.join(mask_dir, filename)
  160. create_mask(image_path, mask_path, background_color)
  161. print(f"Processed mask for {filename}")
  162.  
  163. def check_directories(input_dir, mask_dir):
  164. if os.path.abspath(input_dir) == os.path.abspath(mask_dir):
  165. print("WARNING: The input directory and mask directory are the same.")
  166. print("This may result in overwriting your original images.")
  167. response = input("Are you sure you want to continue? Type 'Y' in uppercase to proceed: ")
  168. if response != 'Y':
  169. print("Operation cancelled. Exiting the script.")
  170. return False
  171. return True
  172.  
  173. # Example usage
  174. input_directory = '.' # Current directory
  175. mask_directory = 'mask' # Directory for masks
  176. background_color_hex = '4D4D4D' # Background/Unmasked color (you can change this to any hex color)
  177. background_color_rgb = hex_to_rgb(background_color_hex)
  178.  
  179. # Check if it's safe to proceed
  180. if check_directories(input_directory, mask_directory):
  181. process_images(input_directory, mask_directory, background_color_rgb)
  182. else:
  183. print("Script execution terminated.")
Advertisement
Add Comment
Please, Sign In to add comment