Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # -------------------------------------------------------------------------------------
- #
- # Copyright (c) 2017, Dmitry Odintsov
- # This code is licensed under the MIT license (MIT)
- # (http://opensource.org/licenses/MIT)
- #
- # 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.
- #
- # -------------------------------------------------------------------------------------
- #
- # This is Python plug-in for GIMP.
- #
- # It can be executed by selecting the menu option: 'Filters/Aninimouse/Downscaling'
- # or by writing the following lines in the Python console (that can be opened with the
- # menu option 'Filters/Python-Fu/Console'):
- # >>> image = gimp.image_list()[0]
- # >>> gimp.pdb.python_fu_aniniscript(image, None, 8, 8)
- from gimpfu import *
- import operator
- def aniniscript(img, drawable, x_factor = 8, y_factor = 8) :
- ''' Merges all visible layers and resizes the image by given factors.
- Each pixel in the final image is colored with the dominant color in the according
- [x_factor:y_factor] block of the old image.
- Parameters:
- img : the current image
- drawable : (unused)
- x_factor : X resize factor
- y_factor : Y resize factor
- '''
- # If we have nothing to do or resulting width/height will be 0, abandon the task
- if (x_factor == 1 and y_factor == 1) or (img.width / x_factor == 0 or img.height / y_factor == 0):
- return
- # Indicates that the process has started.
- gimp.progress_init("Aniniscaling the image...")
- # Set up an undo group, so the operation will be undone in one step.
- pdb.gimp_image_undo_group_start(img)
- # Merge visible layers of the image
- layer = img.merge_visible_layers(CLIP_TO_IMAGE)
- bpp = layer.bpp
- layerName = layer.name
- # Create a new layer to save the results (otherwise is not possible to undo the operation).
- newLayer = gimp.Layer(img,
- "Aniniscaled " + layerName,
- layer.width / x_factor,
- layer.height / y_factor,
- layer.type,
- layer.opacity,
- layer.mode)
- img.add_layer(newLayer, 0)
- # Clear the new layer
- pdb.gimp_edit_clear(newLayer)
- newLayer.flush()
- try:
- dstImage = newLayer.get_pixel_rgn(0, 0, newLayer.width, newLayer.height)
- # Calculate the number of tiles
- tileX = int(layer.width / x_factor)
- tileY = int(layer.height / y_factor)
- # Iterate over the tiles
- for i in range(tileX):
- for j in range(tileY):
- # Update the progress bar
- gimp.progress_update(float(i * tileY + j) / float(tileX * tileY))
- # Get the tile
- srcTile = layer.get_pixel_rgn(i * x_factor, j * y_factor,
- x_factor, y_factor,
- False, False)
- # Iterate over the pixels inside the tile
- blockColors = {}
- for x in range(x_factor):
- for y in range(y_factor):
- # Get pixel color and count it towards block color
- color = srcTile[i * x_factor + x, j * y_factor + y]
- if color not in blockColors:
- blockColors[color] = 0
- blockColors[color] += 1
- # Get dominant color and paint in resulting image
- color = max(blockColors.iteritems(), key=operator.itemgetter(1))[0]
- dstImage[i, j] = color
- # Update the new layer
- newLayer.flush()
- newLayer.merge_shadow(True)
- newLayer.update(0, 0, newLayer.width, newLayer.height)
- # Remove the old layer
- img.remove_layer(layer)
- newLayer.name = layerName
- # Resize the image to new size
- img.resize(newLayer.width, newLayer.height, 0, 0)
- except Exception as err:
- gimp.message("Unexpected error: " + str(err))
- # Close the undo group
- pdb.gimp_image_undo_group_end(img)
- # End progress
- pdb.gimp_progress_end()
- register(
- "aniniscript",
- "Aninimouse downscaling",
- "Resizes the image by taking dominant colors in [X factor : Y factor] blocks",
- "Dmitry Odintsov",
- "MIT",
- "2017",
- "<Image>/Filters/Aninimouse/Downscaling",
- "RGB, RGB*",
- [
- (PF_INT, "x_factor", "X factor", 8),
- (PF_INT, "y_factor", "Y factor", 8)
- ],
- [],
- aniniscript)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement