Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images are not exported to the correct size #1305

Open
CHAOSdonut opened this issue Mar 19, 2024 · 3 comments
Open

Images are not exported to the correct size #1305

CHAOSdonut opened this issue Mar 19, 2024 · 3 comments
Labels

Comments

@CHAOSdonut
Copy link

CHAOSdonut commented Mar 19, 2024

pythonocc-core: 7.7.2

Using the following code i create an offscreen renderer to create a few folders with renders at different angles. I want these images to be 180x180 so i use offscreen_renderer.SetSize(180, 180)

my code:

import os.path
import random
import sys

from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Display.OCCViewer import Viewer3d
from OCC.Core.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity
from OCC.Extend.ShapeFactory import rotate_shp_3_axis

input_dir = "./input/"
output_dir = "./output/"

# create the renderer
offscreen_renderer = Viewer3d()

# by default, the offscreenrenderer size is 640*480
offscreen_renderer.Create()
offscreen_renderer.SetModeWireFrame()
offscreen_renderer.EnableAntiAliasing()
offscreen_renderer.SetSize(180, 180)

def read_step_file(filename):
    # read the STEP file and returns a compound
    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(os.path.join(input_dir + filename))

    if status == IFSelect_RetDone:  # check status
        failsonly = False
        step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
        step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity)
        step_reader.TransferRoot(1)
        a_shape = step_reader.Shape(1)
    else:
        print("Error: can't read file.")
        sys.exit(0)
    return a_shape


def random_rotate_shape(shape):
    angle_x = random.random() * 360
    angle_y = random.random() * 360
    angle_z = random.random() * 360
    return rotate_shp_3_axis(shape, angle_x, angle_y, angle_z, "deg")


def render_file(filename, amount):
    # create shape from .stp
    shape = read_step_file(filename)

    output_dir_path = os.path.join(output_dir + filename.split(".")[0])
    if not os.path.exists(output_dir_path):
        os.makedirs(output_dir_path)

    for i in range(amount):
        # give the shape a random rotation
        shape = random_rotate_shape(shape)
        # send the shape to the renderer
        offscreen_renderer.DisplayColoredShape(shape, color='WHITE', update=True)

        output_name = os.path.join(output_dir_path + "/" + filename.split(".")[0] + "(" + str(i) + ").png")
        # export the current shape to an image
        offscreen_renderer.ExportToImage(output_name)
        offscreen_renderer.EraseAll()


if __name__ == "__main__":
    for filename in os.listdir(input_dir):
        render_file(filename, 5)

all images generated with this code are 196x219 not 180x180

using offscreen_renderer.SetSize(1920, 1080)
results in images that are 1936x1119

@tpaviot tpaviot added the bug label Mar 21, 2024
@tpaviot
Copy link
Owner

tpaviot commented Mar 21, 2024

Thank you for you report, I never measured the resulting image size and missed that point

@tpaviot
Copy link
Owner

tpaviot commented May 23, 2024

Tested on my machine, images are exported with the correct size

scrsht_1305

@CHAOSdonut
Copy link
Author

I retested the same code on my machine again and it still doesn't work. Could it be a windows issue? Did you try my code or could it be that I missed something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants