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.

hacheuur.old.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. # pln.png 3936x1024 pix
  8. '''
  9. image input puredata : 16384 width x 768 height
  10. donc par exemple, pour 3 images :
  11. 3 x 16384 = 49152 pixels en tout
  12. '''
  13. import sys
  14. import Image
  15. import random
  16. import os
  17. import ImageDraw
  18. import ImageFont
  19. import ImageFilter
  20. from time import gmtime, strftime
  21. import time
  22. if not len(sys.argv) > 1:
  23. raise SystemExit("Usage: %s image_source" % sys.argv[0])
  24. # modifs du 30/10/2013
  25. import ImageEnhance
  26. #ouverture de l'image source et conversion en mode couleur 1bit
  27. im1 = Image.open(str(sys.argv[1])).convert('1')
  28. #im1 = Image.open(str(sys.argv[1])).convert('L')
  29. #im1 = Image.open(str(sys.argv[1]))
  30. im2 = im1.copy()
  31. #rapport d'allongement de la nouvelle image par rapport à la largeur de l'image originale
  32. allongement = 8
  33. im3 = Image.new("RGBA",(im1.size[0], im1.size[1]))
  34. im4 = Image.new("RGBA",(im1.size[0]*allongement, im1.size[1]))
  35. #im4 = Image.new("RGBA",(49152, im1.size[1]))
  36. Larg = im1.size[0]
  37. Haut = im1.size[1]
  38. import pickle
  39. loadfile = False
  40. class Sequence:
  41. def __init__(s):
  42. randomCoupeHauteur = []
  43. s.randomCopyPosi =[]
  44. s.proportions=[]
  45. s.choix=[]
  46. s.sizeOutput=None
  47. s.sizeInput=None
  48. """
  49. seq = dict()
  50. seq["randomCoupeHauteur"] = []
  51. seq["randomCopyPosi"] = []
  52. seq["proportions"] = []
  53. seq["choix"] = []
  54. seq["sizeOutput"]= im4.size
  55. seq["sizeInput"] = im1.size
  56. """
  57. if loadfile:
  58. seq=pickle.load(open("test.pickle"))
  59. else :
  60. seq=Sequence()
  61. for i in range(1):
  62. # constitution de la liste des tranches horizontales
  63. # genre comme si qu'on avait un 16 pistes :)
  64. # nombre aleatoire compris dans les limites de l'image
  65. def randHaut():
  66. return random.randint(0, im1.size[1]/8)*8
  67. if loadfile:
  68. randomCoupeHauteur = seq.randomCoupeHauteur
  69. else:
  70. randomCoupeHauteur = [0, \
  71. randHaut(),randHaut(),randHaut(),randHaut(), \
  72. randHaut(),randHaut(),randHaut(),randHaut(), \
  73. randHaut(),randHaut(),randHaut(),randHaut(), \
  74. randHaut(),randHaut(),randHaut(),randHaut(), \
  75. randHaut(),randHaut(),randHaut(),randHaut(), \
  76. randHaut(),randHaut(),randHaut(),randHaut(), \
  77. randHaut(),randHaut(),randHaut(),randHaut(), \
  78. randHaut(),randHaut(),randHaut(),randHaut(), \
  79. im1.size[1]]
  80. # rangement des valeurs des plus petites au plus grandes
  81. randomCoupeHauteur.sort()
  82. seq.randomCoupeHauteur = randomCoupeHauteur
  83. # les hachures
  84. def Hacheur(haut, bas) :
  85. n=0
  86. i=0
  87. #!!!!!!!!!!
  88. while n<im4.size[0] :
  89. if loadfile:
  90. proportions = seq.proportions
  91. choix_rnd = seq.choix[i]
  92. randomCopyPosi = seq.randomCopyPosi[i]
  93. else:
  94. '''
  95. 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 128 136 144 152 160 168
  96. '''
  97. #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)]
  98. #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)]
  99. #proportions = [(1280,1),(800,1),(800,2),(640,1),(640,2),(640,4),(320,4),(320,2),(320,1),(160,1),(120,2),(120,3),(80,6),(64,12),(64,6),(32,4),(32,8),(32,16),(32,32),(32,64)]
  100. proportions = [(256,1),(160,1),(120,2),(120,3),(80,6),(64,12),(24,18),(8,24),(8,16)]
  101. #proportion pln pour output final avec puredata/gem (decalage phase 32pix)
  102. '''
  103. proportions = [ (768,2),\
  104. (512,4),\
  105. (256,8),\
  106. (128,4),(128,16),\
  107. (64,4),(64,8),(64,16),\
  108. (32,8),(32,16),(32,32),(32,32),(32,64),(32,128),\
  109. (16,1),(16,4),(16,16),(16,64)]
  110. '''
  111. #proportions = [(512,1),(256,2),(128,4),(64,8),(64,4),(32,16),(32,32),(32,8),(16,8),(16,32),(8,64),(8,32),(8,16)]
  112. #proportions = [(32,1),(32,2),(32,3),(32,4),(32,5),(32,6),(32,8),(32,12),(32,16),(32,24),(32,32),(32,64)]
  113. seq.proportions = proportions
  114. #proportions = seq.proportions[]
  115. choix_rnd = random.randint(0, len(proportions)-1)
  116. #choix_rnd = random.randint(0, len(proportions)-1)
  117. seq.choix.append(choix_rnd)
  118. largeur = proportions[choix_rnd][0]
  119. # positionnement de la copie, aleatoirement, entre 0 et la largeur totale de l'image
  120. randomCopyPosi = random.randint(0, (im1.size[0]-largeur))
  121. #randomCopyPosi = seq.randomCopyPosi
  122. seq.randomCopyPosi.append(randomCopyPosi)
  123. i+=1
  124. # tirage au sort
  125. #seq["choix"].append(choix_rnd)
  126. # assignation des valeurs (paires) finales choisies
  127. largeur = proportions[choix_rnd][0]
  128. repeat = proportions[choix_rnd][1]
  129. #seq["randomCopyPosi"].append(randomCopyPosi)
  130. cx1 = randomCopyPosi
  131. cx2 = randomCopyPosi + largeur
  132. # decoupage du sample
  133. im3 = im2.crop((cx1,haut,cx2,bas))
  134. print "im3 = im2.crop : "+str(im3.size)
  135. draw = ImageDraw.Draw(im4)
  136. loop = 0
  137. pixelSizeList = [1,1,1,1,1,1,1,1,4,8]
  138. #pixelSizeList = [1]
  139. pixelSizeIndex = random.randint(0,len(pixelSizeList)-1)
  140. pixelSize = pixelSizeList[pixelSizeIndex]
  141. '''
  142. print " pixelSize = "+str(pixelSize)
  143. '''
  144. ''' COLLAGE DU SAMPLE
  145. '''
  146. while loop<repeat:
  147. px1 = n
  148. px2 = n + largeur
  149. draw = ImageDraw.Draw(im3)
  150. ''' PIXELISATION
  151. '''
  152. im3 = im3.resize((im3.size[0]/pixelSize, im3.size[1]/pixelSize), Image.NEAREST)
  153. im3 = im3.resize((im3.size[0]*pixelSize, im3.size[1]*pixelSize), Image.NEAREST)
  154. '''
  155. print "im3.size="+str(im3.size)
  156. print "im4.size="+str(im4.size)
  157. '''
  158. #lignes colorées de 1 pix
  159. draw.line((im3.size[0]-1, 0, im3.size[0]-1, im3.size[1]-1), fill="rgb(255,255,255)")
  160. im4.paste(im3, (px1, haut, px2, bas))
  161. n = n + largeur
  162. loop = loop + 1
  163. ''' OPTIONS
  164. '''
  165. def TrancheHorizontale() :
  166. # tirage au hasard de la bande copiee
  167. pos = random.randint(0, im1.size[1]-im1.size[1]/20)
  168. # copiage
  169. im5 = im2.crop((0,pos,im1.size[0],pos+im1.size[1]/20))
  170. # le soulignage en blanc
  171. draw = ImageDraw.Draw(im5)
  172. draw.line((0, im5.size[1]-1, im5.size[0], im5.size[1]-1), fill="black")
  173. draw.line((0, 1, im5.size[0], 1), fill="black")
  174. # collage
  175. im4.paste(im5, (0,pos,im1.size[0],pos+im1.size[1]/20))
  176. # HAACHEUUR
  177. for j in range(len(randomCoupeHauteur)-1):
  178. Hacheur(randomCoupeHauteur[j], randomCoupeHauteur[j+1])
  179. ''' SAUVEGARDE
  180. '''
  181. # CTRL + S
  182. #chemin du script : scriptpy = sys.argv[0]
  183. #chemin de l'image : str(sys.argv[1])
  184. scriptpy = str(sys.argv[1])
  185. script = scriptpy[:-3]
  186. n = "%05d" % i
  187. im4.save(script+"."+n+"_"+strftime("%Y%m%d-%Hh%Mm%Ss", gmtime())+".png",'PNG', quality=100)
  188. pickle.dump(seq, open("test.pickle","w"))
  189. #print script+"."+str(i)+"_"+strftime("%Y%m%d-%Hh%Mm%Ss", gmtime())+".jpg"
  190. #millis = int(round(time.time() * 1000))
  191. #print "millis-secondes : "+str(millis)
  192. #obtenir les valeurs RGB des pixels de l'image
  193. '''
  194. from PIL import Image
  195. im = Image.open('test.png')
  196. pixels = list(im.getdata())
  197. width, height = im.size
  198. pixels = [pixels[i * width:(i + 1) * width] for i in xrange(height)]
  199. for r in range(0, height-⁠1) :
  200. print "pixels = "+str(pixels[r])
  201. print " "
  202. print "len(pixels) = "+str(len(pixels))
  203. print "width = "+str(width)
  204. print "height = "+str(height)
  205. print " "
  206. #tintin = im.tostring( "raw", "RGBA", 0, -⁠1)
  207. #print "tostring = "+tintin
  208. '''