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!
- Copyright (c) <2019> <email@example.com>
- 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:
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- 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.
- # Install Pillow: pip install Pillow
- # Tested on Python 3.7.2
- from PIL import Image
- def yiq(r, g, b, coeff):
- return 0 if (r * 299 + g * 587 + b * 114) / 1000 < coeff else 255
- def img_1bit_grayscale_yiq_luma(inp_n, coeff):
- img = Image.open(inp_n).convert('RGB')
- pixels = img.load()
- for y in range(img.height):
- for x in range(img.width):
- pixels[x, y] = (yiq(*pixels[x, y], coeff),) * 3
- img.convert('1').save(inp_n + '.result.png')
- img_1bit_grayscale_yiq_luma('test.png', 176)
RAW Paste Data