Separate/reposition/translate shapes in image with pillow in python

2024/10/14 21:16:31

I need to separate or translate or replace pixels in an image with python so as to all the shapes to share the same distance between each other and the limits of the canvas.

background is white, shapes are black.

IMPORTANT: Images are dynamic, all images have bars at different positions, that means i need to detect where a bar starts and where a bar ends to draw the final image!

This is an example INPUT image

enter image description here

This is an example OUTPUT image

enter image description here

I did this by hand, i don't know if all bars are separated by the same distance, it's just an example.

The resulting canvas size can change too but ideally i would like all canvas to have the same width and height after the bars have been respositioned.

All pixels in the images are BLACK or WHITE, that should make it much easier.

What i've tried so far kind of detects the shapes but some shapes are not detected correctly when they are too close of each other:

saut = 1
start_ab = 0
end_ab = 0
start_or = im2.size[1] - 1
end_or = 0
size = 0shapes = []for y in range(0, im2.size[0], saut):  # slice acrossfor x in range(0, im2.size[1], 1):  # slice downpix = im2.getpixel((y, x))if pix != 255:start_or = min(start_or, x)end_or = max(end_or, x)inshape = Trueif foundshape == False and inshape == True:foundshape = Truestart_ab = yif foundshape == True and inshape == False:foundshape = Falseend_ab = ysize = max(size, end_ab)shapes.append((start_ab, end_ab, start_or, end_or))start_or = im2.size[1] - 1end_or = 0inshape = Falseprint shapes
# example shapes output NOT FROM THE EXAMPLE IMAGES PROVIDED but from other shapes [(54, 171, 72, 233), (216, 324, 108, 251), (342, 486, 0, 215), (513, 765, 18, 260), (792, 918, 90, 242)]

And still have to draw the new image, i don't know how to do that, also some images have shapes with "holes" in them, so that makes the "redraw" a little bit more complicated.

Answer

And still have to draw the new image, i don't know how to do that, also some images have shapes with "holes" in them, so that makes the "redraw" a little bit more complicated.

Once you've established the bounds of a rectangle, you could use the Image.crop() function to extract a rectangle from the image (including those holes) and Image.paste() to paste them in the right position. The tutorial contains an example:

http://pillow.readthedocs.org/en/latest/handbook/tutorial.html#cutting-pasting-and-merging-images

https://en.xdnf.cn/q/117909.html

Related Q&A

django.db.utils.OperationalError: (1045, Access denied for user user@localhost

I cant get my Django project to load my database correctly. It throws this error. Im running MariaDB with Django, and I uninstalled all MySQL I added the user by running:create database foo_db; create …

Paho Python Client with HiveMQ

i am developing a module in python that will allow me to connect my raspberry pi to a version of hivemq hosted on my pc.it connects normally but when i add hivemqs file auth plugin it doesnt seem to wo…

Comparison of value items in a dictionary and counting matches

Im using Python 2.7. Im trying to compare the value items in a dictionary.I have two problems. First is the iteration of values in a dictionary with a length of 1. I always get an error, because python…

how to send cookies inside post request

trying to send Post request with the cookies on my pc from get request #! /usr/bin/python import re #regex import urllib import urllib2 #get request x = urllib2.urlopen("http://www.example.com) #…

Flask-Uploads gives AttributeError?

from flask import Flask from flask.ext.uploads import UploadSet, configure_uploads, IMAGESapp = Flask(__name__)app.config[UPLOADED_PHOTOS_DEST] = /home/kevin photos = UploadSet(photos, IMAGES)configure…

Python: Alternate way to covert from base64 string to opencv

Im trying to convert this string in base64 (http://pastebin.com/uz4ta0RL) to something usable by OpenCV. Currently I am using this code (img_temp is the string) to use the OpenCV functions by convertin…

Move 3D plot to avoid clipping by margins

Im trying to figure out how I can get the 3D matplotlib images below to plot higher on the canvas so it doesnt get clipped. Here is the code Im using to create the plot. I couldnt find a way to attach …

HTML Link parsing using BeautifulSoup

here is my Python code which Im using to extract the Specific HTML from the Page links Im sending as parameter. Im using BeautifulSoup. This code works fine for sometimes and sometimes it is getting st…

XML format change using XSL file in a Python code

Have written a Python code to transform a XML file to a particular format using XSL stylesheet. Python code below:#!/usr/bin/env python # -*- coding:utf-8 -*- from lxml import etree def transform(xmlP…

How to extract a specific value from a dictionary in python

I want to extract distance from google distance matrix API in Python. The objet it returned is a python dictionary.{destination_addresses: [Mumbai, Maharashtra, India ],origin_addresses: [Powai, Mumbai…