remghoost

webp to png converter full ChatGPT interaction

Dec 20th, 2022
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.03 KB | None | 0 0
  1. can you write me a python script with a gui that has a window i can drag and drop webp pictures into from the windows file explorer? it will convert them to png using ffmpeg and put the new png in the same folder as the input image with the same name but "converted" at the end of it.
  2.  
  3. put your response in a code block.
  4.  
  5. -------------------
  6.  
  7. Certainly! Here is a Python script that uses the PyQt5 library to create a GUI with a window that you can drag and drop webp images into. The script will then use the ffmpeg command line tool to convert the webp images to PNG format and save them in the same folder as the input images, with "converted" added to the end of the file name.
  8.  
  9. import os
  10. import subprocess
  11. from PyQt5 import QtWidgets, QtGui
  12.  
  13. class MainWindow(QtWidgets.QMainWindow):
  14. def __init__(self):
  15. super().__init__()
  16.  
  17. # Set up the main window
  18. self.setWindowTitle("Webp to PNG Converter")
  19. self.setAcceptDrops(True)
  20.  
  21. # Create a label to display the dropped file name
  22. self.label = QtWidgets.QLabel("Drop a webp file here")
  23. self.setCentralWidget(self.label)
  24.  
  25. def dragEnterEvent(self, event):
  26. # Accept the drag event if a file is being dragged
  27. if event.mimeData().hasUrls():
  28. event.accept()
  29. else:
  30. event.ignore()
  31.  
  32. def dropEvent(self, event):
  33. # Get the file path of the dropped file
  34. file_path = event.mimeData().urls()[0].toLocalFile()
  35.  
  36. # Check if the file is a webp file
  37. if not file_path.endswith(".webp"):
  38. self.label.setText("Error: Not a webp file")
  39. return
  40.  
  41. # Convert the webp file to PNG using ffmpeg
  42. file_name, _ = os.path.splitext(os.path.basename(file_path))
  43. output_path = os.path.join(os.path.dirname(file_path), file_name + "_converted.png")
  44. subprocess.run(["ffmpeg", "-i", file_path, output_path])
  45.  
  46. # Update the label to show the conversion was successful
  47. self.label.setText("Successfully converted {} to {}".format(file_path, output_path))
  48.  
  49. if __name__ == "__main__":
  50. app = QtWidgets.QApplication([])
  51. window = MainWindow()
  52. window.show()
  53. app.exec_()
  54.  
  55. To use this script, you will need to have the PyQt5 library installed, as well as the ffmpeg command line tool. You can install PyQt5 using pip install PyQt5, and you can install ffmpeg by following the instructions on the FFmpeg website.
  56.  
  57. I hope this helps! Let me know if you have any questions or if you need further assistance.
  58.  
  59. --------------------
  60.  
  61. can you make the gui a bit more pretty? and have the file name word wrapped?
  62.  
  63. -------------------
  64.  
  65. Certainly! Here is an updated version of the script that includes some styling and word wrapping for the file name label:
  66.  
  67. import os
  68. import subprocess
  69. from PyQt5 import QtWidgets, QtGui
  70.  
  71. class MainWindow(QtWidgets.QMainWindow):
  72. def __init__(self):
  73. super().__init__()
  74.  
  75. # Set up the main window
  76. self.setWindowTitle("Webp to PNG Converter")
  77. self.setAcceptDrops(True)
  78.  
  79. # Create a label to display the dropped file name
  80. self.label = QtWidgets.QLabel("Drop a webp file here")
  81. self.label.setWordWrap(True)
  82. self.label.setStyleSheet("QLabel { background-color: white; border: 1px solid gray; padding: 10px; }")
  83. self.setCentralWidget(self.label)
  84.  
  85. def dragEnterEvent(self, event):
  86. # Accept the drag event if a file is being dragged
  87. if event.mimeData().hasUrls():
  88. event.accept()
  89. else:
  90. event.ignore()
  91.  
  92. def dropEvent(self, event):
  93. # Get the file path of the dropped file
  94. file_path = event.mimeData().urls()[0].toLocalFile()
  95.  
  96. # Check if the file is a webp file
  97. if not file_path.endswith(".webp"):
  98. self.label.setText("Error: Not a webp file")
  99. return
  100.  
  101. # Convert the webp file to PNG using ffmpeg
  102. file_name, _ = os.path.splitext(os.path.basename(file_path))
  103. output_path = os.path.join(os.path.dirname(file_path), file_name + "_converted.png")
  104. subprocess.run(["ffmpeg", "-i", file_path, output_path])
  105.  
  106. # Update the label to show the conversion was successful
  107. self.label.setText("Successfully converted {}\nto {}".format(file_path, output_path))
  108.  
  109. if __name__ == "__main__":
  110. app = QtWidgets.QApplication([])
  111. window = MainWindow()
  112. window.show()
  113. app.exec_()
  114.  
  115. This script uses the QLabel.setWordWrap method to enable word wrapping for the file name label, and it uses the QLabel.setStyleSheet method to apply some basic styling to the label.
  116.  
  117. I hope this helps! Let me know if you have any questions or if you need further assistance.
  118.  
  119. -----------------------
  120.  
  121. can you make a checkbox in the gui that will automatically confirm overwrites?
  122.  
  123. -------------------------
  124.  
  125. Certainly! Here is an updated version of the script that includes a checkbox to enable automatic confirmation of overwrites:
  126.  
  127. import os
  128. import subprocess
  129. from PyQt5 import QtWidgets, QtGui
  130.  
  131. class MainWindow(QtWidgets.QMainWindow):
  132. def __init__(self):
  133. super().__init__()
  134.  
  135. # Set up the main window
  136. self.setWindowTitle("Webp to PNG Converter")
  137. self.setAcceptDrops(True)
  138.  
  139. # Create a label to display the dropped file name
  140. self.label = QtWidgets.QLabel("Drop a webp file here")
  141. self.label.setWordWrap(True)
  142. self.label.setStyleSheet("QLabel { background-color: white; border: 1px solid gray; padding: 10px; }")
  143.  
  144. # Create a checkbox to enable automatic confirmation of overwrites
  145. self.overwrite_checkbox = QtWidgets.QCheckBox("Automatically confirm overwrites")
  146. self.overwrite_checkbox.setChecked(False)
  147.  
  148. # Create a layout to hold the label and checkbox
  149. layout = QtWidgets.QVBoxLayout()
  150. layout.addWidget(self.label)
  151. layout.addWidget(self.overwrite_checkbox)
  152.  
  153. # Set the layout as the central widget of the main window
  154. central_widget = QtWidgets.QWidget()
  155. central_widget.setLayout(layout)
  156. self.setCentralWidget(central_widget)
  157.  
  158. def dragEnterEvent(self, event):
  159. # Accept the drag event if a file is being dragged
  160. if event.mimeData().hasUrls():
  161. event.accept()
  162. else:
  163. event.ignore()
  164.  
  165. def dropEvent(self, event):
  166. # Get the file path of the dropped file
  167. file_path = event.mimeData().urls()[0].toLocalFile()
  168.  
  169. # Check if the file is a webp file
  170. if not file_path.endswith(".webp"):
  171. self.label.setText("Error: Not a webp file")
  172. return
  173.  
  174. # Convert the webp file to PNG using ffmpeg
  175. file_name, _ = os.path.splitext(os.path.basename(file_path))
  176. output_path = os.path.join(os.path.dirname(file_path), file_name + "_converted.png")
  177. command = ["ffmpeg", "-y", "-i", file_path, output_path] # "-y" flag automatically confirms overwrites
  178. if not self.overwrite_checkbox.isChecked():
  179. command.remove("-y") # remove the "-y" flag if the checkbox is not checked
  180. subprocess.run(command)
  181.  
  182. # Update the label to show the conversion was successful
  183. self.label.setText("Successfully converted {}\nto {}".format(file_path, output_path))
  184.  
  185. if __name__ == "__main__":
  186. app = QtWidgets.QApplication([])
  187. window = MainWindow()
  188. window.show()
  189. app.exec_()
  190.  
  191. This script adds a checkbox to the GUI using the QCheckBox widget, and it uses the
  192.  
  193. --------------------
  194.  
  195. can you make the gui and window dark mode?
  196.  
  197. --------------------
  198.  
  199. Certainly! Here is an updated version of the script that includes a dark mode style for the GUI and window:
  200.  
  201. import os
  202. import subprocess
  203. from PyQt5 import QtWidgets, QtGui
  204.  
  205. class MainWindow(QtWidgets.QMainWindow):
  206. def __init__(self):
  207. super().__init__()
  208.  
  209. # Set up the main window
  210. self.setWindowTitle("Webp to PNG Converter")
  211. self.setAcceptDrops(True)
  212. self.setStyleSheet("QMainWindow { background-color: #333333; }") # set the background color to dark gray
  213.  
  214. # Create a label to display the dropped file name
  215. self.label = QtWidgets.QLabel("Drop a webp file here")
  216. self.label.setWordWrap(True)
  217. self.label.setStyleSheet("QLabel { background-color: #444444; border: 1px solid gray; padding: 10px; color: white; }") # set the label's background color to light gray and its text color to white
  218.  
  219. # Create a checkbox to enable automatic confirmation of overwrites
  220. self.overwrite_checkbox = QtWidgets.QCheckBox("Automatically confirm overwrites")
  221. self.overwrite_checkbox.setChecked(False)
  222. self.overwrite_checkbox.setStyleSheet("QCheckBox { color: white; }") # set the checkbox's text color to white
  223.  
  224. # Create a layout to hold the label and checkbox
  225. layout = QtWidgets.QVBoxLayout()
  226. layout.addWidget(self.label)
  227. layout.addWidget(self.overwrite_checkbox)
  228.  
  229. # Set the layout as the central widget of the main window
  230. central_widget = QtWidgets.QWidget()
  231. central_widget.setLayout(layout)
  232. self.setCentralWidget(central_widget)
  233.  
  234. def dragEnterEvent(self, event):
  235. # Accept the drag event if a file is being dragged
  236. if event.mimeData().hasUrls():
  237. event.accept()
  238. else:
  239. event.ignore()
  240.  
  241. def dropEvent(self, event):
  242. # Get the file path of the dropped file
  243. file_path = event.mimeData().urls()[0].toLocalFile()
  244.  
  245. # Check if the file is a webp file
  246. if not file_path.endswith(".webp"):
  247. self.label.setText("Error: Not a webp file")
  248. return
  249.  
  250. # Convert the webp file to PNG using ffmpeg
  251. file_name, _ = os.path.splitext(os.path.basename(file_path))
  252. output_path = os.path.join(os.path.dirname(file_path), file_name + "_converted.png")
  253. command = ["ffmpeg", "-y", "-i", file_path, output_path] # "-y" flag automatically confirms overwrites
  254. if not self.overwrite_checkbox.isChecked():
  255. command.remove("-y") # remove the "-y" flag if the checkbox is not checked
  256. subprocess.run(command)
  257.  
  258. # Update the label to show the conversion was successful
  259. self.label.setText("
  260.  
  261.  
  262. -------------------------
  263.  
  264. continue
  265.  
  266. -------------------------
  267.  
  268. Sorry about that! Here is the rest of the script:
  269.  
  270. Successfully converted {}\nto {}".format(file_path, output_path))
  271.  
  272. if __name__ == "__main__":
  273. app = QtWidgets.QApplication([])
  274. window = MainWindow()
  275. window.show()
  276. app.exec_()
  277.  
  278. This script sets the background color of the main window to dark gray using the QMainWindow.setStyleSheet method, and it sets the background color and text color of the label to light gray and white, respectively, using the QLabel.setStyleSheet method. It also sets the text color of the checkbox to white using the QCheckBox.setStyleSheet method.
  279.  
  280. I hope this helps! Let me know if you have any questions or if you need further assistance.
  281.  
  282. ------------------------
  283.  
  284. can you make it able to process multiple files at once? i'd like to be able to drag a couple of pictures to it
  285.  
  286. ------------------------
  287.  
  288. Certainly! Here is an updated version of the script that allows you to process multiple files at once by dragging multiple webp images into the window:
  289.  
  290. import os
  291. import subprocess
  292. from PyQt5 import QtWidgets, QtGui
  293.  
  294. class MainWindow(QtWidgets.QMainWindow):
  295. def __init__(self):
  296. super().__init__()
  297.  
  298. # Set up the main window
  299. self.setWindowTitle("Webp to PNG Converter")
  300. self.setAcceptDrops(True)
  301. self.setStyleSheet("QMainWindow { background-color: #333333; }") # set the background color to dark gray
  302.  
  303. # Create a label to display the dropped file names
  304. self.label = QtWidgets.QLabel("Drop webp files here")
  305. self.label.setWordWrap(True)
  306. self.label.setStyleSheet("QLabel { background-color: #444444; border: 1px solid gray; padding: 10px; color: white; }") # set the label's background color to light gray and its text color to white
  307.  
  308. # Create a checkbox to enable automatic confirmation of overwrites
  309. self.overwrite_checkbox = QtWidgets.QCheckBox("Automatically confirm overwrites")
  310. self.overwrite_checkbox.setChecked(False)
  311. self.overwrite_checkbox.setStyleSheet("QCheckBox { color: white; }") # set the checkbox's text color to white
  312.  
  313. # Create a layout to hold the label and checkbox
  314. layout = QtWidgets.QVBoxLayout()
  315. layout.addWidget(self.label)
  316. layout.addWidget(self.overwrite_checkbox)
  317.  
  318. # Set the layout as the central widget of the main window
  319. central_widget = QtWidgets.QWidget()
  320. central_widget.setLayout(layout)
  321. self.setCentralWidget(central_widget)
  322.  
  323. def dragEnterEvent(self, event):
  324. # Accept the drag event if a file is being dragged
  325. if event.mimeData().hasUrls():
  326. event.accept()
  327. else:
  328. event.ignore()
  329.  
  330. def dropEvent(self, event):
  331. # Get the file paths of the dropped files
  332. file_paths = [url.toLocalFile() for url in event.mimeData().urls()]
  333.  
  334. # Check if all the files are webp files
  335. if not all(path.endswith(".webp") for path in file_paths):
  336. self.label.setText("Error: Not all files are webp files")
  337. return
  338.  
  339. # Convert the webp files to PNG using ffmpeg
  340. converted_files = []
  341. for file_path in file_paths:
  342. file_name, _ = os.path.splitext(os.path.basename(file_path))
  343. output_path = os.path.join(os.path.dirname(file_path), file_name + "_converted.png")
  344. command = ["ffmpeg", "-y", "-i", file_path, output_path] # "-y" flag automatically confirms overwrites
  345. if not self.overwrite_checkbox.isChecked():
  346. command.remove("-y") # remove the "-y
  347.  
  348.  
  349. ----------------------
  350.  
  351. continue
  352.  
  353. ----------------------
  354.  
  355. Sorry about that! Here is the rest of the script:
  356.  
  357. subprocess.run(command)
  358. converted_files.append((file_path, output_path))
  359.  
  360. # Update the label to show the conversion was successful
  361. self.label.setText("Successfully converted:\n" + "\n".join("{} to {}".format(input_path, output_path) for input_path, output_path in converted_files))
  362.  
  363. if __name__ == "__main__":
  364. app = QtWidgets.QApplication([])
  365. window = MainWindow()
  366. window.show()
  367. app.exec_()
  368.  
  369. This script uses a list comprehension to get the file paths of the dropped files, and it uses the all function to check if all the files are webp files. It then loops through the file paths and converts each file to a PNG using ffmpeg. Finally, it updates the label to show the list of successfully converted files.
  370.  
  371. I hope this helps! Let me know if you have any questions or if you need further assistance.
  372.  
  373. ---------------------
  374.  
  375. which line do i change to make the textbox automatically be checked when the program starts?
  376.  
  377. ---------------------
  378.  
  379. To make the checkbox automatically checked when the program starts, you can set the QCheckBox.setChecked method to True when you create the checkbox:
  380.  
  381. self.overwrite_checkbox = QtWidgets.QCheckBox("Automatically confirm overwrites")
  382. self.overwrite_checkbox.setChecked(True) # set the checkbox to be checked by default
  383. self.overwrite_checkbox.setStyleSheet("QCheckBox { color: white; }") # set the checkbox's text color to white
  384.  
  385. Alternatively, you can set the QCheckBox.setChecked method to True after creating the checkbox:
  386.  
  387. self.overwrite_checkbox = QtWidgets.QCheckBox("Automatically confirm overwrites")
  388. self.overwrite_checkbox.setStyleSheet("QCheckBox { color: white; }") # set the checkbox's text color to white
  389. self.overwrite_checkbox.setChecked(True) # set the checkbox to be checked by default
  390.  
  391. Either of these approaches should work to make the checkbox checked by default when the program starts.
  392.  
  393. I hope this helps! Let me know if you have any questions or if you need further assistance.
Advertisement
Add Comment
Please, Sign In to add comment