Ce projet essai, en partant d'une IMAGESOURCE de : - appliquer des effets à l'image - la redimmensionner - changer son mode de couleur - la HAACHEER...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

haacheuur.py 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #!/usr/bin/python
  2. # coding: utf-8
  3. # haacheuur 0.24
  4. # port industriel de port la nouvelle - couleur - 60cm*30cm
  5. # image source : pln.jpg
  6. # image obtenue : pln..20150910-11h59m53s.jpg
  7. import sys
  8. import Image
  9. import random
  10. import os
  11. import ImageDraw
  12. import ImageFont
  13. import ImageFilter
  14. from time import gmtime, strftime
  15. import time
  16. # modifs du 30/10/2013
  17. import ImageEnhance
  18. '''
  19. multiples 1024 : 1024 2048 3072 4096 5120 6144 7168 8192 9216 10240
  20. multiples 768 : 768 1536 2304 3072 3840 4608 5376 6144 6912 7680 8448 9216 9984 10752
  21. '''
  22. #rapport d'allongement de la nouvelle image par rapport à la largeur de l'image originale
  23. allongement = 2
  24. #ouvertrure de l'image source et conversion en mode couleur 1bit
  25. #im1 = Image.open(str(sys.argv[1])).convert('1')
  26. #im1 = Image.open(str(sys.argv[1])).convert('L')
  27. im1 = Image.open(str(sys.argv[1]))
  28. im2 = im1.copy()
  29. im3 = Image.new("RGBA",(im1.size[0], im1.size[1]))
  30. im4 = Image.new("RGBA",(im1.size[0]*allongement, im1.size[1]))
  31. boutImage = Image.new("RGBA",(im1.size[0], im1.size[1]))
  32. pix2 = im3.resize((im3.size[0]/2, im3.size[1]/2), Image.NEAREST)
  33. pix4 = im3.resize((im3.size[0]/4, im3.size[1]/4), Image.NEAREST)
  34. pix8 = im3.resize((im3.size[0]/8, im3.size[1]/8), Image.NEAREST)
  35. pix16 = im3.resize((im3.size[0]/16, im3.size[1]/16), Image.NEAREST)
  36. pix32 = im3.resize((im3.size[0]/32, im3.size[1]/32), Image.NEAREST)
  37. pix64 = im3.resize((im3.size[0]/64, im3.size[1]/64), Image.NEAREST)
  38. pix128 = im3.resize((im3.size[0]/128, im3.size[1]/128), Image.NEAREST)
  39. pix256 = im3.resize((im3.size[0]/256, im3.size[1]/256), Image.NEAREST)
  40. pix512 = im3.resize((im3.size[0]/512, im3.size[1]/512), Image.NEAREST)
  41. pix1024 = im3.resize((im3.size[0]/1024, im3.size[1]/1024), Image.NEAREST)
  42. Larg = im1.size[0]
  43. Haut = im1.size[1]
  44. import pickle
  45. loadfile = False
  46. class Sequence:
  47. def __init__(s):
  48. randomCoupeHauteur = []
  49. s.randomCopyPosi =[]
  50. s.proportions=[]
  51. s.choix=[]
  52. s.sizeOutput=None
  53. s.sizeInput=None
  54. """
  55. seq = dict()
  56. seq["randomCoupeHauteur"] = []
  57. seq["randomCopyPosi"] = []
  58. seq["proportions"] = []
  59. seq["choix"] = []
  60. seq["sizeOutput"]= im4.size
  61. seq["sizeInput"] = im1.size
  62. """
  63. if loadfile:
  64. seq=pickle.load(open("test.pickle"))
  65. else :
  66. seq=Sequence()
  67. for i in range(1):
  68. # constitution de la liste des tranches horizontales
  69. # genre comme si qu'on avait un 16 pistes :)
  70. # nombre aleatoire compris dans les limites de l'image
  71. def randHaut():
  72. return random.randint(0, im1.size[1]/16)*16
  73. if loadfile:
  74. randomCoupeHauteur = seq.randomCoupeHauteur
  75. else:
  76. randomCoupeHauteur = [0, \
  77. randHaut(),randHaut(),randHaut(),randHaut(), \
  78. randHaut(),randHaut(),randHaut(),randHaut(), \
  79. randHaut(),randHaut(),randHaut(),randHaut(), \
  80. randHaut(),randHaut(),randHaut(),randHaut(), \
  81. randHaut(),randHaut(),randHaut(),randHaut(), \
  82. randHaut(),randHaut(),randHaut(),randHaut(), \
  83. randHaut(),randHaut(),randHaut(),randHaut(), \
  84. randHaut(),randHaut(),randHaut(),randHaut(), \
  85. im1.size[1]]
  86. # rangement des valeurs des plus petites au plus grandes
  87. randomCoupeHauteur.sort()
  88. seq.randomCoupeHauteur = randomCoupeHauteur
  89. # les hachures
  90. def Hacheur(haut, bas) :
  91. n=0
  92. i=0
  93. #!!!!!!!!!!
  94. while n<im4.size[0] :
  95. if loadfile:
  96. proportions = seq.proportions
  97. choix_rnd = seq.choix[i]
  98. randomCopyPosi = seq.randomCopyPosi[i]
  99. else:
  100. '''
  101. 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168
  102. '''
  103. #proportions = [(160,6),(120,4),(120,8),(80,16),(60,16),(20,32),(8,24),(8,16),(5,64),(3,24),(2,24),(1,256),(1,128),(1,64),(1,32),(1,16),(1,8)]
  104. #proportions = [(2560,1),(1280,2),(1280,1),(640,2),(640,1),(320,4),(320,2),(160,6),(160,2),(120,4),(120,8),(120,1),(80,16),(80,4),(64,16),(64,8),(64,1),(24,32),(24,16),(16,32),(16,16),(16,8),(16,2),(16,1),(8,24),(8,16),(8,8),(8,4),(8,2),(8,1)]
  105. #proportions = [(2560,1),(1280,2),(1280,1),(640,2),(640,1),(320,4),(320,2),(160,6),(160,2),(120,4),(120,8),(120,1),(80,16),(80,4),(64,16),(64,8),(64,1),(32,1),(32,2),(32,4),(32,8),(32,32),(16,1),(16,2),(16,4),(16,16),(16,32)]
  106. proportions = [(2560,1),(1280,2),(1280,1),(640,2),(640,1),(320,4),(320,2),(160,6),(160,2),(120,4),(120,8),(120,1),(80,16),(80,4),(64,16),(64,8),(64,1),(32,1),(32,2),(32,4),(32,8),(32,32),(16,1),(16,2),(16,4),(16,16),(16,32)]
  107. '''
  108. 1,2,4,8,16,32,64,128,256,512,1024
  109. '''
  110. seq.proportions = proportions
  111. #proportions = seq.proportions[]
  112. choix_rnd = random.randint(0, len(proportions)-1)
  113. #choix_rnd = random.randint(0, len(proportions)-1)
  114. seq.choix.append(choix_rnd)
  115. largeur = proportions[choix_rnd][0]
  116. # positionnement de la copie, aleatoirement, entre 0 et la largeur totale de l'image
  117. randomCopyPosi = random.randint(0, (im1.size[0]-largeur))
  118. #randomCopyPosi = seq.randomCopyPosi
  119. seq.randomCopyPosi.append(randomCopyPosi)
  120. i+=1
  121. # tirage au sort
  122. #seq["choix"].append(choix_rnd)
  123. # assignation des valeurs (paires) finales choisies
  124. largeur = proportions[choix_rnd][0]
  125. repeat = proportions[choix_rnd][1]
  126. #seq["randomCopyPosi"].append(randomCopyPosi)
  127. cx1 = randomCopyPosi
  128. cx2 = randomCopyPosi + largeur
  129. # decoupage du sample
  130. im3 = im2.crop((cx1,haut,cx2,bas))
  131. #print "im3 = im2.crop : "+str(im3.size)
  132. draw = ImageDraw.Draw(im4)
  133. loop = 0
  134. #pixelSizeList = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,4,4,8]
  135. pixelSizeList = [1,1,1,1,4,8,16]
  136. #print " pixelSize = "+str(pixelSize)
  137. ''' COLLAGE DU SAMPLE
  138. '''
  139. boutImage = im3
  140. while loop<repeat:
  141. px1 = n
  142. px2 = n + largeur
  143. pixelSizeIndex = random.randint(0,len(pixelSizeList)-1)
  144. pixelSize = pixelSizeList[pixelSizeIndex]
  145. ''' PIXELISATION '''
  146. boutImage = im3.resize((im3.size[0]/pixelSize, im3.size[1]/pixelSize), Image.ANTIALIAS)
  147. boutImage = boutImage.resize((im3.size[0], im3.size[1]), Image.NEAREST)
  148. #lignes colorées de 1 pix
  149. # draw = ImageDraw.Draw(boutImage)
  150. # draw.line((0,0,0,4), fill="rgb(128,128,128)")
  151. # draw.line((0,0,4,0), fill="rgb(128,128,128)")
  152. im4.paste(boutImage, (px1, haut, px2, bas))
  153. pix2 pix4 pix8 pix16 pix32 pix64 pix128 pix256 pix512 pix1024
  154. '''
  155. draw = ImageDraw.Draw(im4)
  156. draw.line((px1-2, haut, px1+2, haut), fill="rgb(0,0,0)")
  157. draw.line((px1, haut-2, px1, haut+2), fill="rgb(0,0,0)")
  158. draw.line((px1-2, bas-1, px1+2, bas-1), fill="rgb(0,0,0)")
  159. draw.line((px1, bas-2, px1, bas+2), fill="rgb(0,0,0)")
  160. '''
  161. n = n + largeur
  162. loop = loop + 1
  163. ''' OPTIONS '''
  164. def TrancheHorizontale() :
  165. # tirage au hasard de la bande copiee
  166. pos = random.randint(0, im1.size[1]-im1.size[1]/20)
  167. # copiage
  168. im5 = im2.crop((0,pos,im1.size[0],pos+im1.size[1]/20))
  169. # le soulignage en blanc
  170. draw = ImageDraw.Draw(im5)
  171. draw.line((0, im5.size[1]-1, im5.size[0], im5.size[1]-1), fill="black")
  172. draw.line((0, 1, im5.size[0], 1), fill="black")
  173. # collage
  174. im4.paste(im5, (0,pos,im1.size[0],pos+im1.size[1]/20))
  175. # HAACHEUUR
  176. for j in range(len(randomCoupeHauteur)-1):
  177. Hacheur(randomCoupeHauteur[j], randomCoupeHauteur[j+1])
  178. ''' SAUVEGARDE '''
  179. # CTRL + S
  180. #chemin du script : scriptpy = sys.argv[0]
  181. #chemin de l'image : str(sys.argv[1])
  182. scriptpy = str(sys.argv[1])
  183. script = scriptpy[:-3]
  184. n = "%05d" % i
  185. im4.save(script+"."+n+"_"+strftime("%Y%m%d-%Hh%Mm%Ss", gmtime())+".png",'PNG', quality=100)
  186. pickle.dump(seq, open(script+"."+n+"_"+strftime("%Y%m%d-%Hh%Mm%Ss", gmtime())+".pickle","w"))
  187. #print script+"."+str(i)+"_"+strftime("%Y%m%d-%Hh%Mm%Ss", gmtime())+".jpg"
  188. #millis = int(round(time.time() * 1000))
  189. #print "millis-secondes : "+str(millis)