Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. import sys
  2. import math
  3. from PyQt5.QtGui import QPixmap, QColor
  4. from PyQt5.QtCore import *
  5. from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication, QLabel, QFileDialog, QPushButton, QSlider
  6.  
  7.  
  8. class Colors(QWidget):
  9.  
  10. def __init__(self):
  11. super().__init__()
  12. self.img = ''
  13. self.label = QLabel(self.window())
  14. self.label.resize(670, 500)
  15. self.label.move(25, 25)
  16. self.pixmap = QPixmap()
  17. self.init_ui()
  18.  
  19. def init_ui(self):
  20. self.resize(1200, 900)
  21. self.center()
  22. self.setWindowTitle('Center')
  23.  
  24. self.h_label = QLabel('H', self)
  25. self.h_label.move(30, 716)
  26. self.h_sld = QSlider(Qt.Horizontal, self)
  27. self.h_sld.setRange(0, 360)
  28. self.h_sld.setPageStep(1)
  29. self.h_sld.move(50, 700)
  30. self.h_sld.resize(500, 50)
  31.  
  32. self.s_label = QLabel('S', self)
  33. self.s_label.move(30, 736)
  34. self.s_sld = QSlider(Qt.Horizontal, self)
  35. self.s_sld.setRange(0, 100)
  36. self.s_sld.setPageStep(1)
  37. self.s_sld.move(50, 720)
  38. self.s_sld.resize(500, 50)
  39.  
  40. self.v_label = QLabel('V', self)
  41. self.v_label.move(30, 756)
  42. self.v_sld = QSlider(Qt.Horizontal, self)
  43. self.v_sld.setRange(0, 100)
  44. self.v_sld.setPageStep(1)
  45. self.v_sld.move(50, 740)
  46. self.v_sld.resize(500, 50)
  47.  
  48. choose_img = QPushButton('Открыть', self)
  49. choose_img.move(700, 50)
  50. choose_img.clicked.connect(self.open_on_click)
  51.  
  52. save_img = QPushButton('Сохранить', self)
  53. save_img.move(800, 50)
  54. save_img.clicked.connect(self.save_on_click)
  55.  
  56. to_grey = QPushButton('RGB -> Grey', self)
  57. to_grey.move(700, 85)
  58. to_grey.clicked.connect(self.to_grey_on_click)
  59.  
  60. to_red = QPushButton('RGB -> Red', self)
  61. to_red.move(800, 85)
  62. to_red.clicked.connect(self.to_red_on_click)
  63.  
  64. to_green = QPushButton('RGB -> Green', self)
  65. to_green.move(700, 120)
  66. to_green.clicked.connect(self.to_green_on_click)
  67.  
  68. to_blue = QPushButton('RGB -> Blue', self)
  69. to_blue.move(800, 120)
  70. to_blue.clicked.connect(self.to_blue_on_click)
  71.  
  72. to_hsv = QPushButton('To HSV', self)
  73. to_hsv.move(700, 730)
  74. to_hsv.clicked.connect(self.to_hsv)
  75.  
  76.  
  77. self.show()
  78.  
  79.  
  80. def to_grey_on_click(self):
  81. img = self.pixmap.toImage()
  82. for x in range(img.width()):
  83. for y in range(img.height()):
  84. r = QColor(img.pixel(x, y)).red()
  85. g = QColor(img.pixel(x, y)).green()
  86. b = QColor(img.pixel(x, y)).blue()
  87. a = (0.2126 * r + 0.7152 * g + 0.0722 * b)
  88. img.setPixel(x, y, QColor(a, a, a).rgb())
  89. self.label.setPixmap(QPixmap(img))
  90. self.show()
  91.  
  92. def to_red_on_click(self):
  93. img = self.pixmap.toImage()
  94. for x in range(img.width()):
  95. for y in range(img.height()):
  96. r = QColor(img.pixel(x, y)).red()
  97. img.setPixel(x, y, QColor(r, 0, 0).rgb())
  98. self.label.setPixmap(QPixmap(img))
  99. self.show()
  100.  
  101. def to_green_on_click(self):
  102. img = self.pixmap.toImage()
  103. for x in range(img.width()):
  104. for y in range(img.height()):
  105. g = QColor(img.pixel(x, y)).green()
  106. img.setPixel(x, y, QColor(0, g, 0).rgb())
  107. self.label.setPixmap(QPixmap(img))
  108. self.show()
  109.  
  110. def to_blue_on_click(self):
  111. img = self.pixmap.toImage()
  112. for x in range(img.width()):
  113. for y in range(img.height()):
  114. b = QColor(img.pixel(x, y)).blue()
  115. img.setPixel(x, y, QColor(0, 0, b).rgb())
  116. self.label.setPixmap(QPixmap(img))
  117. self.show()
  118.  
  119. def open_file_name_dialog(self):
  120. options = QFileDialog.Options()
  121. options |= QFileDialog.DontUseNativeDialog
  122. filename, _ = QFileDialog.getOpenFileName(self, "QFileDialog.getOpenFileName()", "",
  123. "Images (*.jpg *.jpeg *.png)", options=options)
  124. if filename:
  125. self.pixmap = QPixmap(filename).scaled(670, 500)
  126.  
  127. def to_hsv(self):
  128. self.hsv()
  129.  
  130. def hsv(self):
  131. img = self.pixmap.toImage()
  132. for x in range(img.width()):
  133. for y in range(img.height()):
  134. r = QColor(img.pixel(x, y)).red()
  135. g = QColor(img.pixel(x, y)).green()
  136. b = QColor(img.pixel(x, y)).blue()
  137. h, s, v = self.rgb2hsv(r, g, b)
  138. dh = self.h_sld.value()
  139. ds = self.s_sld.value() * 0.01
  140. dv = self.v_sld.value() * 0.01
  141. h1 = (h + dh) % 360
  142. s1 = max(min(s + ds, 1), 0)
  143. v1 = max(min(v + dv, 1), 0)
  144. r1, g1, b1 = self.hsv2rgb(h1, s1, v1)
  145. img.setPixel(x, y, QColor(r1, g1, b1).rgb())
  146. self.label.setPixmap(QPixmap(img))
  147. self.show()
  148.  
  149. def rgb2hsv(self, r, g, b):
  150. r, g, b = r / 255.0, g / 255.0, b / 255.0
  151. mx = max(r, g, b)
  152. mn = min(r, g, b)
  153. df = mx - mn
  154. if mx == mn:
  155. h = 0
  156. elif mx == r:
  157. h = (60 * ((g - b) / df) + 360) % 360
  158. elif mx == g:
  159. h = (60 * ((b - r) / df) + 120) % 360
  160. elif mx == b:
  161. h = (60 * ((r - g) / df) + 240) % 360
  162. if mx == 0:
  163. s = 0
  164. else:
  165. s = df / mx
  166. v = mx
  167. return h, s, v
  168.  
  169. def hsv2rgb(self, h, s, v):
  170. h = float(h)
  171. s = float(s)
  172. v = float(v)
  173. h60 = h / 60.0
  174. h60f = math.floor(h60)
  175. hi = int(h60f) % 6
  176. f = h60 - h60f
  177. p = v * (1 - s)
  178. q = v * (1 - f * s)
  179. t = v * (1 - (1 - f) * s)
  180. r, g, b = 0, 0, 0
  181. if hi == 0:
  182. r, g, b = v, t, p
  183. elif hi == 1:
  184. r, g, b = q, v, p
  185. elif hi == 2:
  186. r, g, b = p, v, t
  187. elif hi == 3:
  188. r, g, b = p, q, v
  189. elif hi == 4:
  190. r, g, b = t, p, v
  191. elif hi == 5:
  192. r, g, b = v, p, q
  193. r, g, b = int(r * 255), int(g * 255), int(b * 255)
  194. return r, g, b
  195.  
  196. def open_on_click(self):
  197. self.open_file_name_dialog()
  198. self.label.setPixmap(self.pixmap)
  199.  
  200. def save_on_click(self):
  201. self.save_file_dialog()
  202.  
  203. def save_file_dialog(self):
  204. options = QFileDialog.Options()
  205. options |= QFileDialog.DontUseNativeDialog
  206. filename, _ = QFileDialog.getSaveFileName(self, "QFileDialog.getSaveFileName()", "",
  207. "Images (*.jpg *.jpeg *.png)", options=options)
  208. if filename:
  209. self.label.pixmap().save(filename)
  210.  
  211. def center(self):
  212. qr = self.frameGeometry()
  213. cp = QDesktopWidget().availableGeometry().center()
  214. qr.moveCenter(cp)
  215. self.move(qr.topLeft())
  216.  
  217.  
  218. if __name__ == '__main__':
  219. app = QApplication(sys.argv)
  220. ex = Colors()
  221. sys.exit(app.exec_())
  222.  
  223. #!/usr/bin/env python3
  224. """Usage: to-grey <image>"""
  225. import sys
  226. from PyQt5.Qt import QApplication, QColor, QIcon, QPixmap, QSize, QToolButton
  227.  
  228. def on_click():
  229. img = w.icon().pixmap(qsize).toImage()
  230. for x in range(img.width()):
  231. for y in range(img.height()):
  232. r = QColor(img.pixel(x, y)).red()
  233. g = QColor(img.pixel(x, y)).green()
  234. b = QColor(img.pixel(x, y)).blue()
  235. a = (0.2126 * r + 0.7152 * g + 0.0722 * b)
  236. img.setPixel(x, y, QColor(a, a, a).rgb())
  237. w.setIcon(QIcon(QPixmap(img)))
  238.  
  239. app = QApplication(sys.argv)
  240. if len(app.arguments()) != 2:
  241. sys.exit(__doc__)
  242. w = QToolButton()
  243. w.setIcon(QIcon(app.arguments()[1]))
  244. qsize = QSize(300, 300)
  245. w.setIconSize(qsize)
  246. w.clicked.connect(on_click)
  247. # center
  248. w.adjustSize() # update w.rect() now
  249. w.move(app.desktop().screen().rect().center() - w.rect().center())
  250. w.show()
  251. sys.exit(app.exec_())
  252.  
  253. $ python3 to-gray.py ~/Pictures/example.jpg
  254.  
  255. $ python -mprofile --sort cumtime to-gray.py ~/Pictures/example.jpg
  256.  
  257. $ pip install pprofile
  258. $ pprofile --exclude-syspath to-gray.py ~/Pictures/example.jpg
  259.  
  260. #!/usr/bin/env python3
  261. """Usage: to-grey <image>"""
  262. import sys
  263. import numpy as np # $ pip install numpy
  264. from PyQt5.Qt import QApplication, QIcon, QImage, QPixmap, QSize, QToolButton
  265.  
  266. def on_click():
  267. # r = arr[..., 0]
  268. # g = arr[..., 1]
  269. # b = arr[..., 2]
  270. # a = (0.2126 * r + 0.7152 * g + 0.0722 * b)
  271. a = arr @ [0.2126, 0.7152, 0.0722] # np.dot
  272. # arr[..., 0] = a
  273. # arr[..., 1] = a
  274. # arr[..., 2] = a
  275. arr[...] = a[..., np.newaxis]
  276.  
  277. w.setIcon(QIcon(QPixmap(qimage)))
  278.  
  279. app = QApplication(sys.argv)
  280. if len(app.arguments()) != 2:
  281. sys.exit(__doc__)
  282. w = QToolButton()
  283. qimage = QImage(app.arguments()[1]).convertToFormat(QImage.Format_RGB888)
  284. w.setIcon(QIcon(QPixmap(qimage)))
  285. width, height = qimage.width(), qimage.height()
  286. w.setIconSize(QSize(width, height))
  287. ptr = qimage.bits()
  288. ptr.setsize(qimage.byteCount())
  289. arr = np.ndarray((width, height, 3),
  290. dtype=np.uint8, buffer=ptr) # share data
  291. w.clicked.connect(on_click)
  292. # center
  293. w.adjustSize() # update w.rect() now
  294. w.move(app.desktop().screen().rect().center() - w.rect().center())
  295. w.show()
  296. sys.exit(app.exec_())
  297.  
  298. def rgb2gray_rgb(arr):
  299. a = arr @ [0.2126, 0.7152, 0.0722]
  300. arr[...] = a[..., np.newaxis]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement