Skip to content

Instantly share code, notes, and snippets.

@unau
Created August 5, 2015 15:41
Show Gist options
  • Save unau/54e70df512354c468851 to your computer and use it in GitHub Desktop.
Save unau/54e70df512354c468851 to your computer and use it in GitHub Desktop.
python-fu : GIMP プラグイン : facebook の個人のページで、一体化して見えるプロフィール写真とカバー写真を作るあれ
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gimpfu import *
class Rect:
def buildWithSelectionBounds(self, bounds):
self.nw = { "x": bounds[1], "y": bounds[2] }
self.se = { "x": bounds[3], "y": bounds[4] }
self.size = { "x": bounds[3] - bounds[1], "y": bounds[4] - bounds[2] }
return self
def buildWithNWPointAndSize(self):
self.ne = { "x": self.nw["x"] + self.size["x"], "y": self.nw["y"] }
self.se = { "x": self.ne["x"], "y": self.ne["y"] + self.size["y"] }
self.sw = { "x": self.nw["x"], "y": self.se["y"] }
return self
def extendToSquare(self):
diff = self.size["x"] - self.size["y"]
if diff > 0:
self.size["y"] = self.size["x"]
elif diff < 0:
self.size["x"] = self.size["y"]
return self.buildWithNWPointAndSize()
def toControlPoints(self):
return (
self.nw["x"], self.nw["y"], self.nw["x"], self.nw["y"], self.nw["x"], self.nw["y"],
self.ne["x"], self.ne["y"], self.ne["x"], self.ne["y"], self.ne["x"], self.ne["y"],
self.se["x"], self.se["y"], self.se["x"], self.se["y"], self.se["x"], self.se["y"],
self.sw["x"], self.sw["y"], self.sw["x"], self.sw["y"], self.sw["x"], self.sw["y"]
)
class FBPPP:
def __init__(self, image, layer, prefix):
self.image = image
self.layer = layer
self.prefix = prefix
def getSelection(self):
pdb.gimp_selection_sharpen(self.image)
bounds = pdb.gimp_selection_bounds(self.image)
non_empty = bounds[0]
if not non_empty: return None
self.org = Rect().buildWithSelectionBounds(bounds).extendToSquare()
self.ratio = self.org.size["x"] / 160.0
return self.org
def selectWithRect(self, rect):
v12 = pdb.gimp_vectors_new(self.image, "v12")
pdb.gimp_image_add_vectors(self.image, v12, -1)
points = rect.toControlPoints()
stroke12 = pdb.gimp_vectors_stroke_new_from_points(v12, 0, len(points), points, TRUE)
pdb.gimp_vectors_to_selection(v12, CHANNEL_OP_REPLACE, 0, 0, 0, 0)
return self
def copy(self):
return pdb.gimp_edit_copy_visible(self.image)
def genProfileImage(self):
self.profileImage = pdb.gimp_edit_paste_as_new()
pdb.gimp_display_new(self.profileImage)
pdb.gimp_image_set_filename(self.profileImage, "%s_profile"%self.prefix)
def genCoverImage(self):
self.coverImage = pdb.gimp_edit_paste_as_new()
pdb.gimp_display_new(self.coverImage)
pdb.gimp_image_set_filename(self.coverImage, "%s_cover"%self.prefix)
def aaa(self):
self.covRect = Rect()
nw = self.org.nw
self.covRect.nw = { "x": nw["x"] - 16.0 * self.ratio,
"y": nw["y"] - 176.0 * self.ratio }
self.covRect.size = { "x": 851.0 * self.ratio,
"y": 315.0 * self.ratio }
self.covRect.buildWithNWPointAndSize()
return self.covRect
def save(self):
pdb.file_png_save_defaults(self.profileImage, self.profileImage.active_layer, self.profileImage.name + '.png', self.profileImage.name + '.png')
pdb.file_png_save_defaults(self.coverImage, self.coverImage.active_layer, self.coverImage.name + '.png', self.coverImage.name + '.png')
def facebook_ppp4pc(image, layer, prefix):
ppp = FBPPP(image, layer, prefix)
org = ppp.getSelection()
if org is None: return
ppp.selectWithRect(org)
if not ppp.copy(): return
ppp.genProfileImage()
cov = ppp.aaa()
ppp.selectWithRect(cov)
if not ppp.copy(): return
ppp.genCoverImage()
ppp.save()
ppp.selectWithRect(org)
register(
"python_fu_facebook_ppp4pc",
"facebook personal page's photo for pc",
"facebook の個人のページで、一体化して見えるプロフィール写真とカバー写真を作るあれ",
"Takeyuki Kojima",
"Takeyuki Koima",
"2015/08/15",
"<Image>/Image/Create facebook personal page photos",
"*", # imagetypes: "RGB*, GRAY*" など
[
(PF_STRING, "prefix", "生成するファイルのプレフィックス", "unknown_w")
],
[], # 戻り値
facebook_ppp4pc) # 関数名
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment