images2gif.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2012, Almar Klein, Ant1, Marius van Voorden
  3. #
  4. # This code is subject to the (new) BSD license:
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of the <organization> nor the
  14. # names of its contributors may be used to endorse or promote products
  15. # derived from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. # ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. """ Module images2gif
  28. Provides functionality for reading and writing animated GIF images.
  29. Use writeGif to write a series of numpy arrays or PIL images as an
  30. animated GIF. Use readGif to read an animated gif as a series of numpy
  31. arrays.
  32. Note that since July 2004, all patents on the LZW compression patent have
  33. expired. Therefore the GIF format may now be used freely.
  34. Acknowledgements
  35. ----------------
  36. Many thanks to Ant1 for:
  37. * noting the use of "palette=PIL.Image.ADAPTIVE", which significantly
  38. improves the results.
  39. * the modifications to save each image with its own palette, or optionally
  40. the global palette (if its the same).
  41. Many thanks to Marius van Voorden for porting the NeuQuant quantization
  42. algorithm of Anthony Dekker to Python (See the NeuQuant class for its
  43. license).
  44. Many thanks to Alex Robinson for implementing the concept of subrectangles,
  45. which (depening on image content) can give a very significant reduction in
  46. file size.
  47. This code is based on gifmaker (in the scripts folder of the source
  48. distribution of PIL)
  49. Usefull links
  50. -------------
  51. * http://tronche.com/computer-graphics/gif/
  52. * http://en.wikipedia.org/wiki/Graphics_Interchange_Format
  53. * http://www.w3.org/Graphics/GIF/spec-gif89a.txt
  54. """
  55. # todo: This module should be part of imageio (or at least based on)
  56. import os, time
  57. try:
  58. import PIL
  59. from PIL import Image
  60. pillow = True
  61. try:
  62. from PIL import PILLOW_VERSION # test if user has Pillow or PIL
  63. except ImportError:
  64. pillow = False
  65. from PIL.GifImagePlugin import getheader, getdata
  66. except ImportError:
  67. PIL = None
  68. try:
  69. import numpy as np
  70. except ImportError:
  71. np = None
  72. def get_cKDTree():
  73. try:
  74. from scipy.spatial import cKDTree
  75. except ImportError:
  76. cKDTree = None
  77. return cKDTree
  78. # getheader gives a 87a header and a color palette (two elements in a list).
  79. # getdata()[0] gives the Image Descriptor up to (including) "LZW min code size".
  80. # getdatas()[1:] is the image data itself in chuncks of 256 bytes (well
  81. # technically the first byte says how many bytes follow, after which that
  82. # amount (max 255) follows).
  83. def checkImages(images):
  84. """ checkImages(images)
  85. Check numpy images and correct intensity range etc.
  86. The same for all movie formats.
  87. """
  88. # Init results
  89. images2 = []
  90. for im in images:
  91. if PIL and isinstance(im, PIL.Image.Image):
  92. # We assume PIL images are allright
  93. images2.append(im)
  94. elif np and isinstance(im, np.ndarray):
  95. # Check and convert dtype
  96. if im.dtype == np.uint8:
  97. images2.append(im) # Ok
  98. elif im.dtype in [np.float32, np.float64]:
  99. im = im.copy()
  100. im[im<0] = 0
  101. im[im>1] = 1
  102. im *= 255
  103. images2.append( im.astype(np.uint8) )
  104. else:
  105. im = im.astype(np.uint8)
  106. images2.append(im)
  107. # Check size
  108. if im.ndim == 2:
  109. pass # ok
  110. elif im.ndim == 3:
  111. if im.shape[2] not in [3,4]:
  112. raise ValueError('This array can not represent an image.')
  113. else:
  114. raise ValueError('This array can not represent an image.')
  115. else:
  116. raise ValueError('Invalid image type: ' + str(type(im)))
  117. # Done
  118. return images2
  119. def intToBin(i):
  120. """ Integer to two bytes """
  121. # devide in two parts (bytes)
  122. i1 = i % 256
  123. i2 = int( i/256)
  124. # make string (little endian)
  125. return chr(i1) + chr(i2)
  126. class GifWriter:
  127. """ GifWriter()
  128. Class that contains methods for helping write the animated GIF file.
  129. """
  130. def getheaderAnim(self, im):
  131. """ getheaderAnim(im)
  132. Get animation header. To replace PILs getheader()[0]
  133. """
  134. bb = "GIF89a"
  135. bb += intToBin(im.size[0])
  136. bb += intToBin(im.size[1])
  137. bb += "\x87\x00\x00"
  138. return bb
  139. def getImageDescriptor(self, im, xy=None):
  140. """ getImageDescriptor(im, xy=None)
  141. Used for the local color table properties per image.
  142. Otherwise global color table applies to all frames irrespective of
  143. whether additional colors comes in play that require a redefined
  144. palette. Still a maximum of 256 color per frame, obviously.
  145. Written by Ant1 on 2010-08-22
  146. Modified by Alex Robinson in Janurari 2011 to implement subrectangles.
  147. """
  148. # Defaule use full image and place at upper left
  149. if xy is None:
  150. xy = (0,0)
  151. # Image separator,
  152. bb = '\x2C'
  153. # Image position and size
  154. bb += intToBin( xy[0] ) # Left position
  155. bb += intToBin( xy[1] ) # Top position
  156. bb += intToBin( im.size[0] ) # image width
  157. bb += intToBin( im.size[1] ) # image height
  158. # packed field: local color table flag1, interlace0, sorted table0,
  159. # reserved00, lct size111=7=2^(7+1)=256.
  160. bb += '\x87'
  161. # LZW minimum size code now comes later, begining of [image data] blocks
  162. return bb
  163. def getAppExt(self, loops=float('inf')):
  164. """ getAppExt(loops=float('inf'))
  165. Application extention. This part specifies the amount of loops.
  166. If loops is 0 or inf, it goes on infinitely.
  167. """
  168. if loops==0 or loops==float('inf'):
  169. loops = 2**16-1
  170. #bb = "" # application extension should not be used
  171. # (the extension interprets zero loops
  172. # to mean an infinite number of loops)
  173. # Mmm, does not seem to work
  174. if True:
  175. bb = "\x21\xFF\x0B" # application extension
  176. bb += "NETSCAPE2.0"
  177. bb += "\x03\x01"
  178. bb += intToBin(loops)
  179. bb += '\x00' # end
  180. return bb
  181. def getGraphicsControlExt(self, duration=0.1, dispose=2):
  182. """ getGraphicsControlExt(duration=0.1, dispose=2)
  183. Graphics Control Extension. A sort of header at the start of
  184. each image. Specifies duration and transparancy.
  185. Dispose
  186. -------
  187. * 0 - No disposal specified.
  188. * 1 - Do not dispose. The graphic is to be left in place.
  189. * 2 - Restore to background color. The area used by the graphic
  190. must be restored to the background color.
  191. * 3 - Restore to previous. The decoder is required to restore the
  192. area overwritten by the graphic with what was there prior to
  193. rendering the graphic.
  194. * 4-7 -To be defined.
  195. """
  196. bb = '\x21\xF9\x04'
  197. bb += chr((dispose & 3) << 2) # low bit 1 == transparency,
  198. # 2nd bit 1 == user input , next 3 bits, the low two of which are used,
  199. # are dispose.
  200. bb += intToBin( int(duration*100) ) # in 100th of seconds
  201. bb += '\x00' # no transparant color
  202. bb += '\x00' # end
  203. return bb
  204. def handleSubRectangles(self, images, subRectangles):
  205. """ handleSubRectangles(images)
  206. Handle the sub-rectangle stuff. If the rectangles are given by the
  207. user, the values are checked. Otherwise the subrectangles are
  208. calculated automatically.
  209. """
  210. if isinstance(subRectangles, (tuple,list)):
  211. # xy given directly
  212. # Check xy
  213. xy = subRectangles
  214. if xy is None:
  215. xy = (0,0)
  216. if hasattr(xy, '__len__'):
  217. if len(xy) == len(images):
  218. xy = [xxyy for xxyy in xy]
  219. else:
  220. raise ValueError("len(xy) doesn't match amount of images.")
  221. else:
  222. xy = [xy for im in images]
  223. xy[0] = (0,0)
  224. else:
  225. # Calculate xy using some basic image processing
  226. # Check Numpy
  227. if np is None:
  228. raise RuntimeError("Need Numpy to use auto-subRectangles.")
  229. # First make numpy arrays if required
  230. for i in range(len(images)):
  231. im = images[i]
  232. if isinstance(im, Image.Image):
  233. tmp = im.convert() # Make without palette
  234. a = np.asarray(tmp)
  235. if len(a.shape)==0:
  236. raise MemoryError("Too little memory to convert PIL image to array")
  237. images[i] = a
  238. # Determine the sub rectangles
  239. images, xy = self.getSubRectangles(images)
  240. # Done
  241. return images, xy
  242. def getSubRectangles(self, ims):
  243. """ getSubRectangles(ims)
  244. Calculate the minimal rectangles that need updating each frame.
  245. Returns a two-element tuple containing the cropped images and a
  246. list of x-y positions.
  247. Calculating the subrectangles takes extra time, obviously. However,
  248. if the image sizes were reduced, the actual writing of the GIF
  249. goes faster. In some cases applying this method produces a GIF faster.
  250. """
  251. # Check image count
  252. if len(ims) < 2:
  253. return ims, [(0,0) for i in ims]
  254. # We need numpy
  255. if np is None:
  256. raise RuntimeError("Need Numpy to calculate sub-rectangles. ")
  257. # Prepare
  258. ims2 = [ims[0]]
  259. xy = [(0,0)]
  260. t0 = time.time()
  261. # Iterate over images
  262. prev = ims[0]
  263. for im in ims[1:]:
  264. # Get difference, sum over colors
  265. diff = np.abs(im-prev)
  266. if diff.ndim==3:
  267. diff = diff.sum(2)
  268. # Get begin and end for both dimensions
  269. X = np.argwhere(diff.sum(0))
  270. Y = np.argwhere(diff.sum(1))
  271. # Get rect coordinates
  272. if X.size and Y.size:
  273. x0, x1 = X[0], X[-1]+1
  274. y0, y1 = Y[0], Y[-1]+1
  275. else: # No change ... make it minimal
  276. x0, x1 = 0, 2
  277. y0, y1 = 0, 2
  278. # Cut out and store
  279. im2 = im[y0:y1,x0:x1]
  280. prev = im
  281. ims2.append(im2)
  282. xy.append((x0,y0))
  283. # Done
  284. #print('%1.2f seconds to determine subrectangles of %i images' %
  285. # (time.time()-t0, len(ims2)) )
  286. return ims2, xy
  287. def convertImagesToPIL(self, images, dither, nq=0):
  288. """ convertImagesToPIL(images, nq=0)
  289. Convert images to Paletted PIL images, which can then be
  290. written to a single animaged GIF.
  291. """
  292. # Convert to PIL images
  293. images2 = []
  294. for im in images:
  295. if isinstance(im, Image.Image):
  296. images2.append(im)
  297. elif np and isinstance(im, np.ndarray):
  298. if im.ndim==3 and im.shape[2]==3:
  299. im = Image.fromarray(im,'RGB')
  300. elif im.ndim==3 and im.shape[2]==4:
  301. im = Image.fromarray(im[:,:,:3],'RGB')
  302. elif im.ndim==2:
  303. im = Image.fromarray(im,'L')
  304. images2.append(im)
  305. # Convert to paletted PIL images
  306. images, images2 = images2, []
  307. if nq >= 1:
  308. # NeuQuant algorithm
  309. for im in images:
  310. im = im.convert("RGBA") # NQ assumes RGBA
  311. nqInstance = NeuQuant(im, int(nq)) # Learn colors from image
  312. if dither:
  313. im = im.convert("RGB").quantize(palette=nqInstance.paletteImage())
  314. else:
  315. im = nqInstance.quantize(im) # Use to quantize the image itself
  316. images2.append(im)
  317. else:
  318. # Adaptive PIL algorithm
  319. AD = Image.ADAPTIVE
  320. for im in images:
  321. im = im.convert('P', palette=AD, dither=dither)
  322. images2.append(im)
  323. # Done
  324. return images2
  325. def writeGifToFile(self, fp, images, durations, loops, xys, disposes):
  326. """ writeGifToFile(fp, images, durations, loops, xys, disposes)
  327. Given a set of images writes the bytes to the specified stream.
  328. Requires different handling of palette for PIL and Pillow:
  329. based on https://github.com/rec/echomesh/blob/master/
  330. code/python/external/images2gif.py
  331. """
  332. # Obtain palette for all images and count each occurance
  333. palettes, occur = [], []
  334. for im in images:
  335. if not pillow:
  336. palette = getheader(im)[1]
  337. else:
  338. palette = getheader(im)[0][-1]
  339. if not palette:
  340. palette = im.palette.tobytes()
  341. palettes.append(palette)
  342. for palette in palettes:
  343. occur.append( palettes.count( palette ) )
  344. # Select most-used palette as the global one (or first in case no max)
  345. globalPalette = palettes[ occur.index(max(occur)) ]
  346. # Init
  347. frames = 0
  348. firstFrame = True
  349. for im, palette in zip(images, palettes):
  350. if firstFrame:
  351. # Write header
  352. # Gather info
  353. header = self.getheaderAnim(im)
  354. appext = self.getAppExt(loops)
  355. # Write
  356. fp.write(header)
  357. fp.write(globalPalette)
  358. fp.write(appext)
  359. # Next frame is not the first
  360. firstFrame = False
  361. if True:
  362. # Write palette and image data
  363. # Gather info
  364. data = getdata(im)
  365. imdes, data = data[0], data[1:]
  366. graphext = self.getGraphicsControlExt(durations[frames],
  367. disposes[frames])
  368. # Make image descriptor suitable for using 256 local color palette
  369. lid = self.getImageDescriptor(im, xys[frames])
  370. # Write local header
  371. if (palette != globalPalette) or (disposes[frames] != 2):
  372. # Use local color palette
  373. fp.write(graphext)
  374. fp.write(lid) # write suitable image descriptor
  375. fp.write(palette) # write local color table
  376. fp.write('\x08') # LZW minimum size code
  377. else:
  378. # Use global color palette
  379. fp.write(graphext)
  380. fp.write(imdes) # write suitable image descriptor
  381. # Write image data
  382. for d in data:
  383. fp.write(d)
  384. # Prepare for next round
  385. frames = frames + 1
  386. fp.write(";") # end gif
  387. return frames
  388. ## Exposed functions
  389. def writeGif(filename, images, duration=0.1, repeat=True, dither=False,
  390. nq=0, subRectangles=True, dispose=None):
  391. """ writeGif(filename, images, duration=0.1, repeat=True, dither=False,
  392. nq=0, subRectangles=True, dispose=None)
  393. Write an animated gif from the specified images.
  394. Parameters
  395. ----------
  396. filename : string
  397. The name of the file to write the image to.
  398. images : list
  399. Should be a list consisting of PIL images or numpy arrays.
  400. The latter should be between 0 and 255 for integer types, and
  401. between 0 and 1 for float types.
  402. duration : scalar or list of scalars
  403. The duration for all frames, or (if a list) for each frame.
  404. repeat : bool or integer
  405. The amount of loops. If True, loops infinitetely.
  406. dither : bool
  407. Whether to apply dithering
  408. nq : integer
  409. If nonzero, applies the NeuQuant quantization algorithm to create
  410. the color palette. This algorithm is superior, but slower than
  411. the standard PIL algorithm. The value of nq is the quality
  412. parameter. 1 represents the best quality. 10 is in general a
  413. good tradeoff between quality and speed. When using this option,
  414. better results are usually obtained when subRectangles is False.
  415. subRectangles : False, True, or a list of 2-element tuples
  416. Whether to use sub-rectangles. If True, the minimal rectangle that
  417. is required to update each frame is automatically detected. This
  418. can give significant reductions in file size, particularly if only
  419. a part of the image changes. One can also give a list of x-y
  420. coordinates if you want to do the cropping yourself. The default
  421. is True.
  422. dispose : int
  423. How to dispose each frame. 1 means that each frame is to be left
  424. in place. 2 means the background color should be restored after
  425. each frame. 3 means the decoder should restore the previous frame.
  426. If subRectangles==False, the default is 2, otherwise it is 1.
  427. """
  428. # Check PIL
  429. if PIL is None:
  430. raise RuntimeError("Need PIL to write animated gif files.")
  431. # Check images
  432. images = checkImages(images)
  433. # Instantiate writer object
  434. gifWriter = GifWriter()
  435. # Check loops
  436. if repeat is False:
  437. loops = 1
  438. elif repeat is True:
  439. loops = 0 # zero means infinite
  440. else:
  441. loops = int(repeat)
  442. # Check duration
  443. if hasattr(duration, '__len__'):
  444. if len(duration) == len(images):
  445. duration = [d for d in duration]
  446. else:
  447. raise ValueError("len(duration) doesn't match amount of images.")
  448. else:
  449. duration = [duration for im in images]
  450. # Check subrectangles
  451. if subRectangles:
  452. images, xy = gifWriter.handleSubRectangles(images, subRectangles)
  453. defaultDispose = 1 # Leave image in place
  454. else:
  455. # Normal mode
  456. xy = [(0,0) for im in images]
  457. defaultDispose = 2 # Restore to background color.
  458. # Check dispose
  459. if dispose is None:
  460. dispose = defaultDispose
  461. if hasattr(dispose, '__len__'):
  462. if len(dispose) != len(images):
  463. raise ValueError("len(xy) doesn't match amount of images.")
  464. else:
  465. dispose = [dispose for im in images]
  466. # Make images in a format that we can write easy
  467. images = gifWriter.convertImagesToPIL(images, dither, nq)
  468. # Write
  469. fp = open(filename, 'wb')
  470. try:
  471. gifWriter.writeGifToFile(fp, images, duration, loops, xy, dispose)
  472. finally:
  473. fp.close()
  474. def readGif(filename, asNumpy=True):
  475. """ readGif(filename, asNumpy=True)
  476. Read images from an animated GIF file. Returns a list of numpy
  477. arrays, or, if asNumpy is false, a list if PIL images.
  478. """
  479. # Check PIL
  480. if PIL is None:
  481. raise RuntimeError("Need PIL to read animated gif files.")
  482. # Check Numpy
  483. if np is None:
  484. raise RuntimeError("Need Numpy to read animated gif files.")
  485. # Check whether it exists
  486. if not os.path.isfile(filename):
  487. raise IOError('File not found: '+str(filename))
  488. # Load file using PIL
  489. pilIm = PIL.Image.open(filename)
  490. pilIm.seek(0)
  491. # Read all images inside
  492. images = []
  493. try:
  494. while True:
  495. # Get image as numpy array
  496. tmp = pilIm.convert() # Make without palette
  497. a = np.asarray(tmp)
  498. if len(a.shape)==0:
  499. raise MemoryError("Too little memory to convert PIL image to array")
  500. # Store, and next
  501. images.append(a)
  502. pilIm.seek(pilIm.tell()+1)
  503. except EOFError:
  504. pass
  505. # Convert to normal PIL images if needed
  506. if not asNumpy:
  507. images2 = images
  508. images = []
  509. for im in images2:
  510. images.append( PIL.Image.fromarray(im) )
  511. # Done
  512. return images
  513. class NeuQuant:
  514. """ NeuQuant(image, samplefac=10, colors=256)
  515. samplefac should be an integer number of 1 or higher, 1
  516. being the highest quality, but the slowest performance.
  517. With avalue of 10, one tenth of all pixels are used during
  518. training. This value seems a nice tradeof between speed
  519. and quality.
  520. colors is the amount of colors to reduce the image to. This
  521. should best be a power of two.
  522. See also:
  523. http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
  524. License of the NeuQuant Neural-Net Quantization Algorithm
  525. ---------------------------------------------------------
  526. Copyright (c) 1994 Anthony Dekker
  527. Ported to python by Marius van Voorden in 2010
  528. NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994.
  529. See "Kohonen neural networks for optimal colour quantization"
  530. in "network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367.
  531. for a discussion of the algorithm.
  532. See also http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
  533. Any party obtaining a copy of these files from the author, directly or
  534. indirectly, is granted, free of charge, a full and unrestricted irrevocable,
  535. world-wide, paid up, royalty-free, nonexclusive right and license to deal
  536. in this software and documentation files (the "Software"), including without
  537. limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  538. and/or sell copies of the Software, and to permit persons who receive
  539. copies from any such party to do so, with the only requirement being
  540. that this copyright notice remain intact.
  541. """
  542. NCYCLES = None # Number of learning cycles
  543. NETSIZE = None # Number of colours used
  544. SPECIALS = None # Number of reserved colours used
  545. BGCOLOR = None # Reserved background colour
  546. CUTNETSIZE = None
  547. MAXNETPOS = None
  548. INITRAD = None # For 256 colours, radius starts at 32
  549. RADIUSBIASSHIFT = None
  550. RADIUSBIAS = None
  551. INITBIASRADIUS = None
  552. RADIUSDEC = None # Factor of 1/30 each cycle
  553. ALPHABIASSHIFT = None
  554. INITALPHA = None # biased by 10 bits
  555. GAMMA = None
  556. BETA = None
  557. BETAGAMMA = None
  558. network = None # The network itself
  559. colormap = None # The network itself
  560. netindex = None # For network lookup - really 256
  561. bias = None # Bias and freq arrays for learning
  562. freq = None
  563. pimage = None
  564. # Four primes near 500 - assume no image has a length so large
  565. # that it is divisible by all four primes
  566. PRIME1 = 499
  567. PRIME2 = 491
  568. PRIME3 = 487
  569. PRIME4 = 503
  570. MAXPRIME = PRIME4
  571. pixels = None
  572. samplefac = None
  573. a_s = None
  574. def setconstants(self, samplefac, colors):
  575. self.NCYCLES = 100 # Number of learning cycles
  576. self.NETSIZE = colors # Number of colours used
  577. self.SPECIALS = 3 # Number of reserved colours used
  578. self.BGCOLOR = self.SPECIALS-1 # Reserved background colour
  579. self.CUTNETSIZE = self.NETSIZE - self.SPECIALS
  580. self.MAXNETPOS = self.NETSIZE - 1
  581. self.INITRAD = self.NETSIZE/8 # For 256 colours, radius starts at 32
  582. self.RADIUSBIASSHIFT = 6
  583. self.RADIUSBIAS = 1 << self.RADIUSBIASSHIFT
  584. self.INITBIASRADIUS = self.INITRAD * self.RADIUSBIAS
  585. self.RADIUSDEC = 30 # Factor of 1/30 each cycle
  586. self.ALPHABIASSHIFT = 10 # Alpha starts at 1
  587. self.INITALPHA = 1 << self.ALPHABIASSHIFT # biased by 10 bits
  588. self.GAMMA = 1024.0
  589. self.BETA = 1.0/1024.0
  590. self.BETAGAMMA = self.BETA * self.GAMMA
  591. self.network = np.empty((self.NETSIZE, 3), dtype='float64') # The network itself
  592. self.colormap = np.empty((self.NETSIZE, 4), dtype='int32') # The network itself
  593. self.netindex = np.empty(256, dtype='int32') # For network lookup - really 256
  594. self.bias = np.empty(self.NETSIZE, dtype='float64') # Bias and freq arrays for learning
  595. self.freq = np.empty(self.NETSIZE, dtype='float64')
  596. self.pixels = None
  597. self.samplefac = samplefac
  598. self.a_s = {}
  599. def __init__(self, image, samplefac=10, colors=256):
  600. # Check Numpy
  601. if np is None:
  602. raise RuntimeError("Need Numpy for the NeuQuant algorithm.")
  603. # Check image
  604. if image.size[0] * image.size[1] < NeuQuant.MAXPRIME:
  605. raise IOError("Image is too small")
  606. if image.mode != "RGBA":
  607. raise IOError("Image mode should be RGBA.")
  608. # Initialize
  609. self.setconstants(samplefac, colors)
  610. self.pixels = np.fromstring(image.tostring(), np.uint32)
  611. self.setUpArrays()
  612. self.learn()
  613. self.fix()
  614. self.inxbuild()
  615. def writeColourMap(self, rgb, outstream):
  616. for i in range(self.NETSIZE):
  617. bb = self.colormap[i,0];
  618. gg = self.colormap[i,1];
  619. rr = self.colormap[i,2];
  620. outstream.write(rr if rgb else bb)
  621. outstream.write(gg)
  622. outstream.write(bb if rgb else rr)
  623. return self.NETSIZE
  624. def setUpArrays(self):
  625. self.network[0,0] = 0.0 # Black
  626. self.network[0,1] = 0.0
  627. self.network[0,2] = 0.0
  628. self.network[1,0] = 255.0 # White
  629. self.network[1,1] = 255.0
  630. self.network[1,2] = 255.0
  631. # RESERVED self.BGCOLOR # Background
  632. for i in range(self.SPECIALS):
  633. self.freq[i] = 1.0 / self.NETSIZE
  634. self.bias[i] = 0.0
  635. for i in range(self.SPECIALS, self.NETSIZE):
  636. p = self.network[i]
  637. p[:] = (255.0 * (i-self.SPECIALS)) / self.CUTNETSIZE
  638. self.freq[i] = 1.0 / self.NETSIZE
  639. self.bias[i] = 0.0
  640. # Omitted: setPixels
  641. def altersingle(self, alpha, i, b, g, r):
  642. """Move neuron i towards biased (b,g,r) by factor alpha"""
  643. n = self.network[i] # Alter hit neuron
  644. n[0] -= (alpha*(n[0] - b))
  645. n[1] -= (alpha*(n[1] - g))
  646. n[2] -= (alpha*(n[2] - r))
  647. def geta(self, alpha, rad):
  648. try:
  649. return self.a_s[(alpha, rad)]
  650. except KeyError:
  651. length = rad*2-1
  652. mid = length/2
  653. q = np.array(list(range(mid-1,-1,-1))+list(range(-1,mid)))
  654. a = alpha*(rad*rad - q*q)/(rad*rad)
  655. a[mid] = 0
  656. self.a_s[(alpha, rad)] = a
  657. return a
  658. def alterneigh(self, alpha, rad, i, b, g, r):
  659. if i-rad >= self.SPECIALS-1:
  660. lo = i-rad
  661. start = 0
  662. else:
  663. lo = self.SPECIALS-1
  664. start = (self.SPECIALS-1 - (i-rad))
  665. if i+rad <= self.NETSIZE:
  666. hi = i+rad
  667. end = rad*2-1
  668. else:
  669. hi = self.NETSIZE
  670. end = (self.NETSIZE - (i+rad))
  671. a = self.geta(alpha, rad)[start:end]
  672. p = self.network[lo+1:hi]
  673. p -= np.transpose(np.transpose(p - np.array([b, g, r])) * a)
  674. #def contest(self, b, g, r):
  675. # """ Search for biased BGR values
  676. # Finds closest neuron (min dist) and updates self.freq
  677. # finds best neuron (min dist-self.bias) and returns position
  678. # for frequently chosen neurons, self.freq[i] is high and self.bias[i] is negative
  679. # self.bias[i] = self.GAMMA*((1/self.NETSIZE)-self.freq[i])"""
  680. #
  681. # i, j = self.SPECIALS, self.NETSIZE
  682. # dists = abs(self.network[i:j] - np.array([b,g,r])).sum(1)
  683. # bestpos = i + np.argmin(dists)
  684. # biasdists = dists - self.bias[i:j]
  685. # bestbiaspos = i + np.argmin(biasdists)
  686. # self.freq[i:j] -= self.BETA * self.freq[i:j]
  687. # self.bias[i:j] += self.BETAGAMMA * self.freq[i:j]
  688. # self.freq[bestpos] += self.BETA
  689. # self.bias[bestpos] -= self.BETAGAMMA
  690. # return bestbiaspos
  691. def contest(self, b, g, r):
  692. """ Search for biased BGR values
  693. Finds closest neuron (min dist) and updates self.freq
  694. finds best neuron (min dist-self.bias) and returns position
  695. for frequently chosen neurons, self.freq[i] is high and self.bias[i] is negative
  696. self.bias[i] = self.GAMMA*((1/self.NETSIZE)-self.freq[i])"""
  697. i, j = self.SPECIALS, self.NETSIZE
  698. dists = abs(self.network[i:j] - np.array([b,g,r])).sum(1)
  699. bestpos = i + np.argmin(dists)
  700. biasdists = dists - self.bias[i:j]
  701. bestbiaspos = i + np.argmin(biasdists)
  702. self.freq[i:j] *= (1-self.BETA)
  703. self.bias[i:j] += self.BETAGAMMA * self.freq[i:j]
  704. self.freq[bestpos] += self.BETA
  705. self.bias[bestpos] -= self.BETAGAMMA
  706. return bestbiaspos
  707. def specialFind(self, b, g, r):
  708. for i in range(self.SPECIALS):
  709. n = self.network[i]
  710. if n[0] == b and n[1] == g and n[2] == r:
  711. return i
  712. return -1
  713. def learn(self):
  714. biasRadius = self.INITBIASRADIUS
  715. alphadec = 30 + ((self.samplefac-1)/3)
  716. lengthcount = self.pixels.size
  717. samplepixels = lengthcount / self.samplefac
  718. delta = samplepixels / self.NCYCLES
  719. alpha = self.INITALPHA
  720. i = 0;
  721. rad = biasRadius >> self.RADIUSBIASSHIFT
  722. if rad <= 1:
  723. rad = 0
  724. print("Beginning 1D learning: samplepixels = %1.2f rad = %i" %
  725. (samplepixels, rad) )
  726. step = 0
  727. pos = 0
  728. if lengthcount%NeuQuant.PRIME1 != 0:
  729. step = NeuQuant.PRIME1
  730. elif lengthcount%NeuQuant.PRIME2 != 0:
  731. step = NeuQuant.PRIME2
  732. elif lengthcount%NeuQuant.PRIME3 != 0:
  733. step = NeuQuant.PRIME3
  734. else:
  735. step = NeuQuant.PRIME4
  736. i = 0
  737. printed_string = ''
  738. while i < samplepixels:
  739. if i%100 == 99:
  740. tmp = '\b'*len(printed_string)
  741. printed_string = str((i+1)*100/samplepixels)+"%\n"
  742. print(tmp + printed_string)
  743. p = self.pixels[pos]
  744. r = (p >> 16) & 0xff
  745. g = (p >> 8) & 0xff
  746. b = (p ) & 0xff
  747. if i == 0: # Remember background colour
  748. self.network[self.BGCOLOR] = [b, g, r]
  749. j = self.specialFind(b, g, r)
  750. if j < 0:
  751. j = self.contest(b, g, r)
  752. if j >= self.SPECIALS: # Don't learn for specials
  753. a = (1.0 * alpha) / self.INITALPHA
  754. self.altersingle(a, j, b, g, r)
  755. if rad > 0:
  756. self.alterneigh(a, rad, j, b, g, r)
  757. pos = (pos+step)%lengthcount
  758. i += 1
  759. if i%delta == 0:
  760. alpha -= alpha / alphadec
  761. biasRadius -= biasRadius / self.RADIUSDEC
  762. rad = biasRadius >> self.RADIUSBIASSHIFT
  763. if rad <= 1:
  764. rad = 0
  765. finalAlpha = (1.0*alpha)/self.INITALPHA
  766. print("Finished 1D learning: final alpha = %1.2f!" % finalAlpha)
  767. def fix(self):
  768. for i in range(self.NETSIZE):
  769. for j in range(3):
  770. x = int(0.5 + self.network[i,j])
  771. x = max(0, x)
  772. x = min(255, x)
  773. self.colormap[i,j] = x
  774. self.colormap[i,3] = i
  775. def inxbuild(self):
  776. previouscol = 0
  777. startpos = 0
  778. for i in range(self.NETSIZE):
  779. p = self.colormap[i]
  780. q = None
  781. smallpos = i
  782. smallval = p[1] # Index on g
  783. # Find smallest in i..self.NETSIZE-1
  784. for j in range(i+1, self.NETSIZE):
  785. q = self.colormap[j]
  786. if q[1] < smallval: # Index on g
  787. smallpos = j
  788. smallval = q[1] # Index on g
  789. q = self.colormap[smallpos]
  790. # Swap p (i) and q (smallpos) entries
  791. if i != smallpos:
  792. p[:],q[:] = q, p.copy()
  793. # smallval entry is now in position i
  794. if smallval != previouscol:
  795. self.netindex[previouscol] = (startpos+i) >> 1
  796. for j in range(previouscol+1, smallval):
  797. self.netindex[j] = i
  798. previouscol = smallval
  799. startpos = i
  800. self.netindex[previouscol] = (startpos+self.MAXNETPOS) >> 1
  801. for j in range(previouscol+1, 256): # Really 256
  802. self.netindex[j] = self.MAXNETPOS
  803. def paletteImage(self):
  804. """ PIL weird interface for making a paletted image: create an image which
  805. already has the palette, and use that in Image.quantize. This function
  806. returns this palette image. """
  807. if self.pimage is None:
  808. palette = []
  809. for i in range(self.NETSIZE):
  810. palette.extend(self.colormap[i][:3])
  811. palette.extend([0]*(256-self.NETSIZE)*3)
  812. # a palette image to use for quant
  813. self.pimage = Image.new("P", (1, 1), 0)
  814. self.pimage.putpalette(palette)
  815. return self.pimage
  816. def quantize(self, image):
  817. """ Use a kdtree to quickly find the closest palette colors for the pixels """
  818. if get_cKDTree():
  819. return self.quantize_with_scipy(image)
  820. else:
  821. print('Scipy not available, falling back to slower version.')
  822. return self.quantize_without_scipy(image)
  823. def quantize_with_scipy(self, image):
  824. w,h = image.size
  825. px = np.asarray(image).copy()
  826. px2 = px[:,:,:3].reshape((w*h,3))
  827. cKDTree = get_cKDTree()
  828. kdtree = cKDTree(self.colormap[:,:3],leafsize=10)
  829. result = kdtree.query(px2)
  830. colorindex = result[1]
  831. print("Distance: %1.2f" % (result[0].sum()/(w*h)) )
  832. px2[:] = self.colormap[colorindex,:3]
  833. return Image.fromarray(px).convert("RGB").quantize(palette=self.paletteImage())
  834. def quantize_without_scipy(self, image):
  835. """" This function can be used if no scipy is availabe.
  836. It's 7 times slower though.
  837. """
  838. w,h = image.size
  839. px = np.asarray(image).copy()
  840. memo = {}
  841. for j in range(w):
  842. for i in range(h):
  843. key = (px[i,j,0],px[i,j,1],px[i,j,2])
  844. try:
  845. val = memo[key]
  846. except KeyError:
  847. val = self.convert(*key)
  848. memo[key] = val
  849. px[i,j,0],px[i,j,1],px[i,j,2] = val
  850. return Image.fromarray(px).convert("RGB").quantize(palette=self.paletteImage())
  851. def convert(self, *color):
  852. i = self.inxsearch(*color)
  853. return self.colormap[i,:3]
  854. def inxsearch(self, r, g, b):
  855. """Search for BGR values 0..255 and return colour index"""
  856. dists = (self.colormap[:,:3] - np.array([r,g,b]))
  857. a= np.argmin((dists*dists).sum(1))
  858. return a
  859. if __name__ == '__main__':
  860. im = np.zeros((200,200), dtype=np.uint8)
  861. im[10:30,:] = 100
  862. im[:,80:120] = 255
  863. im[-50:-40,:] = 50
  864. images = [im*1.0, im*0.8, im*0.6, im*0.4, im*0]
  865. writeGif('lala3.gif',images, duration=0.5, dither=0)