Compare two schemes Python [image] filleted

Demand: the existing 200 * 200 pixels JPG images, to be made into a 129 * 129 pixel PNG rounded head.

Two programs.

 

Scenario 1: JPG picture directly, using the grayscale mask, superimposed been rounded PNG.

 

Advantages: convenient, without hands-P transparent template base map.

Cons: rounded serrated very clear, high-definition map can not be used, as compared with the use of PS rounded clipping masks made, sawtooth obvious.

FIG Effect: left to PS results (129 * 129 radius of a rounded rectangle as the mask 10), the right is the result code execution.

Seen, PS while processing fillet, increased transition effect, and Python program is relatively simple, direct people in a zigzag shown. Therefore, directly on the original rounded corner cutting hard ineffective codes (or require further optimization in code).

Python code looks hard cut fillet, renderings and explained in my another blog :

# 使用 PIL 将图象处理为圆角矩形
# Success
 
from PIL import Image, ImageDraw
 
radii=10
img = Image.open('flag.jpg')	
 
# 矩形图像转为圆角矩形
def circle_corner(img, radii):
	# 画圆(用于分离4个角)
	circle = Image.new('L', (radii * 2, radii * 2), 0)  # 创建黑色方形
	# circle.save('1.jpg','JPEG',qulity=100)
	draw = ImageDraw.Draw(circle)
	draw.ellipse((0, 0, radii * 2, radii * 2), fill=255)  # 黑色方形内切白色圆形
	# circle.save('2.jpg','JPEG',qulity=100)
 
	# 原图转为带有alpha通道(表示透明程度)
	img = img.convert("RGBA")
	w, h = img.size
 
	# 画4个角(将整圆分离为4个部分)
	alpha = Image.new('L', img.size, 255)	#与img同大小的白色矩形,L 表示黑白图
	# alpha.save('3.jpg','JPEG',qulity=100)
	alpha.paste(circle.crop((0, 0, radii, radii)), (0, 0))  # 左上角
	alpha.paste(circle.crop((radii, 0, radii * 2, radii)), (w - radii, 0))  # 右上角
	alpha.paste(circle.crop((radii, radii, radii * 2, radii * 2)), (w - radii, h - radii))  # 右下角
	alpha.paste(circle.crop((0, radii, radii, radii * 2)), (0, h - radii))  # 左下角
	# alpha.save('4.jpg','JPEG',qulity=100)
 
	img.putalpha(alpha)		# 白色区域透明可见,黑色区域不可见
	img.save('5.png','PNG',qulity=100)
 
	return img
 
img = circle_corner(img, radii)
img.save('result.png', 'png', quality = 100)

 

Scheme 2: JPG superimposed with the fillet PNG

 

Advantages: no fillet jagged, and PS the same effect with transition effects

Disadvantages: First, the need to produce a radius of 129 * 129 10 black background with a rounded base map PS clipping mask.

FIG Effect: left to PS results (129 * 129 radius of a rounded rectangle as the mask 10), the right is the result code execution.

You can see both close to the same, Python effect is not inferior to PS (after all, PNG or PS export base map of ...). Anyway, the conclusion is that the method is suitable for production of high-definition picture.

The code is simple:

# mkicon
from PIL  import Image,ImageOps

img = Image.open('src.jpg').resize((129,129))

border = Image.open('b.png').convert('L')
invert = ImageOps.invert(border)
invert.save('invert.png')

img.putalpha(invert)
img.save('icon.png','PNG',qulity=100)

Resource requirements as shown:

Code putalpha () is intended to increase the alpha transparent channel, invert () the image color inversion, the specific meaning see pillow library official manual .

 

At last, 

Screenshot thank-made software Snipaste 

. Text images are bloggers for the first time Snipaste interception and editing software, personal development of domestic software to have such a nice, Zhennai industry a blessing.

发布了58 篇原创文章 · 获赞 44 · 访问量 18万+

Guess you like

Origin blog.csdn.net/qilei2010/article/details/104572847
Recommended