SHARE
TWEET

image 1bit grayscale YIQ luma

ze7g0ne Oct 14th, 2019 (edited) 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2.  Copyright (c) <2019> <ze7g0ne@cock.li>
  3.  
  4.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  5.  
  6.  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7.  
  8.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  9. '''
  10.  
  11. # Install Pillow: pip install Pillow
  12. # Tested on Python 3.7.2
  13.  
  14. from PIL import Image
  15.  
  16. def yiq(r, g, b, coeff):
  17.     return 0 if (r * 299 + g * 587 + b * 114) / 1000 < coeff else 255
  18.  
  19. def img_1bit_grayscale_yiq_luma(inp_n, coeff):
  20.     img = Image.open(inp_n).convert('RGB')
  21.     pixels = img.load()
  22.  
  23.     for y in range(img.height):
  24.         for x in range(img.width):
  25.             pixels[x, y] = (yiq(*pixels[x, y], coeff),) * 3
  26.  
  27.     img.convert('1').save(inp_n + '.result.png')
  28.  
  29. img_1bit_grayscale_yiq_luma('test.png', 176)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top