Advertisement
Guest User

Copy image and split

a guest
Jul 7th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import os
  2. def main():
  3. image = pickAFile() #pick the file
  4. #imgCheck(image)
  5. fileSize(image)
  6. # Pick the file and pass it on to the next part
  7.  
  8. #def imgCheck(image): #check for all image extensions compatible with python
  9. #here you need to do the checks on whether the file picked is a file compatible with jython
  10. # try:
  11. #some code here that will throw an exception if an incompatible file is tried, i recommend getHeight(makePicture(image))
  12. # except:
  13. #show an error message: showInformation("error") etc
  14. # else:
  15. # fileSize(image)
  16.  
  17. def fileSize(image): # find the size of the file, dale already gave us this code
  18. fileStats = os.stat(image)
  19. fsize = fileStats.st_size
  20. numberSlices(fsize, image)
  21.  
  22. def numberSlices(fsize, image): #find the number of slices needed to keep each slice below 20000bytes
  23. sliceLimit = 20000
  24. numberSlices = (fsize / sliceLimit) + 1 #add 1 such that uneven divisions will always result in a slice size less than 20000 bytes
  25. showInformation("Number of Slices:" + str(numberSlices))
  26. if numberSlices == 1:
  27. showInformation("This image does not need to be split and can be uploaded")
  28. else:
  29. fileExtension(numberSlices, image)
  30.  
  31. def fileExtension(numberSlices, image):
  32. #find the file extension and the file path minus the extension
  33. fileExtension = os.path.splitext(image)[1]
  34. filePath = os.path.splitext(image)[0]
  35. splitImage(numberSlices, image, fileExtension, filePath)
  36.  
  37. def splitImage(numberSlices, image, fileExtension, filePath): #time to split the image, into noSlices many pieces
  38. pic = makePicture(image)
  39. imageOrder = 0 #number each file in the order it is saved
  40. imageHeight = getHeight(pic)
  41. imageWidth = getWidth(pic)
  42. imageX = 0 #x pixel coordinate
  43. imageY = 0 #y pixel coordinate this has to increment
  44. splitHeight = imageHeight / (numberSlices)
  45. imgHeight = splitHeight
  46.  
  47.  
  48. for imageOrder in range(0, (numberSlices)):
  49.  
  50. canvas = makeEmptyPicture(imageWidth,splitHeight)
  51. fileName = (filePath + "_" + str(imageOrder) + "_" + str(numberSlices) + fileExtension)
  52. for sourceX in range(0, imageWidth):
  53. for sourceY in range(imageY, imgHeight):
  54. color = getColor(getPixel(pic, sourceX, sourceY))
  55. setColor(getPixel(canvas, sourceX, sourceY), color)
  56.  
  57. writePictureTo(canvas, fileName)
  58.  
  59. imageY = imageY + splitHeight
  60. imgHeight = imgHeight + splitHeight
  61. imageOrder = imageOrder + 1
  62.  
  63.  
  64. #loop for the last slice starts here this has 2 loops as well.
  65. #you need to find the height of the last slice in pixels. I used lastHeight = imageHeight - (splitHeight * (numberslices - 1))
  66. #then specify the fileName again because its a variable
  67. #canvas = #make the canvas again but this time with the new split height
  68. #then start the copy function again and make sure you use the right y axis pixels.... think about this.
  69.  
  70. #writePictureTo(canvas, fileName)
  71. print "all done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement