Skip to content

Commit

Permalink
Improve rect color conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed Feb 16, 2024
1 parent 545062e commit e9f875b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion MLStructFP/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__description__ = 'Machine learning structural floor plan dataset'
__keywords__ = ['ml', 'ai', 'dataset', 'calc', 'matrix analysis', 'cnn', 'structural analysis', 'structural design']
__email__ = 'pablo@ppizarror.com'
__version__ = '0.4.8'
__version__ = '0.4.9'

# URL
__url__ = 'https://github.com/MLSTRUCT/MLSTRUCT-FP'
Expand Down
17 changes: 13 additions & 4 deletions MLStructFP/db/image/_rect_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ def make_rect(self, rect: 'Rect', crop_length: NumberType = 5) -> Tuple[int, 'np
floor=rect.floor, rect=rect
)

# noinspection PyMethodMayBeStatic
def _convert_image_color(self, im: 'Image.Image') -> 'Image.Image':
"""
Convert image.
:param im: Image
:return: Converted image
"""
return im.convert('P', palette=Image.ADAPTIVE)

def make_region(self, xmin: NumberType, xmax: NumberType, ymin: NumberType, ymax: NumberType,
floor: 'Floor', rect: Optional['Rect'] = None) -> Tuple[int, 'np.ndarray']:
"""
Expand Down Expand Up @@ -158,15 +168,14 @@ def make_region(self, xmin: NumberType, xmax: NumberType, ymin: NumberType, ymax
ax.set_xlim(min(xmin, xmax), max(xmin, xmax))
ax.set_ylim(min(ymin, ymax), max(ymin, ymax))

# Convert
# Convert from matplotlib
ram = io.BytesIO()
plt.savefig(ram, format='png', dpi=100, bbox_inches='tight', transparent=False)
ram.seek(0)
im: 'Image.Image' = Image.open(ram)

im2: 'Image.Image' = im.convert('RGB').convert('P', palette=Image.ADAPTIVE)
# im2 = im.convert('RGB') # Produces larger files
# im2 = im.convert('1')
# Convert color
im2: 'Image.Image' = self._convert_image_color(im.convert('RGB'))

# Resize
s_resize = self._image_size + 2 * self._crop_px
Expand Down

0 comments on commit e9f875b

Please sign in to comment.