images2gif.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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. Many thanks to Ant1 for:
  36. * noting the use of "palette=PIL.Image.ADAPTIVE", which significantly
  37. improves the results.
  38. * the modifications to save each image with its own palette, or optionally
  39. the global palette (if its the same).
  40. Many thanks to Marius van Voorden for porting the NeuQuant quantization
  41. algorithm of Anthony Dekker to Python (See the NeuQuant class for its
  42. license).
  43. Many thanks to Alex Robinson for implementing the concept of subrectangles,
  44. which (depening on image content) can give a very significant reduction in
  45. file size.
  46. This code is based on gifmaker (in the scripts folder of the source
  47. distribution of PIL)
  48. Usefull links:
  49. * http://tronche.com/computer-graphics/gif/
  50. * http://en.wikipedia.org/wiki/Graphics_Interchange_Format
  51. * http://www.w3.org/Graphics/GIF/spec-gif89a.txt
  52. """
  53. # todo: This module should be part of imageio (or at least based on)
  54. import os
  55. import time
  56. try:
  57. import PIL
  58. from PIL import Image
  59. pillow = True
  60. try:
  61. from PIL import PILLOW_VERSION # test if user has Pillow or PIL
  62. except ImportError:
  63. pillow = False
  64. from PIL.GifImagePlugin import getheader, getdata
  65. except ImportError:
  66. PIL = None
  67. try:
  68. import numpy as np
  69. except ImportError:
  70. np = None
  71. def get_cKDTree():
  72. try:
  73. from scipy.spatial import cKDTree
  74. except ImportError:
  75. cKDTree = None
  76. return cKDTree
  77. # getheader gives a 87a header and a color palette (two elements in a list)
  78. # getdata()[0] gives the Image Descriptor up to (including) "LZW min code size"
  79. # getdatas()[1:] is the image data itself in chuncks of 256 bytes (well
  80. # technically the first byte says how many bytes follow, after which that
  81. # amount (max 255) follows)
  82. def checkImages(images):
  83. """ checkImages(images)
  84. Check numpy images and correct intensity range etc.
  85. The same for all movie formats.
  86. :param images:
  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. """Class that contains methods for helping write the animated GIF file.
  128. """
  129. def getheaderAnim(self, im):
  130. """Get animation header. To replace PILs getheader()[0]
  131. :param im:
  132. """
  133. bb = "GIF89a"
  134. bb += intToBin(im.size[0])
  135. bb += intToBin(im.size[1])
  136. bb += "\x87\x00\x00"
  137. return bb
  138. def getImageDescriptor(self, im, xy=None):
  139. """Used for the local color table properties per image.
  140. Otherwise global color table applies to all frames irrespective of
  141. whether additional colors comes in play that require a redefined
  142. palette. Still a maximum of 256 color per frame, obviously.
  143. Written by Ant1 on 2010-08-22
  144. Modified by Alex Robinson in Janurari 2011 to implement subrectangles.
  145. :param im:
  146. :param xy:
  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 min size code now comes later, begining of [image data] blocks
  162. return bb
  163. def getAppExt(self, loops=float('inf')):
  164. """Application extension. This part specifies the amount of loops.
  165. If loops is 0 or inf, it goes on infinitely.
  166. :param float loops:
  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. """Graphics Control Extension. A sort of header at the start of
  183. each image. Specifies duration and transparancy.
  184. Dispose:
  185. * 0 - No disposal specified.
  186. * 1 - Do not dispose. The graphic is to be left in place.
  187. * 2 - Restore to background color. The area used by the graphic
  188. must be restored to the background color.
  189. * 3 - Restore to previous. The decoder is required to restore the
  190. area overwritten by the graphic with what was there prior to
  191. rendering the graphic.
  192. * 4-7 -To be defined.
  193. :param double duration:
  194. :param dispose:
  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. """Handle the sub-rectangle stuff. If the rectangles are given by the
  206. user, the values are checked. Otherwise the subrectangles are
  207. calculated automatically.
  208. """
  209. if isinstance(subRectangles, (tuple, list)):
  210. # xy given directly
  211. # Check xy
  212. xy = subRectangles
  213. if xy is None:
  214. xy = (0, 0)
  215. if hasattr(xy, '__len__'):
  216. if len(xy) == len(images):
  217. xy = [xxyy for xxyy in xy]
  218. else:
  219. raise ValueError("len(xy) doesn't match amount of images.")
  220. else:
  221. xy = [xy for im in images]
  222. xy[0] = (0, 0)
  223. else:
  224. # Calculate xy using some basic image processing
  225. # Check Numpy
  226. if np is None:
  227. raise RuntimeError("Need Numpy to use auto-subRectangles.")
  228. # First make numpy arrays if required
  229. for i in range(len(images)):
  230. im = images[i]
  231. if isinstance(im, Image.Image):
  232. tmp = im.convert() # Make without palette
  233. a = np.asarray(tmp)
  234. if len(a.shape) == 0:
  235. raise MemoryError("Too little memory to convert PIL image to array")
  236. images[i] = a
  237. # Determine the sub rectangles
  238. images, xy = self.getSubRectangles(images)
  239. # Done
  240. return images, xy
  241. def getSubRectangles(self, ims):
  242. """ getSubRectangles(ims)
  243. Calculate the minimal rectangles that need updating each frame.
  244. Returns a two-element tuple containing the cropped images and a
  245. list of x-y positions.
  246. Calculating the subrectangles takes extra time, obviously. However,
  247. if the image sizes were reduced, the actual writing of the GIF
  248. goes faster. In some cases applying this method produces a GIF faster.
  249. """
  250. # Check image count
  251. if len(ims) < 2:
  252. return ims, [(0, 0) for i in ims]
  253. # We need numpy
  254. if np is None:
  255. raise RuntimeError("Need Numpy to calculate sub-rectangles. ")
  256. # Prepare
  257. ims2 = [ims[0]]
  258. xy = [(0, 0)]
  259. t0 = time.time()
  260. # Iterate over images
  261. prev = ims[0]
  262. for im in ims[1:]:
  263. # Get difference, sum over colors
  264. diff = np.abs(im-prev)
  265. if diff.ndim == 3:
  266. diff = diff.sum(2)
  267. # Get begin and end for both dimensions
  268. X = np.argwhere(diff.sum(0))
  269. Y = np.argwhere(diff.sum(1))
  270. # Get rect coordinates
  271. if X.size and Y.size:
  272. x0, x1 = X[0], X[-1] + 1
  273. y0, y1 = Y[0], Y[-1] + 1
  274. else: # No change ... make it minimal
  275. x0, x1 = 0, 2
  276. y0, y1 = 0, 2
  277. # Cut out and store
  278. im2 = im[y0:y1, x0:x1]
  279. prev = im
  280. ims2.append(im2)
  281. xy.append((x0, y0))
  282. # Done
  283. # print('%1.2f seconds to determine subrectangles of %i images' %
  284. # (time.time()-t0, len(ims2)))
  285. return ims2, xy
  286. def convertImagesToPIL(self, images, dither, nq=0):
  287. """ convertImagesToPIL(images, nq=0)
  288. Convert images to Paletted PIL images, which can then be
  289. written to a single animaged GIF.
  290. """
  291. # Convert to PIL images
  292. images2 = []
  293. for im in images:
  294. if isinstance(im, Image.Image):
  295. images2.append(im)
  296. elif np and isinstance(im, np.ndarray):
  297. if im.ndim == 3 and im.shape[2] == 3:
  298. im = Image.fromarray(im, 'RGB')
  299. elif im.ndim == 3 and im.shape[2] == 4:
  300. im = Image.fromarray(im[:, :, :3], 'RGB')
  301. elif im.ndim == 2:
  302. im = Image.fromarray(im, 'L')
  303. images2.append(im)
  304. # Convert to paletted PIL images
  305. images, images2 = images2, []
  306. if nq >= 1:
  307. # NeuQuant algorithm
  308. for im in images:
  309. im = im.convert("RGBA") # NQ assumes RGBA
  310. nqInstance = NeuQuant(im, int(nq)) # Learn colors from image
  311. if dither:
  312. im = im.convert("RGB").quantize(palette=nqInstance.paletteImage())
  313. else:
  314. # Use to quantize the image itself
  315. im = nqInstance.quantize(im)
  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. def writeGif(filename, images, duration=0.1, repeat=True, dither=False,
  389. nq=0, subRectangles=True, dispose=None):
  390. """Write an animated gif from the specified images.
  391. :param str filename: the name of the file to write the image to.
  392. :param list images: should be a list consisting of PIL images or numpy
  393. arrays. The latter should be between 0 and 255 for
  394. integer types, and between 0 and 1 for float types.
  395. :param duration: scalar or list of scalars The duration for all frames, or
  396. (if a list) for each frame.
  397. :param repeat: bool or integer The amount of loops. If True, loops infinitetely.
  398. :param bool dither: whether to apply dithering
  399. :param int nq: If nonzero, applies the NeuQuant quantization algorithm to
  400. create the color palette. This algorithm is superior, but
  401. slower than the standard PIL algorithm. The value of nq is
  402. the quality parameter. 1 represents the best quality. 10 is
  403. in general a good tradeoff between quality and speed. When
  404. using this option, better results are usually obtained when
  405. subRectangles is False.
  406. :param subRectangles: False, True, or a list of 2-element tuples
  407. Whether to use sub-rectangles. If True, the minimal
  408. rectangle that is required to update each frame is
  409. automatically detected. This can give significant
  410. reductions in file size, particularly if only a part
  411. of the image changes. One can also give a list of x-y
  412. coordinates if you want to do the cropping yourself.
  413. The default is True.
  414. :param int dispose: how to dispose each frame. 1 means that each frame is
  415. to be left in place. 2 means the background color
  416. should be restored after each frame. 3 means the
  417. decoder should restore the previous frame. If
  418. subRectangles==False, the default is 2, otherwise it is 1.
  419. """
  420. # Check PIL
  421. if PIL is None:
  422. raise RuntimeError("Need PIL to write animated gif files.")
  423. # Check images
  424. images = checkImages(images)
  425. # Instantiate writer object
  426. gifWriter = GifWriter()
  427. # Check loops
  428. if repeat is False:
  429. loops = 1
  430. elif repeat is True:
  431. loops = 0 # zero means infinite
  432. else:
  433. loops = int(repeat)
  434. # Check duration
  435. if hasattr(duration, '__len__'):
  436. if len(duration) == len(images):
  437. duration = [d for d in duration]
  438. else:
  439. raise ValueError("len(duration) doesn't match amount of images.")
  440. else:
  441. duration = [duration for im in images]
  442. # Check subrectangles
  443. if subRectangles:
  444. images, xy = gifWriter.handleSubRectangles(images, subRectangles)
  445. defaultDispose = 1 # Leave image in place
  446. else:
  447. # Normal mode
  448. xy = [(0, 0) for im in images]
  449. defaultDispose = 2 # Restore to background color.
  450. # Check dispose
  451. if dispose is None:
  452. dispose = defaultDispose
  453. if hasattr(dispose, '__len__'):
  454. if len(dispose) != len(images):
  455. raise ValueError("len(xy) doesn't match amount of images.")
  456. else:
  457. dispose = [dispose for im in images]
  458. # Make images in a format that we can write easy
  459. images = gifWriter.convertImagesToPIL(images, dither, nq)
  460. # Write
  461. fp = open(filename, 'wb')
  462. try:
  463. gifWriter.writeGifToFile(fp, images, duration, loops, xy, dispose)
  464. finally:
  465. fp.close()
  466. def readGif(filename, asNumpy=True):
  467. """Read images from an animated GIF file. Returns a list of numpy
  468. arrays, or, if asNumpy is false, a list if PIL images.
  469. """
  470. # Check PIL
  471. if PIL is None:
  472. raise RuntimeError("Need PIL to read animated gif files.")
  473. # Check Numpy
  474. if np is None:
  475. raise RuntimeError("Need Numpy to read animated gif files.")
  476. # Check whether it exists
  477. if not os.path.isfile(filename):
  478. raise IOError('File not found: ' + str(filename))
  479. # Load file using PIL
  480. pilIm = PIL.Image.open(filename)
  481. pilIm.seek(0)
  482. # Read all images inside
  483. images = []
  484. try:
  485. while True:
  486. # Get image as numpy array
  487. tmp = pilIm.convert() # Make without palette
  488. a = np.asarray(tmp)
  489. if len(a.shape) == 0:
  490. raise MemoryError("Too little memory to convert PIL image to array")
  491. # Store, and next
  492. images.append(a)
  493. pilIm.seek(pilIm.tell() + 1)
  494. except EOFError:
  495. pass
  496. # Convert to normal PIL images if needed
  497. if not asNumpy:
  498. images2 = images
  499. images = []
  500. for im in images2:
  501. images.append(PIL.Image.fromarray(im))
  502. # Done
  503. return images
  504. class NeuQuant:
  505. """ NeuQuant(image, samplefac=10, colors=256)
  506. samplefac should be an integer number of 1 or higher, 1
  507. being the highest quality, but the slowest performance.
  508. With avalue of 10, one tenth of all pixels are used during
  509. training. This value seems a nice tradeof between speed
  510. and quality.
  511. colors is the amount of colors to reduce the image to. This
  512. should best be a power of two.
  513. See also:
  514. http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
  515. **License of the NeuQuant Neural-Net Quantization Algorithm**
  516. Copyright (c) 1994 Anthony Dekker
  517. Ported to python by Marius van Voorden in 2010
  518. NEUQUANT Neural-Net quantization algorithm by Anthony Dekker, 1994.
  519. See "Kohonen neural networks for optimal colour quantization"
  520. in "network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367.
  521. for a discussion of the algorithm.
  522. See also http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
  523. Any party obtaining a copy of these files from the author, directly or
  524. indirectly, is granted, free of charge, a full and unrestricted
  525. irrevocable, world-wide, paid up, royalty-free, nonexclusive right and
  526. license to deal in this software and documentation files (the "Software"),
  527. including without limitation the rights to use, copy, modify, merge,
  528. publish, distribute, sublicense, and/or sell copies of the Software, and
  529. to permit persons who receive copies from any such party to do so, with
  530. the only requirement being that this copyright notice remain intact.
  531. """
  532. NCYCLES = None # Number of learning cycles
  533. NETSIZE = None # Number of colours used
  534. SPECIALS = None # Number of reserved colours used
  535. BGCOLOR = None # Reserved background colour
  536. CUTNETSIZE = None
  537. MAXNETPOS = None
  538. INITRAD = None # For 256 colours, radius starts at 32
  539. RADIUSBIASSHIFT = None
  540. RADIUSBIAS = None
  541. INITBIASRADIUS = None
  542. RADIUSDEC = None # Factor of 1/30 each cycle
  543. ALPHABIASSHIFT = None
  544. INITALPHA = None # biased by 10 bits
  545. GAMMA = None
  546. BETA = None
  547. BETAGAMMA = None
  548. network = None # The network itself
  549. colormap = None # The network itself
  550. netindex = None # For network lookup - really 256
  551. bias = None # Bias and freq arrays for learning
  552. freq = None
  553. pimage = None
  554. # Four primes near 500 - assume no image has a length so large
  555. # that it is divisible by all four primes
  556. PRIME1 = 499
  557. PRIME2 = 491
  558. PRIME3 = 487
  559. PRIME4 = 503
  560. MAXPRIME = PRIME4
  561. pixels = None
  562. samplefac = None
  563. a_s = None
  564. def setconstants(self, samplefac, colors):
  565. self.NCYCLES = 100 # Number of learning cycles
  566. self.NETSIZE = colors # Number of colours used
  567. self.SPECIALS = 3 # Number of reserved colours used
  568. self.BGCOLOR = self.SPECIALS-1 # Reserved background colour
  569. self.CUTNETSIZE = self.NETSIZE - self.SPECIALS
  570. self.MAXNETPOS = self.NETSIZE - 1
  571. self.INITRAD = self.NETSIZE/8 # For 256 colours, radius starts at 32
  572. self.RADIUSBIASSHIFT = 6
  573. self.RADIUSBIAS = 1 << self.RADIUSBIASSHIFT
  574. self.INITBIASRADIUS = self.INITRAD * self.RADIUSBIAS
  575. self.RADIUSDEC = 30 # Factor of 1/30 each cycle
  576. self.ALPHABIASSHIFT = 10 # Alpha starts at 1
  577. self.INITALPHA = 1 << self.ALPHABIASSHIFT # biased by 10 bits
  578. self.GAMMA = 1024.0
  579. self.BETA = 1.0/1024.0
  580. self.BETAGAMMA = self.BETA * self.GAMMA
  581. self.network = np.empty((self.NETSIZE, 3), dtype='float64') # The network itself
  582. self.colormap = np.empty((self.NETSIZE, 4), dtype='int32') # The network itself
  583. self.netindex = np.empty(256, dtype='int32') # For network lookup - really 256
  584. self.bias = np.empty(self.NETSIZE, dtype='float64') # Bias and freq arrays for learning
  585. self.freq = np.empty(self.NETSIZE, dtype='float64')
  586. self.pixels = None
  587. self.samplefac = samplefac
  588. self.a_s = {}
  589. def __init__(self, image, samplefac=10, colors=256):
  590. # Check Numpy
  591. if np is None:
  592. raise RuntimeError("Need Numpy for the NeuQuant algorithm.")
  593. # Check image
  594. if image.size[0] * image.size[1] < NeuQuant.MAXPRIME:
  595. raise IOError("Image is too small")
  596. if image.mode != "RGBA":
  597. raise IOError("Image mode should be RGBA.")
  598. # Initialize
  599. self.setconstants(samplefac, colors)
  600. self.pixels = np.fromstring(image.tostring(), np.uint32)
  601. self.setUpArrays()
  602. self.learn()
  603. self.fix()
  604. self.inxbuild()
  605. def writeColourMap(self, rgb, outstream):
  606. for i in range(self.NETSIZE):
  607. bb = self.colormap[i, 0]
  608. gg = self.colormap[i, 1]
  609. rr = self.colormap[i, 2]
  610. outstream.write(rr if rgb else bb)
  611. outstream.write(gg)
  612. outstream.write(bb if rgb else rr)
  613. return self.NETSIZE
  614. def setUpArrays(self):
  615. self.network[0, 0] = 0.0 # Black
  616. self.network[0, 1] = 0.0
  617. self.network[0, 2] = 0.0
  618. self.network[1, 0] = 255.0 # White
  619. self.network[1, 1] = 255.0
  620. self.network[1, 2] = 255.0
  621. # RESERVED self.BGCOLOR # Background
  622. for i in range(self.SPECIALS):
  623. self.freq[i] = 1.0 / self.NETSIZE
  624. self.bias[i] = 0.0
  625. for i in range(self.SPECIALS, self.NETSIZE):
  626. p = self.network[i]
  627. p[:] = (255.0 * (i-self.SPECIALS)) / self.CUTNETSIZE
  628. self.freq[i] = 1.0 / self.NETSIZE
  629. self.bias[i] = 0.0
  630. # Omitted: setPixels
  631. def altersingle(self, alpha, i, b, g, r):
  632. """Move neuron i towards biased (b, g, r) by factor alpha"""
  633. n = self.network[i] # Alter hit neuron
  634. n[0] -= (alpha * (n[0] - b))
  635. n[1] -= (alpha * (n[1] - g))
  636. n[2] -= (alpha * (n[2] - r))
  637. def geta(self, alpha, rad):
  638. try:
  639. return self.a_s[(alpha, rad)]
  640. except KeyError:
  641. length = rad * 2-1
  642. mid = length/2
  643. q = np.array(list(range(mid-1, -1, -1)) + list(range(-1, mid)))
  644. a = alpha * (rad * rad - q * q)/(rad * rad)
  645. a[mid] = 0
  646. self.a_s[(alpha, rad)] = a
  647. return a
  648. def alterneigh(self, alpha, rad, i, b, g, r):
  649. if i-rad >= self.SPECIALS-1:
  650. lo = i-rad
  651. start = 0
  652. else:
  653. lo = self.SPECIALS-1
  654. start = (self.SPECIALS-1 - (i-rad))
  655. if i + rad <= self.NETSIZE:
  656. hi = i + rad
  657. end = rad * 2-1
  658. else:
  659. hi = self.NETSIZE
  660. end = (self.NETSIZE - (i + rad))
  661. a = self.geta(alpha, rad)[start:end]
  662. p = self.network[lo + 1:hi]
  663. p -= np.transpose(np.transpose(p - np.array([b, g, r])) * a)
  664. #def contest(self, b, g, r):
  665. # """ Search for biased BGR values
  666. # Finds closest neuron (min dist) and updates self.freq
  667. # finds best neuron (min dist-self.bias) and returns position
  668. # for frequently chosen neurons, self.freq[i] is high and self.bias[i] is negative
  669. # self.bias[i] = self.GAMMA * ((1/self.NETSIZE)-self.freq[i])"""
  670. #
  671. # i, j = self.SPECIALS, self.NETSIZE
  672. # dists = abs(self.network[i:j] - np.array([b, g, r])).sum(1)
  673. # bestpos = i + np.argmin(dists)
  674. # biasdists = dists - self.bias[i:j]
  675. # bestbiaspos = i + np.argmin(biasdists)
  676. # self.freq[i:j] -= self.BETA * self.freq[i:j]
  677. # self.bias[i:j] += self.BETAGAMMA * self.freq[i:j]
  678. # self.freq[bestpos] += self.BETA
  679. # self.bias[bestpos] -= self.BETAGAMMA
  680. # return bestbiaspos
  681. def contest(self, b, g, r):
  682. """Search for biased BGR values
  683. Finds closest neuron (min dist) and updates self.freq
  684. finds best neuron (min dist-self.bias) and returns position
  685. for frequently chosen neurons, self.freq[i] is high and self.bias[i]
  686. is negative self.bias[i] = self.GAMMA * ((1/self.NETSIZE)-self.freq[i])
  687. """
  688. i, j = self.SPECIALS, self.NETSIZE
  689. dists = abs(self.network[i:j] - np.array([b, g, r])).sum(1)
  690. bestpos = i + np.argmin(dists)
  691. biasdists = dists - self.bias[i:j]
  692. bestbiaspos = i + np.argmin(biasdists)
  693. self.freq[i:j] *= (1-self.BETA)
  694. self.bias[i:j] += self.BETAGAMMA * self.freq[i:j]
  695. self.freq[bestpos] += self.BETA
  696. self.bias[bestpos] -= self.BETAGAMMA
  697. return bestbiaspos
  698. def specialFind(self, b, g, r):
  699. for i in range(self.SPECIALS):
  700. n = self.network[i]
  701. if n[0] == b and n[1] == g and n[2] == r:
  702. return i
  703. return -1
  704. def learn(self):
  705. biasRadius = self.INITBIASRADIUS
  706. alphadec = 30 + ((self.samplefac-1)/3)
  707. lengthcount = self.pixels.size
  708. samplepixels = lengthcount / self.samplefac
  709. delta = samplepixels / self.NCYCLES
  710. alpha = self.INITALPHA
  711. i = 0
  712. rad = biasRadius >> self.RADIUSBIASSHIFT
  713. if rad <= 1:
  714. rad = 0
  715. print("Beginning 1D learning: samplepixels = %1.2f rad = %i" %
  716. (samplepixels, rad))
  717. step = 0
  718. pos = 0
  719. if lengthcount % NeuQuant.PRIME1 != 0:
  720. step = NeuQuant.PRIME1
  721. elif lengthcount % NeuQuant.PRIME2 != 0:
  722. step = NeuQuant.PRIME2
  723. elif lengthcount % NeuQuant.PRIME3 != 0:
  724. step = NeuQuant.PRIME3
  725. else:
  726. step = NeuQuant.PRIME4
  727. i = 0
  728. printed_string = ''
  729. while i < samplepixels:
  730. if i % 100 == 99:
  731. tmp = '\b' * len(printed_string)
  732. printed_string = str((i + 1) * 100/samplepixels) + "%\n"
  733. print(tmp + printed_string)
  734. p = self.pixels[pos]
  735. r = (p >> 16) & 0xff
  736. g = (p >> 8) & 0xff
  737. b = (p) & 0xff
  738. if i == 0: # Remember background colour
  739. self.network[self.BGCOLOR] = [b, g, r]
  740. j = self.specialFind(b, g, r)
  741. if j < 0:
  742. j = self.contest(b, g, r)
  743. if j >= self.SPECIALS: # Don't learn for specials
  744. a = (1.0 * alpha) / self.INITALPHA
  745. self.altersingle(a, j, b, g, r)
  746. if rad > 0:
  747. self.alterneigh(a, rad, j, b, g, r)
  748. pos = (pos + step) % lengthcount
  749. i += 1
  750. if i % delta == 0:
  751. alpha -= alpha / alphadec
  752. biasRadius -= biasRadius / self.RADIUSDEC
  753. rad = biasRadius >> self.RADIUSBIASSHIFT
  754. if rad <= 1:
  755. rad = 0
  756. finalAlpha = (1.0 * alpha)/self.INITALPHA
  757. print("Finished 1D learning: final alpha = %1.2f!" % finalAlpha)
  758. def fix(self):
  759. for i in range(self.NETSIZE):
  760. for j in range(3):
  761. x = int(0.5 + self.network[i, j])
  762. x = max(0, x)
  763. x = min(255, x)
  764. self.colormap[i, j] = x
  765. self.colormap[i, 3] = i
  766. def inxbuild(self):
  767. previouscol = 0
  768. startpos = 0
  769. for i in range(self.NETSIZE):
  770. p = self.colormap[i]
  771. q = None
  772. smallpos = i
  773. smallval = p[1] # Index on g
  774. # Find smallest in i..self.NETSIZE-1
  775. for j in range(i + 1, self.NETSIZE):
  776. q = self.colormap[j]
  777. if q[1] < smallval: # Index on g
  778. smallpos = j
  779. smallval = q[1] # Index on g
  780. q = self.colormap[smallpos]
  781. # Swap p (i) and q (smallpos) entries
  782. if i != smallpos:
  783. p[:], q[:] = q, p.copy()
  784. # smallval entry is now in position i
  785. if smallval != previouscol:
  786. self.netindex[previouscol] = (startpos + i) >> 1
  787. for j in range(previouscol + 1, smallval):
  788. self.netindex[j] = i
  789. previouscol = smallval
  790. startpos = i
  791. self.netindex[previouscol] = (startpos + self.MAXNETPOS) >> 1
  792. for j in range(previouscol + 1, 256): # Really 256
  793. self.netindex[j] = self.MAXNETPOS
  794. def paletteImage(self):
  795. """PIL weird interface for making a paletted image: create an image
  796. which already has the palette, and use that in Image.quantize. This
  797. function returns this palette image."""
  798. if self.pimage is None:
  799. palette = []
  800. for i in range(self.NETSIZE):
  801. palette.extend(self.colormap[i][:3])
  802. palette.extend([0] * (256-self.NETSIZE) * 3)
  803. # a palette image to use for quant
  804. self.pimage = Image.new("P", (1, 1), 0)
  805. self.pimage.putpalette(palette)
  806. return self.pimage
  807. def quantize(self, image):
  808. """Use a kdtree to quickly find the closest palette colors for the
  809. pixels
  810. :param image:
  811. """
  812. if get_cKDTree():
  813. return self.quantize_with_scipy(image)
  814. else:
  815. print('Scipy not available, falling back to slower version.')
  816. return self.quantize_without_scipy(image)
  817. def quantize_with_scipy(self, image):
  818. w, h = image.size
  819. px = np.asarray(image).copy()
  820. px2 = px[:, :, :3].reshape((w * h, 3))
  821. cKDTree = get_cKDTree()
  822. kdtree = cKDTree(self.colormap[:, :3], leafsize=10)
  823. result = kdtree.query(px2)
  824. colorindex = result[1]
  825. print("Distance: %1.2f" % (result[0].sum()/(w * h)))
  826. px2[:] = self.colormap[colorindex, :3]
  827. return Image.fromarray(px).convert("RGB").quantize(palette=self.paletteImage())
  828. def quantize_without_scipy(self, image):
  829. """" This function can be used if no scipy is availabe.
  830. It's 7 times slower though.
  831. :param image:
  832. """
  833. w, h = image.size
  834. px = np.asarray(image).copy()
  835. memo = {}
  836. for j in range(w):
  837. for i in range(h):
  838. key = (px[i, j, 0], px[i, j, 1], px[i, j, 2])
  839. try:
  840. val = memo[key]
  841. except KeyError:
  842. val = self.convert(*key)
  843. memo[key] = val
  844. px[i, j, 0], px[i, j, 1], px[i, j, 2] = val
  845. return Image.fromarray(px).convert("RGB").quantize(palette=self.paletteImage())
  846. def convert(self, *color):
  847. i = self.inxsearch(*color)
  848. return self.colormap[i, :3]
  849. def inxsearch(self, r, g, b):
  850. """Search for BGR values 0..255 and return colour index"""
  851. dists = (self.colormap[:, :3] - np.array([r, g, b]))
  852. a = np.argmin((dists * dists).sum(1))
  853. return a
  854. if __name__ == '__main__':
  855. im = np.zeros((200, 200), dtype=np.uint8)
  856. im[10: 30, :] = 100
  857. im[:, 80: 120] = 255
  858. im[-50: -40, :] = 50
  859. images = [im * 1.0, im * 0.8, im * 0.6, im * 0.4, im * 0]
  860. writeGif('lala3.gif', images, duration=0.5, dither=0)