page_resizer.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from krita import *
  2. from PyQt5.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QComboBox, QCheckBox, QPushButton
  3. from PyQt5.QtCore import *
  4. class PageResizer(Extension):
  5. def __init__(self, parent):
  6. super().__init__(parent)
  7. # Krita.instance() exists, so do any setup work
  8. def setup(self):
  9. pass
  10. # called after setup(self)
  11. def createActions(self, window):
  12. # Create menu item in Tools > Scripts.
  13. action = window.createAction("PageResizer", "Page Resizer")
  14. action.triggered.connect(self.resizer)
  15. def resizer(self):
  16. # Get the current document.
  17. self.doc = Krita.instance().activeDocument()
  18. # Create dialog.
  19. newDialog = QDialog()
  20. newDialog.setWindowTitle("Bleed Generator!")
  21. desc = QLabel('Adds bleed to the active paint layer')
  22. desc.setAlignment(Qt.AlignCenter)
  23. # Count and unit inputs. Default to 2mm.
  24. self.countInput = QLineEdit('2')
  25. self.unitInput = QComboBox()
  26. self.unitInput.addItems( ['mm', 'inch', 'px'] )
  27. self.unitIndex = 0 # track current unit.
  28. self.offsetInput = QLineEdit('0')
  29. # Side selection
  30. self.topCheck = QCheckBox('Top')
  31. self.topCheck.setChecked(True)
  32. self.botCheck = QCheckBox('Bottom')
  33. self.botCheck.setChecked(True)
  34. self.leftCheck = QCheckBox('Left')
  35. self.leftCheck.setChecked(True)
  36. self.rightCheck = QCheckBox('Right')
  37. self.rightCheck.setChecked(True)
  38. # Button!
  39. goButton = QPushButton("Add Bleed")
  40. goButton.setIcon( Krita.instance().icon('animation_play') )
  41. # create layouts
  42. row0 = QHBoxLayout()
  43. row1 = QHBoxLayout()
  44. row2 = QHBoxLayout()
  45. row3 = QHBoxLayout()
  46. row0.addWidget(desc)
  47. row1.addWidget(QLabel('How much bleed:'))
  48. row1.addWidget(self.countInput)
  49. row1.addWidget(self.unitInput)
  50. row1.addWidget(QLabel('Offset (0 = use edge):'))
  51. row1.addWidget(self.offsetInput)
  52. row2.addWidget(QLabel('Sides to bleed:'))
  53. row2.addWidget(self.topCheck)
  54. row2.addWidget(self.botCheck)
  55. row2.addWidget(self.leftCheck)
  56. row2.addWidget(self.rightCheck)
  57. if self.layer.type() != 'paintlayer':
  58. desc.setText('Please select a paint layer!')
  59. else:
  60. row3.addWidget(goButton)
  61. layoutMain = QVBoxLayout()
  62. layoutMain.addLayout(row0)
  63. layoutMain.addLayout(row1)
  64. layoutMain.addLayout(row2)
  65. layoutMain.addLayout(row3)
  66. newDialog.setLayout(layoutMain)
  67. # hook up the actions.
  68. goButton.clicked.connect( self.generateBleed )
  69. # self.unitInput.currentIndexChanged.connect( self.convertCount )
  70. # show the dialog.
  71. newDialog.exec_()
  72. ##########
  73. # Slots
  74. ##########
  75. # Actually generates the bleed!
  76. def generateBleed(self, e):
  77. # Calculate how many lines of pixels to copy.
  78. unit = self.unitInput.currentIndex()
  79. count = float(self.countInput.text())
  80. ppi = self.doc.resolution()
  81. if unit == 0: # mm
  82. count = count * (ppi / 24.5)
  83. if unit == 1: # inch
  84. count = count * ppi
  85. offset = round(float(self.offsetInput.text()))
  86. count = round(count) - offset
  87. print("Pixel bleed amount: ", count)
  88. print("Edge offset: ", offset)
  89. # Copy lines for selected sides.
  90. if self.topCheck.checkState():
  91. bds = self.layer.bounds()
  92. xpos = bds.left()
  93. ypos = bds.top() - offset
  94. len = bds.width()
  95. bleedline = self.layer.pixelData(xpos, ypos, len, 1)
  96. for c in range(1, count+1):
  97. self.layer.setPixelData(bleedline, xpos, (ypos - c), len, 1)
  98. print("Top lines cloned: ", count)
  99. if self.botCheck.checkState():
  100. bds = self.layer.bounds()
  101. xpos = bds.left()
  102. ypos = bds.bottom() + offset
  103. len = bds.width()
  104. bleedline = self.layer.pixelData(xpos, ypos, len, 1)
  105. for c in range(1, count+1):
  106. self.layer.setPixelData(bleedline, xpos, (ypos + c), len, 1)
  107. print("Bottom lines cloned: ", count)
  108. if self.leftCheck.checkState():
  109. bds = self.layer.bounds()
  110. xpos = bds.left() - offset
  111. ypos = bds.top()
  112. len = bds.height()
  113. bleedline = self.layer.pixelData(xpos, ypos, 1, len)
  114. for c in range(1, count+1):
  115. self.layer.setPixelData(bleedline, (xpos - c), ypos, 1, len)
  116. print("Left lines cloned: ", count)
  117. if self.rightCheck.checkState():
  118. bds = self.layer.bounds()
  119. xpos = bds.right() + offset
  120. ypos = bds.top()
  121. len = bds.height()
  122. bleedline = self.layer.pixelData(xpos, ypos, 1, len)
  123. for c in range(1, count+1):
  124. self.layer.setPixelData(bleedline, (xpos + c), ypos, 1, len)
  125. print("Right lines cloned: ", count)
  126. self.doc.refreshProjection()
  127. # Convert count input values to relevant units
  128. def convertCount():
  129. unitIndex = unitInput.currentIndex()
  130. print("Updated unit index: ", unitIndex)